-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[id].tsx
More file actions
215 lines (206 loc) · 5.23 KB
/
Copy path[id].tsx
File metadata and controls
215 lines (206 loc) · 5.23 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import {
Flex,
Text,
Box,
Divider,
Textarea,
Button,
Spinner,
Center,
useToast,
Spacer,
AlertDialogOverlay,
AlertDialogContent,
AlertDialogBody,
AlertDialogFooter,
AlertDialogHeader,
AlertDialog,
} from '@chakra-ui/react';
import { DeleteIcon, EditIcon } from '@chakra-ui/icons';
import { useRouter } from 'next/router';
import { Container } from '@/layout/container';
import useSWR from 'swr';
import { deletePost, fetchPostById } from '@/lib/posts';
import { postReply } from '@/lib/replies';
import { useState, useRef } from 'react';
import { userStore } from '@/lib/store';
///
export default function PostPage() {
const [value, setValue] = useState('');
const user = userStore((state) => state.user);
const toast = useToast();
const router = useRouter();
///
const { id } = router.query;
const postApiUrl = `${process.env.NEXT_PUBLIC_API_URL!}/api/v1/posts/${id}`;
const {
data: post,
mutate,
isValidating,
} = useSWR(postApiUrl, fetchPostById);
///
const handleInputChange = (e: any): void => {
let inputValue = e.target.value;
setValue(inputValue);
};
const onSubmit = async (): Promise<void> => {
if (user && value) {
const result = await postReply({
body: value,
author: user.id,
postId: id as string,
});
setValue('');
if (!result) {
toast({
title: 'Could not reply',
status: 'error',
duration: 2000,
});
return;
}
mutate({ ...post!, replies: [...post!.replies!, result] });
toast({
title: 'Reply created',
status: 'success',
duration: 2000,
});
}
};
const onDeleteHandler = async (): Promise<void> => {
const wasDeleted = await deletePost(postApiUrl);
if (wasDeleted) {
toast({
title: 'Post deleted sucessfully',
status: 'success',
duration: 2000,
});
router.push('/home');
return;
}
toast({
title: 'Coult not delete post, please try again later',
status: 'error',
duration: 2000,
});
return;
};
///
return (
<Container title={'Post'}>
{!isValidating && post ? (
<Box position="sticky" p="10">
<Text fontWeight="bold" fontSize="xl">
{post?.title}
</Text>
<Text fontSize="md">{post?.body}</Text>
<Text textAlign="right" fontSize="xs">
~{post?.author}
</Text>
{user?.id === post.author ? (
<Flex w="full" flexDir="row" experimental_spaceX="3" py="2">
<Spacer />
{/* <EditIcon /> */}
<CustomDeleteButton onDelete={onDeleteHandler} />
</Flex>
) : (
''
)}
</Box>
) : (
<Center p="20">
<Spinner />
</Center>
)}
<Divider />
<Box
px="10"
py="5"
experimental_spaceY="2"
alignContent="end"
alignItems="end"
flexDir="column"
placeContent="end"
>
<Textarea
borderRadius="lg"
value={value}
onChange={handleInputChange}
placeholder="Write something ..."
size="sm"
type="submit"
/>
<Button
onClick={onSubmit}
alignSelf="end"
placeSelf="end"
type="submit"
>
Submit
</Button>
</Box>
<Flex
flexDir="column"
align="stretch"
px="10"
py="5"
experimental_spaceY="5"
>
{!isValidating ? (
post?.replies?.map((e) => (
<Box key={e.ID} borderWidth="1" borderColor="gray">
<Text fontSize="x-small">{e.author}</Text>
<Text fontSize="md">{e.body}</Text>
<Divider borderColor="gray.300" py="2" />
</Box>
))
) : (
<Center>
<Spinner size="xl" />
</Center>
)}
</Flex>
</Container>
);
}
const CustomDeleteButton = ({ onDelete }: { onDelete: () => void }) => {
const [isOpen, setIsOpen] = useState(false);
const onClose = () => setIsOpen(false);
const cancelRef = useRef(null);
return (
<>
<DeleteIcon color="red" onClick={() => setIsOpen(true)} />
<AlertDialog
isOpen={isOpen}
leastDestructiveRef={cancelRef}
onClose={onClose}
>
<AlertDialogOverlay textColor="black">
<AlertDialogContent>
<AlertDialogHeader textColor="teal" fontSize="lg" fontWeight="bold">
Delete Customer
</AlertDialogHeader>
<AlertDialogBody>
Are you sure? You can't undo this action afterwards.
</AlertDialogBody>
<AlertDialogFooter>
<Button ref={cancelRef} onClick={onClose}>
Cancel
</Button>
<Button
colorScheme="red"
onClick={() => {
onDelete();
onClose();
}}
ml={3}
>
Delete
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogOverlay>
</AlertDialog>
</>
);
};