2023-02-06 21:17:46 -05:00
|
|
|
import React from 'react';
|
2023-02-07 10:26:19 -05:00
|
|
|
import TrashIcon from '../svg/TrashIcon';
|
2023-02-11 10:22:15 -05:00
|
|
|
import CrossIcon from '../svg/CrossIcon';
|
2023-02-07 16:22:35 -05:00
|
|
|
import manualSWR from '~/utils/fetchers';
|
|
|
|
|
import { useDispatch } from 'react-redux';
|
2023-03-09 18:57:37 -05:00
|
|
|
import { setNewConvo, removeConvo } from '~/store/convoSlice';
|
2023-02-07 16:22:35 -05:00
|
|
|
import { setMessages } from '~/store/messageSlice';
|
2023-03-11 22:35:39 -05:00
|
|
|
import { setSubmission } from '~/store/submitSlice';
|
2023-02-07 16:22:35 -05:00
|
|
|
|
2023-03-11 22:59:32 -05:00
|
|
|
export default function DeleteButton({ conversationId, renaming, cancelHandler, retainView }) {
|
2023-02-07 16:22:35 -05:00
|
|
|
const dispatch = useDispatch();
|
2023-02-21 21:31:36 -05:00
|
|
|
const { trigger } = manualSWR(
|
2023-03-10 21:06:13 +08:00
|
|
|
`/api/convos/clear`,
|
2023-02-07 16:22:35 -05:00
|
|
|
'post',
|
|
|
|
|
() => {
|
|
|
|
|
dispatch(setMessages([]));
|
2023-03-07 13:53:23 -05:00
|
|
|
dispatch(removeConvo(conversationId));
|
2023-03-09 18:57:37 -05:00
|
|
|
dispatch(setNewConvo());
|
2023-03-11 22:35:39 -05:00
|
|
|
dispatch(setSubmission({}));
|
2023-03-11 22:59:32 -05:00
|
|
|
retainView();
|
2023-02-07 16:22:35 -05:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const clickHandler = () => trigger({ conversationId });
|
2023-02-11 10:22:15 -05:00
|
|
|
const handler = renaming ? cancelHandler : clickHandler;
|
2023-02-06 21:17:46 -05:00
|
|
|
|
|
|
|
|
return (
|
2023-02-07 16:22:35 -05:00
|
|
|
<button
|
|
|
|
|
className="p-1 hover:text-white"
|
2023-02-11 10:22:15 -05:00
|
|
|
onClick={handler}
|
2023-02-07 16:22:35 -05:00
|
|
|
>
|
2023-02-11 10:22:15 -05:00
|
|
|
{ renaming ? <CrossIcon/> : <TrashIcon />}
|
2023-02-06 21:17:46 -05:00
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|