soapbox/app/soapbox/entity-store/hooks/useDeleteEntity.ts

61 wiersze
1.8 KiB
TypeScript
Czysty Zwykły widok Historia

2023-03-23 21:22:15 +00:00
import { useAppDispatch, useGetState } from 'soapbox/hooks';
import { deleteEntities, importEntities } from '../actions';
2023-03-23 21:22:15 +00:00
import { useEntityRequest } from './useEntityRequest';
import { toAxiosRequest } from './utils';
import type { EntityCallbacks, EntityRequest } from './types';
/**
* Optimistically deletes an entity from the store.
* This hook should be used to globally delete an entity from all lists.
* To remove an entity from a single list, see `useDismissEntity`.
*/
function useDeleteEntity(
entityType: string,
2023-03-23 21:22:15 +00:00
entityRequest: EntityRequest,
) {
const dispatch = useAppDispatch();
const getState = useGetState();
2023-03-23 21:22:15 +00:00
const { request, isLoading } = useEntityRequest();
async function deleteEntity(entityId: string, callbacks: EntityCallbacks<string> = {}): Promise<void> {
// Get the entity before deleting, so we can reverse the action if the API request fails.
const entity = getState().entities[entityType]?.store[entityId];
// Optimistically delete the entity from the _store_ but keep the lists in tact.
dispatch(deleteEntities([entityId], entityType, { preserveLists: true }));
try {
// HACK: replace occurrences of `:id` in the URL. Maybe there's a better way?
2023-03-23 21:22:15 +00:00
const axiosReq = toAxiosRequest(entityRequest);
axiosReq.url?.replaceAll(':id', entityId);
2023-03-23 21:22:15 +00:00
await request(axiosReq);
// Success - finish deleting entity from the state.
dispatch(deleteEntities([entityId], entityType));
if (callbacks.onSuccess) {
callbacks.onSuccess(entityId);
}
} catch (e) {
if (entity) {
// If the API failed, reimport the entity.
dispatch(importEntities([entity], entityType));
}
if (callbacks.onError) {
callbacks.onError(e);
}
}
}
return {
deleteEntity,
isLoading,
};
}
export { useDeleteEntity };