({ questionId, answerId })
| 19 | ) => Promise<ActionState<DeleteActionData, Delete>>; |
| 20 | |
| 21 | const DeleteQAForm: React.FC<IVoteFormProps> = ({ questionId, answerId }) => { |
| 22 | const idForm = useId(); |
| 23 | const router = useRouter(); |
| 24 | const deleteAction: DeleteAction = async ({ questionId, answerId }) => { |
| 25 | if (questionId) { |
| 26 | return deleteQuestion({ questionId }); |
| 27 | } else if (answerId) { |
| 28 | return deleteAnswer({ answerId }); |
| 29 | } |
| 30 | throw new Error('Neither questionId nor answerId is provided'); |
| 31 | }; |
| 32 | |
| 33 | const { execute } = useAction(deleteAction, { |
| 34 | onSuccess: (data) => { |
| 35 | toast.success(`${data.message}`); |
| 36 | if (questionId) { |
| 37 | router.push('/question'); |
| 38 | } |
| 39 | }, |
| 40 | onError: (error) => { |
| 41 | toast.error(error); |
| 42 | }, |
| 43 | }); |
| 44 | |
| 45 | const hanleDeleteFunction = () => { |
| 46 | execute(questionId ? { questionId } : { answerId }); |
| 47 | }; |
| 48 | |
| 49 | return ( |
| 50 | <Button |
| 51 | id={`delete-${idForm}`} |
| 52 | onClick={hanleDeleteFunction} |
| 53 | size="icon" |
| 54 | variant="destructive" |
| 55 | > |
| 56 | <Trash2 className="size-4" /> |
| 57 | </Button> |
| 58 | ); |
| 59 | }; |
| 60 | |
| 61 | export default DeleteQAForm; |
nothing calls this directly
no test coverage detected