-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.tsx
237 lines (206 loc) · 5.66 KB
/
list.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import React from "react";
import Head from "next/head";
import styled from "styled-components";
import Footer from "../../../../components/Footer";
import Image from "next/image";
import Link from "next/link";
import { News } from "../../../../models/news";
import "firebase/firestore";
import { useRouter } from "next/router";
import { DeleteNewsById } from "../../../../firestore/news/deleteNewsById";
import { useState } from "react";
import { NewsPagination } from "../../../../firestore/news/newsPagination";
export default function NewsListTemplate({ newsList }: { newsList: News[] }) {
const router = useRouter();
var [newsListResult, setNewsListResult] = useState(newsList);
const [end, setEnd] = useState(false);
const [start, setStart] = useState(true);
const nextPage = async () => {
setEnd(false);
const newNewsList = await NewsPagination("next", newsListResult);
setNewsListResult((newsListResult = newNewsList));
if (newsListResult.length < 2) {
setEnd(true);
}
setStart(false);
};
const prevPage = async () => {
setEnd(false);
setStart(false);
const newNewsList = await NewsPagination("prev", newsListResult);
// newsL = newNewsList;
setNewsListResult((newsListResult = newNewsList));
if (newsListResult.length < 2) {
setEnd(true);
}
if (arraysMatch(newsListResult, newsList)) {
setStart(true);
}
};
var arraysMatch = function (arr1: News[] = [], arr2: News[] = []) {
// Check if the arrays are the same length
if (arr1.length !== arr2.length) return false;
// Check if all items exist and are in the same order
for (var i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) return false;
}
// Otherwise, return true
return true;
};
async function handleDelete(id: string) {
// e.preventDefault();
await DeleteNewsById(id);
alert("Document successfully deleted!" + id);
router.replace("/admin/news/list");
}
return (
<>
<Head>
<Title>News List</Title>
</Head>
<Heading>News List</Heading>
<Container>
<TotalCount>
<B>Total: 20 news</B>
<Link href={`/admin/news/form`} passHref>
<AddNewsButton>Add new</AddNewsButton>
</Link>
</TotalCount>
<StyledTable>
<THead>
<TR>
<TH>Photo</TH>
<TH>Title</TH>
<TH>Descrption</TH>
<TH>Edit</TH>
<TH>Delete</TH>
</TR>
</THead>
<TBody>
{newsListResult.map((item, index) => (
<TR key={item.id}>
<TD>
{item.imagePath && (
<Image
src={item.imagePath || ''}
alt="Picture of the product"
width={80}
height={80}
/>
)}
</TD>
<TD>{item.title}</TD>
<TD>{item.description}</TD>
<TD>
<Link href={`/admin/news/form/${item.id}`} passHref>
<Button>Edit</Button>
</Link>
</TD>
<TD>
{/* <Link href="#" passHref> */}
{/* <Button onClick={handleDelete(`${item.id}`)}>Delete</Button> */}
<Button onClick={() => handleDelete(`${item.id}`)}>
Delete
</Button>
{/* </Link> */}
</TD>
</TR>
))}
</TBody>
</StyledTable>
<Pagination>
{start == false ? (
<PaginationStep onClick={prevPage}>« Prev</PaginationStep>
) : (
""
)}
{/* <PaginationStep>1</PaginationStep>
<PaginationStep>2</PaginationStep>
<PaginationStep>3</PaginationStep>
<PaginationStep>4</PaginationStep> */}
{end == false ? (
<PaginationStep onClick={nextPage}>Next »</PaginationStep>
) : (
""
)}
</Pagination>
<br />
<Link href={`/admin/top`} passHref>
<Button>Admin</Button>
</Link>
<br />
</Container>
<Footer />
</>
);
}
const Heading = styled.h1`
font-size: 2em;
text-align: center;
color: palevioletred;
`;
const Title = styled.title``;
const Container = styled.div`
text-align: center;
padding: 0 0.5rem;
align-items: center;
width: 80%;
display: block;
margin-left: auto;
margin-right: auto;
`;
const TotalCount = styled.p`
text-align: left;
color: red;
`;
const StyledTable = styled.table`
// custom css goes here
width: 100%;
border: 1px solid black;
border-collapse: collapse;
`;
const THead = styled.thead`
// custom css goes here
`;
const TFoot = styled.tfoot`
// custom css goes here
`;
const TBody = styled.tbody`
// custom css goes here
`;
const TR = styled.tr`
// custom css goes here
`;
const TH = styled.th`
// custom css goes here
border: 1px solid black;
border-collapse: collapse;
`;
const TD = styled.td`
// custom css goes here
border: 1px solid black;
border-collapse: collapse;
`;
const Pagination = styled.div`
display: inline-block;
`;
const PaginationStep = styled.a`
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
`;
const Button = styled.button`
background: white;
color: palevioletred;
font-size: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
cursor: pointer;
`;
const AddNewsButton = styled(Button)`
position: absolute;
right: 200px;
`;
const B = styled.b``;