From 50f65bc7c97db6e127553c1811edaa66d7c9296e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 23 Mar 2023 14:52:38 -0500 Subject: [PATCH] useCreateEntity: pass an EntityRequest, refactor --- .../entity-store/hooks/useCreateEntity.ts | 50 ++++++------------- .../entity-store/hooks/useEntityActions.ts | 13 ++--- 2 files changed, 23 insertions(+), 40 deletions(-) diff --git a/app/soapbox/entity-store/hooks/useCreateEntity.ts b/app/soapbox/entity-store/hooks/useCreateEntity.ts index 373434e73c..432bea8fa5 100644 --- a/app/soapbox/entity-store/hooks/useCreateEntity.ts +++ b/app/soapbox/entity-store/hooks/useCreateEntity.ts @@ -1,52 +1,45 @@ import { z } from 'zod'; -import { useAppDispatch } from 'soapbox/hooks'; +import { useApi, useAppDispatch } from 'soapbox/hooks'; import { importEntities } from '../actions'; -import { parseEntitiesPath } from './utils'; +import { parseEntitiesPath, toAxiosRequest } from './utils'; import type { Entity } from '../types'; -import type { EntitySchema, ExpandedEntitiesPath } from './types'; - -type CreateFn = (params: Params) => Promise | Result; +import type { EntityRequest, EntitySchema, ExpandedEntitiesPath } from './types'; interface UseCreateEntityOpts { schema?: EntitySchema } -type CreateEntityResult = - { - success: true - result: Result - entity: TEntity - } | { - success: false - error: Error - } - interface EntityCallbacks { onSuccess?(entity: TEntity): void onError?(error: Error): void } -function useCreateEntity( +function useCreateEntity( expandedPath: ExpandedEntitiesPath, - createFn: CreateFn, + request: EntityRequest, opts: UseCreateEntityOpts = {}, ) { - const { entityType, listKey } = parseEntitiesPath(expandedPath); - + const api = useApi(); const dispatch = useAppDispatch(); + const { entityType, listKey } = parseEntitiesPath(expandedPath); + return async function createEntity( - params: Params, + data: Data, callbacks: EntityCallbacks = {}, - ): Promise> { + ): Promise { try { - const result = await createFn(params); + const result = await api.request({ + ...toAxiosRequest(request), + data, + }); + const schema = opts.schema || z.custom(); - const entity = schema.parse(result); + const entity = schema.parse(result.data); // TODO: optimistic updating dispatch(importEntities([entity], entityType, listKey)); @@ -54,21 +47,10 @@ function useCreateEntity( +function useEntityActions( expandedPath: ExpandedEntitiesPath, endpoints: EntityActionEndpoints, opts: UseEntityActionsOpts = {}, @@ -34,11 +34,12 @@ function useEntityActions( .finally(() => setIsLoading(false)); }); - const createEntity = useCreateEntity(path, (params: Params) => { - if (!endpoints.post) return Promise.reject(endpoints); - return api.post(endpoints.post, params) - .finally(() => setIsLoading(false)); - }, opts); + const create = useCreateEntity(path, { method: 'post', url: endpoints.post }, opts); + + const createEntity: typeof create = async (...args) => { + await create(...args); + setIsLoading(false); + }; return { createEntity,