Vercel/src/admin/DeletePhotosButton.tsx
2024-07-26 14:18:33 -05:00

61 lines
1.7 KiB
TypeScript

'use client';
import LoaderButton from '@/components/primitives/LoaderButton';
import { photoQuantityText } from '@/photo';
import { deletePhotosAction } from '@/photo/actions';
import { useAppState } from '@/state/AppState';
import { toastSuccess, toastWarning } from '@/toast';
import { ComponentProps, useState } from 'react';
import DeleteButton from './DeleteButton';
export default function DeletePhotosButton({
photoIds = [],
onDelete,
clearLocalState = true,
onClick,
onFinish,
confirmText,
toastText,
...rest
}: {
photoIds?: string[]
onClick?: () => void
onFinish?: () => void
onDelete?: () => void
clearLocalState?: boolean
toastText?: string
} & ComponentProps<typeof LoaderButton>) {
const [isLoading, setIsLoading] = useState(false);
const photosText = photoQuantityText(photoIds.length, false, false);
const { invalidateSwr, registerAdminUpdate } = useAppState();
return (
<DeleteButton
{...rest}
isLoading={isLoading}
// eslint-disable-next-line max-len
confirmText={confirmText ?? `Are you sure you want to delete ${photosText}? This action cannot be undone.`}
onClick={() => {
onClick?.();
setIsLoading(true);
deletePhotosAction(photoIds)
.then(() => {
toastSuccess(toastText ?? `${photosText} deleted`);
if (clearLocalState) {
invalidateSwr?.();
registerAdminUpdate?.();
}
onDelete?.();
})
.catch(() => toastWarning(`Failed to delete ${photosText}`))
.finally(() => {
setIsLoading(false);
onFinish?.();
});
}}
/>
);
}