-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.tsx
222 lines (196 loc) · 7.92 KB
/
App.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
import { Suspense, useCallback, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useRecoilState, useRecoilValue } from "recoil";
import "./App.scss";
import Smorgasbord from "./components/Smorgasbord/Smorgasbord";
import Flavour from "./interfaces";
import ImportJsonButton from "./components/ImportJsonButton/ImportJsonButton";
import ExportAsJsonButton from "./components/ExportAsJsonButton/ExportAsJsonButton";
import ExportAsImageButton from "./components/ExportAsImageButton/ExportAsImageButton";
import ResetButton from "./components/ResetButton/ResetButton";
import EditButton from "./components/EditButton/EditButton";
import EditModal from "./components/EditModal/EditModal";
import ResetConfirmationModal from "./components/ResetConfirmationModal/ResetConfirmationModal";
import flavoursState from "./states/flavours.atom";
import hierarchicalFlavoursState from "./states/hierarchicalFlavours.selector";
const App = () : JSX.Element => {
const { t, i18n } = useTranslation();
const [flavours, setFlavours] = useRecoilState(flavoursState);
const hierarchicalFlavours = useRecoilValue(hierarchicalFlavoursState);
const [resetConfirmationModalActive, setResetConfirmationModalActive] = useState<boolean>(false);
const [editModalActive, setEditModalActive] = useState<boolean>(false);
const [buttonsFloating, setButtonsFloating] = useState<boolean>(false);
const changeLanguage = (lang) : void => {
i18n.changeLanguage(lang);
};
const fetchDefaultFlavours = () : Promise<Flavour[]> => {
return fetch("flavours.json")
.then((response) => {
return response.json();
})
.then((flavours) => {
return flavours.map(flavour => {
flavour.state = "NO";
return flavour;
});
});
}
useEffect(() => {
let flavours;
if (localStorage) {
flavours = JSON.parse(localStorage.getItem("flavours"));
}
if (flavours) {
setFlavours(flavours);
} else {
fetchDefaultFlavours().then((flavours) => {
setFlavours(flavours);
})
}
}, [ setFlavours ]);
useEffect(() => {
if (!flavours || flavours.length === 0) {
return;
}
if (localStorage) {
localStorage.setItem("flavours", JSON.stringify(flavours));
}
}, [ flavours ])
const handleScroll = useCallback(() => {
let buttonPosition = document.getElementsByClassName("js-button-container-scrolltop")[0].getBoundingClientRect().top;
if (buttonPosition < 30 && !buttonsFloating) {
setButtonsFloating(true);
} else if (buttonPosition >= 30 && buttonsFloating) {
setButtonsFloating(false);
}
}, [ buttonsFloating ]);
useEffect(() => {
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
}
}, [ handleScroll ]);
const resetFlavours = () : void => {
fetchDefaultFlavours().then((flavours) => {
setFlavours(flavours);
});
}
const toggleEditMode = () : void => {
setEditModalActive(!editModalActive);
}
const handleElementClick = (uuid: string) : void => {
// find the target flavour
let targetFlavour = flavours.find(flavour => flavour.uuid === uuid);
let targetHierarchicalFlavour = hierarchicalFlavours.find(hierarchicalFlavour => hierarchicalFlavour.data.uuid === targetFlavour.uuid);
// ignore root click
if (targetHierarchicalFlavour.ancestors().length === 1) {
return;
}
let oldState = targetFlavour.state;
let newState = oldState === "NO" ? "YES"
: oldState === "YES" ? "MAYBE"
: "NO";
setFlavours(
flavours.map((flavour): Flavour => {
if (flavour.uuid === uuid) {
return {
...flavour,
state: newState
}
} else {
let hierarchicalFlavour = hierarchicalFlavours.find(hierarchicalFlavour => hierarchicalFlavour.data.uuid === flavour.uuid);
if (
(
newState === "NO" // 'NO'? Update all children to that
&& hierarchicalFlavour.ancestors().some(ancestor => ancestor.data.uuid === targetFlavour.uuid)
) ||
(
newState === "YES" // 'YES'? Update the parents to that
&& hierarchicalFlavour.descendants().some(child => child.data.uuid === targetFlavour.uuid)
) ||
(
newState === "MAYBE" // 'MAYBE'? Update the children that have 'YES' to that
&& hierarchicalFlavour.ancestors().some(ancestor => ancestor.data.uuid === targetFlavour.uuid && hierarchicalFlavour.data.state === "YES")
)) {
return {
...flavour,
state: newState
}
}
}
return flavour;
})
);
}
// TODO break up this file in separate sub-files;
// bring part of this down to the actual functional app;
// extract out the header, the FAQ and the footer
return (
<Suspense fallback="loading">
<section className="section">
<div className="container content has-text-centered">
<h1 className="title">{t("header.title")}</h1>
<h2 className="subtitle">{t("header.subtitle")}</h2>
<p>
<a href="#what-is-this">{t("faq.whats_this")}</a>
</p>
</div>
</section>
<section className="section">
<div className="container content">
<div className='js-button-container-scrolltop buttons-wrapper'>
<div className={ buttonsFloating
? "buttons has-addons is-centered is-fixed"
: "buttons has-addons is-centered"}>
<ExportAsImageButton></ExportAsImageButton>
<ExportAsJsonButton></ExportAsJsonButton>
<ImportJsonButton></ImportJsonButton>
<EditButton onClick={toggleEditMode}></EditButton>
<ResetButton onClick={() : void => { setResetConfirmationModalActive(true); }}></ResetButton>
</div>
</div>
</div>
</section>
<ResetConfirmationModal
isActive={resetConfirmationModalActive}
onReset={() : void => { resetFlavours(); setResetConfirmationModalActive(false); }}
onCancel={() : void => { setResetConfirmationModalActive(false); }}
></ResetConfirmationModal>
<EditModal
isActive={editModalActive}
onClose={toggleEditMode}></EditModal>
<section className="section">
<div className="container content has-text-centered">
<Smorgasbord
onElementClick={handleElementClick}></Smorgasbord>
</div>
</section>
<section className="section">
<div className="container">
<div className="content has-text-centered">
<h2 id="what-is-this" className="title is-4">{t("faq.whats_this")}</h2>
<p dangerouslySetInnerHTML={{__html: t("faq.whats_this_content")}}></p>
<h2 className="title is-4">{t("faq.how_to_use")}</h2>
<p>{t("faq.how_to_use_content_1")}</p>
<p>{t("faq.how_to_use_content_2")}</p>
<h2 className="title is-4">{t("faq.whats_ra")}</h2>
<p dangerouslySetInnerHTML={{__html: t("faq.whats_ra_content")}}></p>
</div>
</div>
</section>
<footer className="footer">
<div className="content">
<h3>{t("header.title")}</h3>
<p>
{t("footer.languages")}
<button className="button-link" onClick={() : void => changeLanguage("en")}>{t("footer.languages_english")}</button>,
<button className="button-link" onClick={() : void => changeLanguage("es")}>{t("footer.languages_spanish")}</button>,
<button className="button-link" onClick={() : void => changeLanguage("de")}>{t("footer.languages_german")}</button>.
</p>
<p dangerouslySetInnerHTML={{__html: t("footer.disclaimer")}}></p>
</div>
</footer>
</Suspense>
);
}
export default App;