-
Notifications
You must be signed in to change notification settings - Fork 0
/
Home.jsx
458 lines (437 loc) · 13.5 KB
/
Home.jsx
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import React, { useState, useEffect } from "react";
import {
Container,
Row,
Col,
Card,
Modal,
Stack,
Button,
} from "react-bootstrap";
import { Link } from "react-router-dom";
import { MdOutlineVideoLibrary } from "react-icons/md";
import { FaLock } from "react-icons/fa6";
import { MdOutlineSentimentSatisfiedAlt } from "react-icons/md";
import {
BsFillBookFill,
BsTools,
BsFillQuestionCircleFill,
} from "react-icons/bs";
import { AiFillSetting, AiTwotoneReconciliation } from "react-icons/ai";
import { FaBookReader } from "react-icons/fa";
import { useNavigate } from "react-router-dom";
import { get } from "../axios";
import useModal from "../../js/useModal";
import { clearUserSession } from "../../js/userAction";
import styles from "../../styles/pages/HomePage.module.scss";
import { handleConvertTime } from "../../js/dateTimeFormat";
/**
* @param {String} styleOfHeaderBlock 輸入樣式
* @param {String} styleOfHeaderText 輸入樣式
* @param {String} text 輸入文字
* @returns Card.Header
*/
export const CardHeader = ({ styleOfHeaderBlock, styleOfHeaderText, text }) => {
return (
<Card.Header className={styleOfHeaderBlock}>
<h3 className={styleOfHeaderText}>{text}</h3>
</Card.Header>
);
};
/**
*
* @param {String} text 輸入文字
* @returns Card.Body
* @description 用於個人資訊頁面,僅限有帳號的用戶
*/
export const CheckIsClientCardNormalBody = ({ text }) => {
return (
<Card.Body>
<h1 className="text-end py-2">{text}</h1>
</Card.Body>
);
};
/**
* @param {String} text 輸入文字
* @param {String} dirLink 輸入連結
* @returns Card.Body
* @description 用於個人資訊頁面,僅限有帳號的用戶
*/
export const CheckIsClientCardLinkBody = ({ text, dirLink }) => {
return (
<Card.Body>
<h1 className="text-end py-2">
{text}
<Link to={dirLink} className={styles.linkCountinuousWatch}>
繼續觀看
</Link>
</h1>
</Card.Body>
);
};
/**
* @returns Card.Body
* @description 用於個人資訊頁面,訪客顯示用
*/
export const CheckIsGuestCardBody = () => {
return (
<Card.Body className={styles.checkIsGuestCardBodyBlock}>
<h3 className="text-center py-2">
<FaLock />
<br />
權限不足,無法查看
</h3>
</Card.Body>
);
};
export default function Home({ user }) {
const navigate = useNavigate();
const permission = user.permission;
const checkIsClient = permission === "ylhClient" ? true : false;
const mdSize = permission === "ylhClient" ? 4 : 3;
const smSize = permission === "ylhClient" ? 4 : 6;
const xsSize = permission === "ylhClient" ? 4 : 4;
const [
showChoseVideoModal,
handleCloseChoseVideoModal,
handleShowChoseVideoModal,
] = useModal();
const [
showChoseRecordModal,
handleCloseChoseRecordModal,
handleShowChoseRecordModal,
] = useModal();
const [
showLearnStatusModal,
handleCloseLearnStatusModal,
handleShowLearnStatusModal,
] = useModal();
const [loading, setLoading] = useState(checkIsClient ? true : false);
const [error, setError] = useState(null);
const [usrInfo, setUsrInfo] = useState();
useEffect(() => {
if (permission === "ylhClient") {
const token = user.client_token;
getUserRecord({
api: `client/web/record/${token}`,
});
}
}, []);
const getUserRecord = async ({ api }) => {
try {
const res = await get(api);
setUsrInfo(res.data);
setTimeout(() => {
setLoading(false);
}, 2000);
} catch (err) {
if (err.response) {
const { status, data } = err.response;
if (status === 401 && data.message === "請求錯誤") {
handleSessionTimeout();
} else {
setError(data.message);
setLoading(false);
}
} else {
// 處理非 API 回應的錯誤
// ...
}
}
};
const handleSessionTimeout = () => {
alert("登入逾時,請重新登入");
clearUserSession();
navigate("/");
};
useEffect(() => {
if (user === null) {
clearUserSession();
navigate("/");
}
}, []);
return (
<>
<Container className="mt-3">
{/* {checkIsClient && (
<Row>
<h3 className="text-primary">個人資訊</h3>
</Row>
)}
{loading === false && checkIsClient ? (
<Row>
<Col sm={6} md={6} lg={4}>
<Card border="primary" className="mb-2">
<CardHeader
styleOfHeaderBlock={styles.cardHeaderOfWatchTimesColor}
styleOfHeaderText={styles.cardHeaderText}
text="瀏覽影片次數"
/>
<CheckIsClientCardNormalBody
text={`${usrInfo.TotalWatchCount}次`}
/>
</Card>
</Col>
<Col sm={6} md={6} lg={4}>
<Card border="secondary" className="mb-2">
<CardHeader
styleOfHeaderBlock={styles.cardHeaderOfWatchDurationColor}
styleOfHeaderText={styles.cardHeaderText}
text="瀏覽影片時間"
/>
<CheckIsClientCardNormalBody
text={handleConvertTime(usrInfo.TotalWatchTime)}
/>
</Card>
</Col>
<Col sm={7} md={6} lg={4}>
<Card border="danger" className="mb-2">
<CardHeader
styleOfHeaderBlock={styles.cardHeaderOfFinishColor}
styleOfHeaderText={styles.cardHeaderText}
text="完成基礎練習"
/>
<CheckIsClientCardLinkBody
text={`${usrInfo.TotalFinishBasicVideo}部`}
dirLink={"/basic"}
/>
</Card>
</Col>
<Col sm={7} md={6} lg={4}>
<Card border="warning" className="mb-2">
<CardHeader
styleOfHeaderBlock={styles.cardHeaderOfFinishPraticeColor}
styleOfHeaderText={styles.cardHeaderText}
text="完成練習"
/>
<CheckIsClientCardLinkBody
text={`${usrInfo.TotalFinishPraticeVideo}部`}
dirLink={"/pratice"}
/>
</Card>
</Col>
<Col sm={7} md={6} lg={4}>
<Card border="success" className="mb-2">
<CardHeader
styleOfHeaderBlock={styles.cardHeaderOfFinishTestColor}
styleOfHeaderText={styles.cardHeaderText}
text="完成測驗"
/>
<CheckIsClientCardLinkBody
text={`${usrInfo.TotalFinishTestVideo}部`}
dirLink={"/test"}
/>
</Card>
</Col>
</Row>
) : (
checkIsClient && (
<Card.Body>
<Card.Text>用戶資訊載入中</Card.Text>
</Card.Body>
)
)} */}
</Container>
<Container className="pb-4 my-4">
<Card className={`py-4 ${styles.container_CardContainer}`}>
<Row>
<Col
md={mdSize}
sm={smSize}
xs={xsSize}
className="text-center my-3"
>
<Link
type="button"
className={styles.videoContainer}
onClick={handleShowChoseVideoModal}
>
<Row className="mx-auto my-auto">
<MdOutlineVideoLibrary className="fs-1" />
<p className="text-center fs-5 my-auto">衛教資訊</p>
</Row>
</Link>
</Col>
<Col
md={mdSize}
sm={smSize}
xs={xsSize}
className="text-center my-3"
>
<Link
to={"https://www.ylh.gov.tw/?aid=612"}
className={styles.infoContainer}
>
<Row>
<BsFillBookFill className="fs-1" />
<p className="text-center fs-5 my-auto">衛教天地</p>
</Row>
</Link>
</Col>
{permission === "ylhClient" && (
<Col
md={mdSize}
sm={smSize}
xs={xsSize}
className="text-center my-3"
>
<Link
type="button"
className={styles.recordContainer}
onClick={handleShowChoseRecordModal}
>
<Row>
<AiTwotoneReconciliation className="fs-1" />
<p className="text-center fs-5 my-auto">練習紀錄</p>
</Row>
</Link>
</Col>
)}
<Col
md={mdSize}
sm={smSize}
xs={xsSize}
className="text-center my-3"
>
<Link to="/usingTip" className={styles.tutorialContainer}>
<Row>
<BsTools className="fs-1" />
<p className="text-center fs-5 my-auto">使用教學</p>
</Row>
</Link>
</Col>
<Col
md={mdSize}
sm={smSize}
xs={xsSize}
className="text-center my-3"
>
<Link to="/comment" className={styles.suggestionContainer}>
<Row>
<MdOutlineSentimentSatisfiedAlt className="fs-1" />
<p className="text-center fs-5 my-auto">滿意度調查</p>
</Row>
</Link>
</Col>
{permission === "ylhClient" && (
<Col
md={mdSize}
sm={smSize}
xs={xsSize}
className="text-center my-3"
>
<Link to="/setting" className={styles.settingContainer}>
<Row>
<AiFillSetting className="fs-1" />
<p className="text-center fs-5">設定</p>
</Row>
</Link>
</Col>
)}
</Row>
{permission === "ylhClient" && (
<Button
variant="outline-primary"
className={styles.learnStatusContainer}
onClick={handleShowLearnStatusModal}
>
<p className="text-center fs-3 m-0">
<FaBookReader />
學習狀態
</p>
</Button>
)}
</Card>
</Container>
{/* 選擇衛教影音類型:基礎/練習/測驗 */}
<Modal show={showChoseVideoModal} onHide={handleCloseChoseVideoModal}>
<Modal.Header closeButton>
<Modal.Title>請選擇欲觀看類型</Modal.Title>
</Modal.Header>
<Modal.Body>
<Stack gap={1}>
{/* 基礎練習用 */}
<Link
to={{
pathname: "/basic",
}}
className={styles.linkContainer_link}
>
<h3 className="mt-1 mb-1">基本練習</h3>
</Link>
{/* 練習用 */}
<Link
to={{
pathname: "/pratice",
}}
className={styles.linkContainer_link}
>
<h3 className="mt-1 mb-1">練習</h3>
</Link>
{/* 測驗用 */}
<Link
to={{
pathname: "/test",
}}
className={styles.linkContainer_link}
>
<h3 className="mt-1 mb-1">測驗</h3>
</Link>
</Stack>
</Modal.Body>
</Modal>
{/* 練習紀錄Modal */}
<Modal show={showChoseRecordModal} onHide={handleCloseChoseRecordModal}>
<Modal.Header closeButton>
<Modal.Title>請選擇欲觀看類型</Modal.Title>
</Modal.Header>
<Modal.Body>
<div className={`d-flex flex-column justify-content-center`}>
<Link
to={{
pathname: "/record/pratice",
}}
className={styles.linkContainer_link}
>
<h3 className="mt-1 mb-1">練習用衛教紀錄</h3>
</Link>
<Link
to={{
pathname: "/record/test",
}}
className={styles.linkContainer_link}
>
<h3 className="mt-1 mb-1">測驗用衛教紀錄</h3>
</Link>
</div>
</Modal.Body>
</Modal>
{/* 學習狀態Modal */}
<Modal show={showLearnStatusModal} onHide={handleCloseLearnStatusModal}>
<Modal.Header closeButton>
<Modal.Title>學習狀態</Modal.Title>
</Modal.Header>
<Modal.Body>
{loading === false && checkIsClient ? (
<Container>
<Row>
<Col>
<h5>瀏覽影片次數</h5>
<p>{usrInfo.TotalWatchCount}次</p>
</Col>
</Row>
<Row>
<Col>
<h5>瀏覽影片時間</h5>
<p>{handleConvertTime(usrInfo.TotalWatchTime)}</p>
</Col>
</Row>
</Container>
) : (
<p>用戶資訊載入中</p>
)}
</Modal.Body>
</Modal>
</>
);
}