2023-03-23 13:30:45 -07:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
2023-03-23 13:05:34 -07:00
|
|
|
import { useApi, useAppDispatch, useGetState } from 'soapbox/hooks';
|
2023-03-22 12:05:24 -07:00
|
|
|
|
|
|
|
import { deleteEntities, importEntities } from '../actions';
|
|
|
|
|
2023-03-23 13:05:34 -07:00
|
|
|
import { toAxiosRequest } from './utils';
|
2023-03-22 12:05:24 -07:00
|
|
|
|
2023-03-23 13:05:34 -07:00
|
|
|
import type { EntityRequest } from './types';
|
|
|
|
|
|
|
|
interface DeleteEntityCallbacks {
|
2023-03-23 08:45:49 -07:00
|
|
|
onSuccess?(): void
|
2023-03-23 13:05:34 -07:00
|
|
|
onError?(): void
|
2023-03-23 08:45:49 -07:00
|
|
|
}
|
|
|
|
|
2023-03-22 12:40:18 -07:00
|
|
|
/**
|
|
|
|
* 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`.
|
|
|
|
*/
|
2023-03-23 13:05:34 -07:00
|
|
|
function useDeleteEntity(
|
2023-03-23 08:45:49 -07:00
|
|
|
entityType: string,
|
2023-03-23 13:05:34 -07:00
|
|
|
request: EntityRequest,
|
2023-03-23 08:45:49 -07:00
|
|
|
) {
|
2023-03-23 13:05:34 -07:00
|
|
|
const api = useApi();
|
2023-03-22 12:05:24 -07:00
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
const getState = useGetState();
|
2023-03-23 13:30:45 -07:00
|
|
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
|
|
|
|
|
|
async function deleteEntity(entityId: string, callbacks: DeleteEntityCallbacks = {}): Promise<void> {
|
|
|
|
setIsLoading(true);
|
2023-03-22 12:05:24 -07:00
|
|
|
|
|
|
|
// 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 {
|
2023-03-23 13:05:34 -07:00
|
|
|
// HACK: replace occurrences of `:id` in the URL. Maybe there's a better way?
|
|
|
|
const axiosReq = toAxiosRequest(request);
|
|
|
|
axiosReq.url?.replaceAll(':id', entityId);
|
|
|
|
|
|
|
|
await api.request(axiosReq);
|
|
|
|
|
2023-03-22 12:05:24 -07:00
|
|
|
// Success - finish deleting entity from the state.
|
|
|
|
dispatch(deleteEntities([entityId], entityType));
|
2023-03-23 08:45:49 -07:00
|
|
|
|
|
|
|
if (callbacks.onSuccess) {
|
|
|
|
callbacks.onSuccess();
|
|
|
|
}
|
2023-03-22 12:05:24 -07:00
|
|
|
} catch (e) {
|
|
|
|
if (entity) {
|
|
|
|
// If the API failed, reimport the entity.
|
|
|
|
dispatch(importEntities([entity], entityType));
|
|
|
|
}
|
2023-03-23 13:05:34 -07:00
|
|
|
|
|
|
|
if (callbacks.onError) {
|
|
|
|
callbacks.onError();
|
|
|
|
}
|
2023-03-22 12:05:24 -07:00
|
|
|
}
|
2023-03-23 13:30:45 -07:00
|
|
|
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
deleteEntity,
|
|
|
|
isLoading,
|
2023-03-22 12:05:24 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useDeleteEntity };
|