-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
92 lines (83 loc) · 2.36 KB
/
index.tsx
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
import { Pagination } from '@mui/material';
import { lazy, useState, useEffect, useRef } from 'react';
import VideoItem from 'src/components/main/videoItem';
import { useAlert } from 'src/components/user/use-common';
import { useMain } from 'src/hooks/use-main';
import { VideoList } from 'src/typings/common';
const Modal = lazy(() => import('src/components/common/modal'));
export default function MainPage() {
const [data, setData] = useState<VideoList>({
videos: [],
nowPage: 1,
size: 10,
totalPage: 1,
totalCount: 0,
});
const [currentPage, setCurrentPage] = useState(1);
const videoId = useRef(-1);
const { videoList, deleteVideo } = useMain();
const { onShowAlert } = useAlert();
useEffect(() => {
fetchVideoList();
}, [currentPage]);
const fetchVideoList = async () => {
const { data, error } = await videoList(currentPage);
if (error) {
alert('에러가 발생했습니다.');
return;
}
if (data) {
setData(data);
setCurrentPage(data.nowPage);
}
};
/* modal */
const [open, setOpen] = useState(false);
const onDelete = (id: number) => {
videoId.current = id;
setOpen(true);
};
const onOk = async () => {
const error = await deleteVideo(videoId.current);
if (error) {
onShowAlert(error.data.message, 'error');
return;
}
onShowAlert('영상을 삭제했습니다', 'success');
await fetchVideoList();
onClose();
};
const onClose = () => {
setOpen(false);
};
/* pagination */
const handleChangePage = (newPage: number) => setCurrentPage(newPage);
return (
<>
<h1 className='px-6 pt-6 font-bold'>메인 콘텐츠</h1>
<div className={`grid grid-cols-5 gap-5 px-6 py-8`}>
{data.videos
.filter(v => v.videoAdminState !== 'deleted')
.map(v => {
return (
<div key={v.videoId}>
<VideoItem video={v} onDelete={onDelete} />
</div>
);
})}
</div>
<div className='flex justify-end px-10 pb-10'>
<Pagination
count={data.totalPage}
page={currentPage}
onChange={(_, p) => handleChangePage(p)}
/>
</div>
<Modal
{...{ onOk, onClose, open }}
title='영상 삭제'
content='정말 영상을 삭제하시겠습니까?'
/>
</>
);
}