import { useState } from 'react'; import { useCreateEntity } from './useCreateEntity'; import { useDeleteEntity } from './useDeleteEntity'; import { parseEntitiesPath } from './utils'; import type { Entity } from '../types'; import type { EntitySchema, ExpandedEntitiesPath } from './types'; interface UseEntityActionsOpts { schema?: EntitySchema } interface EntityActionEndpoints { post?: string delete?: string } function useEntityActions( expandedPath: ExpandedEntitiesPath, endpoints: EntityActionEndpoints, opts: UseEntityActionsOpts = {}, ) { const { entityType, path } = parseEntitiesPath(expandedPath); const [isLoading, setIsLoading] = useState(false); const _delete = useDeleteEntity(entityType, { method: 'delete', url: endpoints.delete }); const create = useCreateEntity(path, { method: 'post', url: endpoints.post }, opts); const createEntity: typeof create = async (...args) => { setIsLoading(true); await create(...args); setIsLoading(false); }; const deleteEntity: typeof _delete = async (...args) => { setIsLoading(true); await _delete(...args); setIsLoading(false); }; return { createEntity, deleteEntity, isLoading, }; } export { useEntityActions };