More work on pl-api migration

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak
2024-08-06 23:19:00 +02:00
parent 32c68a9221
commit 0fc8a2993f
53 changed files with 119 additions and 183 deletions

View File

@ -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';

View File

@ -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,

View File

@ -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();

View File

@ -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 };