More work on pl-api migration
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
export { useEntities } from './useEntities';
|
||||
export { useEntity } from './useEntity';
|
||||
export { useEntityActions } from './useEntityActions';
|
||||
export { useEntityLookup } from './useEntityLookup';
|
||||
export { useCreateEntity } from './useCreateEntity';
|
||||
export { useDeleteEntity } from './useDeleteEntity';
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import type { Entity } from '../types';
|
||||
import type { PlApiResponse } from 'pl-api';
|
||||
import type z from 'zod';
|
||||
|
||||
type EntitySchema<TEntity extends Entity = Entity> = z.ZodType<TEntity, z.ZodTypeDef, any>;
|
||||
@ -35,7 +34,7 @@ interface EntityCallbacks<Value, Error = unknown> {
|
||||
* Passed into hooks to make requests.
|
||||
* Must return a response.
|
||||
*/
|
||||
type EntityFn<T> = (value: T) => Promise<T>
|
||||
type EntityFn<T> = (value: T) => Promise<any>;
|
||||
|
||||
export type {
|
||||
EntitySchema,
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import { useEffect } from 'react';
|
||||
import z from 'zod';
|
||||
|
||||
import { useClient } from 'soapbox/hooks';
|
||||
import { useAppDispatch } from 'soapbox/hooks/useAppDispatch';
|
||||
import { useAppSelector } from 'soapbox/hooks/useAppSelector';
|
||||
import { useGetState } from 'soapbox/hooks/useGetState';
|
||||
@ -38,7 +37,6 @@ const useEntities = <TEntity extends Entity>(
|
||||
/** Additional options for the hook. */
|
||||
opts: UseEntitiesOpts<TEntity> = {},
|
||||
) => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
const getState = useGetState();
|
||||
|
||||
|
||||
@ -1,49 +0,0 @@
|
||||
import { useClient } from 'soapbox/hooks';
|
||||
|
||||
import { useCreateEntity } from './useCreateEntity';
|
||||
import { useDeleteEntity } from './useDeleteEntity';
|
||||
import { parseEntitiesPath } from './utils';
|
||||
|
||||
import type { EntitySchema, ExpandedEntitiesPath } from './types';
|
||||
import type { Entity } from '../types';
|
||||
|
||||
interface UseEntityActionsOpts<TEntity extends Entity = Entity> {
|
||||
schema?: EntitySchema<TEntity>;
|
||||
}
|
||||
|
||||
interface EntityActionEndpoints {
|
||||
delete?: string;
|
||||
patch?: string;
|
||||
post?: string;
|
||||
}
|
||||
|
||||
const useEntityActions = <TEntity extends Entity = Entity, Data = any>(
|
||||
expandedPath: ExpandedEntitiesPath,
|
||||
endpoints: EntityActionEndpoints,
|
||||
opts: UseEntityActionsOpts<TEntity> = {},
|
||||
) => {
|
||||
const client = useClient();
|
||||
const { entityType, path } = parseEntitiesPath(expandedPath);
|
||||
|
||||
const { deleteEntity, isSubmitting: deleteSubmitting } =
|
||||
useDeleteEntity(entityType, (entityId) => client.request(endpoints.delete!.replace(/:id/g, entityId), { method: 'DELETE' }));
|
||||
|
||||
const { createEntity, isSubmitting: createSubmitting } =
|
||||
useCreateEntity<TEntity, Data>(path, (data) => client.request(endpoints.post!, {
|
||||
method: 'POST', body: data,
|
||||
}), opts);
|
||||
|
||||
const { createEntity: updateEntity, isSubmitting: updateSubmitting } =
|
||||
useCreateEntity<TEntity, Data>(path, (data) => client.request(endpoints.patch!, {
|
||||
method: 'PATCH', body: data,
|
||||
}), opts);
|
||||
|
||||
return {
|
||||
createEntity,
|
||||
deleteEntity,
|
||||
updateEntity,
|
||||
isSubmitting: createSubmitting || deleteSubmitting || updateSubmitting,
|
||||
};
|
||||
};
|
||||
|
||||
export { useEntityActions };
|
||||
Reference in New Issue
Block a user