useCreateEntity: pass an EntityRequest, refactor

This commit is contained in:
Alex Gleason 2023-03-23 14:52:38 -05:00
parent ad3f8acbe5
commit 50f65bc7c9
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 23 additions and 40 deletions

View file

@ -1,52 +1,45 @@
import { z } from 'zod'; import { z } from 'zod';
import { useAppDispatch } from 'soapbox/hooks'; import { useApi, useAppDispatch } from 'soapbox/hooks';
import { importEntities } from '../actions'; import { importEntities } from '../actions';
import { parseEntitiesPath } from './utils'; import { parseEntitiesPath, toAxiosRequest } from './utils';
import type { Entity } from '../types'; import type { Entity } from '../types';
import type { EntitySchema, ExpandedEntitiesPath } from './types'; import type { EntityRequest, EntitySchema, ExpandedEntitiesPath } from './types';
type CreateFn<Params, Result> = (params: Params) => Promise<Result> | Result;
interface UseCreateEntityOpts<TEntity extends Entity = Entity> { interface UseCreateEntityOpts<TEntity extends Entity = Entity> {
schema?: EntitySchema<TEntity> schema?: EntitySchema<TEntity>
} }
type CreateEntityResult<TEntity extends Entity = Entity, Result = unknown, Error = unknown> =
{
success: true
result: Result
entity: TEntity
} | {
success: false
error: Error
}
interface EntityCallbacks<TEntity extends Entity = Entity, Error = unknown> { interface EntityCallbacks<TEntity extends Entity = Entity, Error = unknown> {
onSuccess?(entity: TEntity): void onSuccess?(entity: TEntity): void
onError?(error: Error): void onError?(error: Error): void
} }
function useCreateEntity<TEntity extends Entity = Entity, Params = any, Result = unknown>( function useCreateEntity<TEntity extends Entity = Entity, Data = any>(
expandedPath: ExpandedEntitiesPath, expandedPath: ExpandedEntitiesPath,
createFn: CreateFn<Params, Result>, request: EntityRequest,
opts: UseCreateEntityOpts<TEntity> = {}, opts: UseCreateEntityOpts<TEntity> = {},
) { ) {
const { entityType, listKey } = parseEntitiesPath(expandedPath); const api = useApi();
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const { entityType, listKey } = parseEntitiesPath(expandedPath);
return async function createEntity( return async function createEntity(
params: Params, data: Data,
callbacks: EntityCallbacks = {}, callbacks: EntityCallbacks = {},
): Promise<CreateEntityResult<TEntity>> { ): Promise<void> {
try { try {
const result = await createFn(params); const result = await api.request({
...toAxiosRequest(request),
data,
});
const schema = opts.schema || z.custom<TEntity>(); const schema = opts.schema || z.custom<TEntity>();
const entity = schema.parse(result); const entity = schema.parse(result.data);
// TODO: optimistic updating // TODO: optimistic updating
dispatch(importEntities([entity], entityType, listKey)); dispatch(importEntities([entity], entityType, listKey));
@ -54,21 +47,10 @@ function useCreateEntity<TEntity extends Entity = Entity, Params = any, Result =
if (callbacks.onSuccess) { if (callbacks.onSuccess) {
callbacks.onSuccess(entity); callbacks.onSuccess(entity);
} }
return {
success: true,
result,
entity,
};
} catch (error) { } catch (error) {
if (callbacks.onError) { if (callbacks.onError) {
callbacks.onError(error); callbacks.onError(error);
} }
return {
success: false,
error,
};
} }
}; };
} }

View file

@ -18,7 +18,7 @@ interface EntityActionEndpoints {
delete?: string delete?: string
} }
function useEntityActions<TEntity extends Entity = Entity, Params = any>( function useEntityActions<TEntity extends Entity = Entity, Data = any>(
expandedPath: ExpandedEntitiesPath, expandedPath: ExpandedEntitiesPath,
endpoints: EntityActionEndpoints, endpoints: EntityActionEndpoints,
opts: UseEntityActionsOpts<TEntity> = {}, opts: UseEntityActionsOpts<TEntity> = {},
@ -34,11 +34,12 @@ function useEntityActions<TEntity extends Entity = Entity, Params = any>(
.finally(() => setIsLoading(false)); .finally(() => setIsLoading(false));
}); });
const createEntity = useCreateEntity(path, (params: Params) => { const create = useCreateEntity<TEntity, Data>(path, { method: 'post', url: endpoints.post }, opts);
if (!endpoints.post) return Promise.reject(endpoints);
return api.post(endpoints.post, params) const createEntity: typeof create = async (...args) => {
.finally(() => setIsLoading(false)); await create(...args);
}, opts); setIsLoading(false);
};
return { return {
createEntity, createEntity,