This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
/
DeleteFeedbackButton.js
74 lines (69 loc) · 1.79 KB
/
DeleteFeedbackButton.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { useState, useRef } from 'react';
import { mutate } from 'swr';
import {
AlertDialog,
AlertDialogBody,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogContent,
AlertDialogOverlay,
IconButton,
Button
} from '@chakra-ui/core';
import { deleteFeedback } from '@/lib/db';
import { useAuth } from '@/lib/auth';
const DeleteFeedbackButton = ({ feedbackId }) => {
const [isOpen, setIsOpen] = useState();
const cancelRef = useRef();
const auth = useAuth();
const onClose = () => setIsOpen(false);
const onDelete = () => {
deleteFeedback(feedbackId);
mutate(
['/api/feedback', auth.user.token],
async (data) => {
return {
feedback: data.feedback.filter(
(feedback) => feedback.id !== feedbackId
)
};
},
false
);
onClose();
};
return (
<>
<IconButton
aria-label="Delete feedback"
icon="delete"
variant="ghost"
onClick={() => setIsOpen(true)}
/>
<AlertDialog
isOpen={isOpen}
leastDestructiveRef={cancelRef}
onClose={onClose}
>
<AlertDialogOverlay />
<AlertDialogContent>
<AlertDialogHeader fontSize="lg" fontWeight="bold">
Delete Feedback
</AlertDialogHeader>
<AlertDialogBody>
Are you sure? You can't undo this action afterwards.
</AlertDialogBody>
<AlertDialogFooter>
<Button ref={cancelRef} onClick={onClose}>
Cancel
</Button>
<Button fontWeight="bold" variantColor="red" onClick={onDelete} ml={3}>
Delete
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
);
};
export default DeleteFeedbackButton;