-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAgendaSection.tsx
107 lines (96 loc) · 2.96 KB
/
AgendaSection.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
import React, { useCallback, useState } from "react";
import type { AgendaStatus } from "@biseo/interface/agenda";
import { AgendaCard } from "@biseo/web/components/molecules";
import { useAgenda } from "@biseo/web/services/agenda";
import {
isOngoingAgenda,
isTerminatedAgenda,
isPreparingAgenda,
} from "@biseo/web/utils/agenda";
import { scroll } from "@biseo/web/styles";
import { css } from "@emotion/react";
const gridLayout = css`
display: grid;
grid-template-columns: 380px 300px;
grid-template-rows: auto 1fr;
grid-gap: 15px 20px;
// Ongoing Agenda
*:nth-child(1) {
grid-column: 1;
grid-row: 1;
}
// Preparing Agenda
*:nth-child(2) {
grid-column: 1;
grid-row: 2;
}
// Terminated Agenda
*:nth-child(3) {
grid-column: 2;
grid-row: 1 / 3;
}
`;
export const AgendaSection: React.FC = () => {
const [showRecentAgendasOnly, setShowRecentAgendasOnly] = useState(
localStorage.getItem("showRecentAgendasOnly") === "true",
);
const { preparingAgendas, ongoingAgendas, terminatedAgendas } = useAgenda(
state => ({
preparingAgendas: state.agendas.filter(isPreparingAgenda),
ongoingAgendas: state.agendas.filter(isOngoingAgenda),
terminatedAgendas: state.agendas.filter(isTerminatedAgenda),
}),
);
const getAgendas = useCallback(
(agendaStatus: AgendaStatus) => {
if (agendaStatus === "preparing") return preparingAgendas;
if (agendaStatus === "ongoing")
return ongoingAgendas.sort((a, b) => a.voters.voted - b.voters.voted);
if (agendaStatus === "terminated") {
const recent24Hours = new Date();
recent24Hours.setDate(new Date().getDate() - 1);
return showRecentAgendasOnly
? terminatedAgendas.filter(
agenda => new Date(agenda.endAt) > recent24Hours,
)
: terminatedAgendas;
}
return [];
},
[preparingAgendas, ongoingAgendas, terminatedAgendas],
);
const getAgendaCards = useCallback(
(agendaStatus: AgendaStatus) => {
const agendas = getAgendas(agendaStatus);
return agendas.map(agenda => (
<AgendaCard key={agenda.id} agenda={agenda} />
));
},
[preparingAgendas, ongoingAgendas, terminatedAgendas],
);
return (
<section css={scroll.y}>
<div css={gridLayout}>
<AgendaCard.Group agendaStatus="ongoing">
{getAgendaCards("ongoing")}
</AgendaCard.Group>
<AgendaCard.Group agendaStatus="preparing">
{getAgendaCards("preparing")}
</AgendaCard.Group>
<AgendaCard.Group
agendaStatus="terminated"
recentOnly={showRecentAgendasOnly}
handleRecentOnly={() => {
setShowRecentAgendasOnly(curr => !curr);
localStorage.setItem(
"showRecentAgendasOnly",
String(!showRecentAgendasOnly),
);
}}
>
{getAgendaCards("terminated")}
</AgendaCard.Group>
</div>
</section>
);
};