kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
Move useCreateEntity into its own hook as well, because why not
rodzic
b76559f24a
commit
b127025167
|
@ -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 };
|
|
@ -1,14 +1,10 @@
|
||||||
import { z } from 'zod';
|
import { useApi } from 'soapbox/hooks';
|
||||||
|
|
||||||
import { useApi, useAppDispatch } from 'soapbox/hooks';
|
|
||||||
|
|
||||||
import { importEntities } from '../actions';
|
|
||||||
|
|
||||||
|
import { useCreateEntity } from './useCreateEntity';
|
||||||
import { useDeleteEntity } from './useDeleteEntity';
|
import { useDeleteEntity } from './useDeleteEntity';
|
||||||
|
|
||||||
import type { Entity } from '../types';
|
import type { Entity } from '../types';
|
||||||
import type { EntitySchema } from './types';
|
import type { EntitySchema } from './types';
|
||||||
import type { AxiosResponse } from 'axios';
|
|
||||||
|
|
||||||
type EntityPath = [entityType: string, listKey?: string]
|
type EntityPath = [entityType: string, listKey?: string]
|
||||||
|
|
||||||
|
@ -16,59 +12,32 @@ interface UseEntityActionsOpts<TEntity extends Entity = Entity> {
|
||||||
schema?: EntitySchema<TEntity>
|
schema?: EntitySchema<TEntity>
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CreateEntityResult<TEntity extends Entity = Entity> {
|
|
||||||
response: AxiosResponse
|
|
||||||
entity: TEntity
|
|
||||||
}
|
|
||||||
|
|
||||||
interface EntityActionEndpoints {
|
interface EntityActionEndpoints {
|
||||||
post?: string
|
post?: string
|
||||||
delete?: string
|
delete?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EntityCallbacks<TEntity extends Entity = Entity> {
|
function useEntityActions<TEntity extends Entity = Entity, Params = any>(
|
||||||
onSuccess?(entity: TEntity): void
|
|
||||||
}
|
|
||||||
|
|
||||||
function useEntityActions<TEntity extends Entity = Entity, P = any>(
|
|
||||||
path: EntityPath,
|
path: EntityPath,
|
||||||
endpoints: EntityActionEndpoints,
|
endpoints: EntityActionEndpoints,
|
||||||
opts: UseEntityActionsOpts<TEntity> = {},
|
opts: UseEntityActionsOpts<TEntity> = {},
|
||||||
) {
|
) {
|
||||||
const [entityType, listKey] = path;
|
|
||||||
|
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
const dispatch = useAppDispatch();
|
const [entityType] = path;
|
||||||
|
|
||||||
const deleteEntity = useDeleteEntity(entityType, (entityId) => {
|
const deleteEntity = useDeleteEntity(entityType, (entityId) => {
|
||||||
if (!endpoints.delete) return Promise.reject(endpoints);
|
if (!endpoints.delete) return Promise.reject(endpoints);
|
||||||
return api.delete(endpoints.delete.replace(':id', entityId));
|
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);
|
if (!endpoints.post) return Promise.reject(endpoints);
|
||||||
|
return api.post(endpoints.post, params);
|
||||||
return api.post(endpoints.post, params).then((response) => {
|
}, opts);
|
||||||
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 {
|
return {
|
||||||
createEntity: createEntity,
|
createEntity,
|
||||||
deleteEntity: endpoints.delete ? deleteEntity : undefined,
|
deleteEntity,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue