|
1 | | -import React from 'react' |
| 1 | +import React, { useState } from 'react' |
2 | 2 | import type { NextPage, GetServerSideProps } from 'next' |
3 | 3 | import { useTranslation } from 'react-i18next' |
4 | 4 | import { PageName } from '../../data/enums/PageName' |
5 | 5 | import PageTitle from '../../components/core/PageTitle' |
6 | 6 | import { PageProps } from '../../interfaces/PageProps' |
| 7 | +import { getTalkList } from '../api/program' |
| 8 | +import { GetServerSidePropsContext } from 'next' |
| 9 | +import { ITalkItem, ITalkList, ITalkTableList } from '../../interfaces/IProgram' |
| 10 | +import { compareAsc, isSameDay } from 'date-fns' |
| 11 | +import TalkTableToggleButton from '../../components/service/Program/TalkTableToggleButton' |
| 12 | +import TalkTable from '../../components/service/Program/TalkTable' |
7 | 13 |
|
8 | | -const TalkSchedule: NextPage = (props: PageProps) => { |
| 14 | +interface TalkTableProps extends PageProps { |
| 15 | + data: ITalkList |
| 16 | +} |
| 17 | + |
| 18 | +const TalkSchedule: NextPage = (props: TalkTableProps) => { |
9 | 19 | const { t } = useTranslation() |
| 20 | + const { pageName, data } = props |
| 21 | + const [selectedDay, setSelectedDay] = useState<string>('day1') |
| 22 | + |
| 23 | + const updateSelectedDay = (day: string) => { |
| 24 | + setSelectedDay(day) |
| 25 | + } |
| 26 | + |
| 27 | + const groupByProperty = ( |
| 28 | + array: ITalkItem[], |
| 29 | + property: string |
| 30 | + ): ITalkTableList[] => { |
| 31 | + const groupByValue: { [key: string]: ITalkItem[] } = array.reduce( |
| 32 | + (obj, item) => { |
| 33 | + obj[item[property]] = obj[item[property]] || [] |
| 34 | + obj[item[property]].push(item) |
| 35 | + return obj |
| 36 | + }, |
| 37 | + {} |
| 38 | + ) |
| 39 | + |
| 40 | + return Object.keys(groupByValue).map((key: string) => ({ |
| 41 | + [property]: key, |
| 42 | + talkList: groupByValue[key] |
| 43 | + })) |
| 44 | + } |
| 45 | + |
| 46 | + const tableData: ITalkItem[] = data.list.sort((a, b) => |
| 47 | + compareAsc(new Date(a.video_open_at), new Date(b.video_open_at)) |
| 48 | + ) |
| 49 | + |
| 50 | + const day1tableList: ITalkItem[] = tableData.filter((item) => |
| 51 | + isSameDay(new Date(item.video_open_at), new Date(2022, 9, 1)) |
| 52 | + ) |
| 53 | + const day2tableList: ITalkItem[] = tableData.filter((item) => |
| 54 | + isSameDay(new Date(item.video_open_at), new Date(2022, 9, 2)) |
| 55 | + ) |
10 | 56 |
|
11 | 57 | return ( |
12 | 58 | <div> |
13 | | - <PageTitle title={props.pageName} /> |
14 | | - {t('label:preparing')} |
| 59 | + <PageTitle title={pageName} /> |
| 60 | + <TalkTableToggleButton handleClick={updateSelectedDay} /> |
| 61 | + {selectedDay === 'day1' ? ( |
| 62 | + <TalkTable |
| 63 | + day="day1" |
| 64 | + headers={['트랙1', '트랙2']} |
| 65 | + list={groupByProperty(day1tableList, 'video_open_at')} |
| 66 | + /> |
| 67 | + ) : ( |
| 68 | + <TalkTable |
| 69 | + day="day2" |
| 70 | + headers={['트랙3', '트랙4']} |
| 71 | + list={groupByProperty(day2tableList, 'video_open_at')} |
| 72 | + /> |
| 73 | + )} |
15 | 74 | </div> |
16 | 75 | ) |
17 | 76 | } |
18 | 77 |
|
19 | | -export const getServerSideProps: GetServerSideProps = async () => { |
20 | | - return { |
21 | | - props: { |
22 | | - title: PageName.TalkSchedule |
| 78 | +export const getServerSideProps: GetServerSideProps = async ( |
| 79 | + context: GetServerSidePropsContext |
| 80 | +) => { |
| 81 | + const { locale } = context |
| 82 | + try { |
| 83 | + const data: ITalkList = await getTalkList() |
| 84 | + |
| 85 | + return { |
| 86 | + props: { |
| 87 | + title: PageName.TalkSchedule, |
| 88 | + locale, |
| 89 | + data |
| 90 | + } |
| 91 | + } |
| 92 | + } catch (error) { |
| 93 | + // TODO: Add error interface |
| 94 | + if (error.notFound) { |
| 95 | + return { |
| 96 | + notFound: true |
| 97 | + } |
23 | 98 | } |
24 | 99 | } |
25 | 100 | } |
|
0 commit comments