Move useCreateEntity into its own hook as well, because why not

environments/review-group-requ-7youoa/deployments/2945
Alex Gleason 2023-03-22 15:31:58 -05:00
rodzic b76559f24a
commit b127025167
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
2 zmienionych plików z 83 dodań i 40 usunięć

Wyświetl plik

@ -0,0 +1,74 @@
import { z } from 'zod';
import { useAppDispatch } from 'soapbox/hooks';
import { importEntities } from '../actions';
import type { Entity } from '../types';
import type { EntitySchema } from './types';
type EntityPath = [entityType: string, listKey?: string]
type CreateFn<Params, Result> = (params: Params) => Promise<Result> | Result;
interface UseCreateEntityOpts<TEntity extends Entity = Entity> {
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> {
onSuccess?(entity: TEntity): void
onError?(error: Error): void
}
function useCreateEntity<TEntity extends Entity = Entity, Params = any, Result = unknown>(
path: EntityPath,
createFn: CreateFn<Params, Result>,
opts: UseCreateEntityOpts<TEntity> = {},
) {
const [entityType, listKey] = path;
const dispatch = useAppDispatch();
return async function createEntity(
params: Params,
callbacks: EntityCallbacks = {},
): Promise<CreateEntityResult<TEntity>> {
try {
const result = await createFn(params);
const schema = opts.schema || z.custom<TEntity>();
const entity = schema.parse(result);
// TODO: optimistic updating
dispatch(importEntities([entity], entityType, listKey));
if (callbacks.onSuccess) {
callbacks.onSuccess(entity);
}
return {
success: true,
result,
entity,
};
} catch (error) {
if (callbacks.onError) {
callbacks.onError(error);
}
return {
success: false,
error,
};
}
};
}
export { useCreateEntity };

Wyświetl plik

@ -1,14 +1,10 @@
import { z } from 'zod';
import { useApi, useAppDispatch } from 'soapbox/hooks';
import { importEntities } from '../actions';
import { useApi } from 'soapbox/hooks';
import { useCreateEntity } from './useCreateEntity';
import { useDeleteEntity } from './useDeleteEntity';
import type { Entity } from '../types';
import type { EntitySchema } from './types';
import type { AxiosResponse } from 'axios';
type EntityPath = [entityType: string, listKey?: string]
@ -16,59 +12,32 @@ interface UseEntityActionsOpts<TEntity extends Entity = Entity> {
schema?: EntitySchema<TEntity>
}
interface CreateEntityResult<TEntity extends Entity = Entity> {
response: AxiosResponse
entity: TEntity
}
interface EntityActionEndpoints {
post?: string
delete?: string
}
interface EntityCallbacks<TEntity extends Entity = Entity> {
onSuccess?(entity: TEntity): void
}
function useEntityActions<TEntity extends Entity = Entity, P = any>(
function useEntityActions<TEntity extends Entity = Entity, Params = any>(
path: EntityPath,
endpoints: EntityActionEndpoints,
opts: UseEntityActionsOpts<TEntity> = {},
) {
const [entityType, listKey] = path;
const api = useApi();
const dispatch = useAppDispatch();
const [entityType] = path;
const deleteEntity = useDeleteEntity(entityType, (entityId) => {
if (!endpoints.delete) return Promise.reject(endpoints);
return api.delete(endpoints.delete.replace(':id', entityId));
});
function createEntity(params: P, callbacks: EntityCallbacks = {}): Promise<CreateEntityResult<TEntity>> {
const createEntity = useCreateEntity(path, (params: Params) => {
if (!endpoints.post) return Promise.reject(endpoints);
return api.post(endpoints.post, params).then((response) => {
const schema = opts.schema || z.custom<TEntity>();
const entity = schema.parse(response.data);
// TODO: optimistic updating
dispatch(importEntities([entity], entityType, listKey));
if (callbacks.onSuccess) {
callbacks.onSuccess(entity);
}
return {
response,
entity,
};
});
}
return api.post(endpoints.post, params);
}, opts);
return {
createEntity: createEntity,
deleteEntity: endpoints.delete ? deleteEntity : undefined,
createEntity,
deleteEntity,
};
}