]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/pages/Techniques.js
expand tech
[alttp.git] / resources / js / components / pages / Techniques.js
1 import axios from 'axios';
2 import React from 'react';
3 import { withTranslation } from 'react-i18next';
4
5 import NotFound from './NotFound';
6 import ErrorBoundary from '../common/ErrorBoundary';
7 import ErrorMessage from '../common/ErrorMessage';
8 import Loading from '../common/Loading';
9 import Overview from '../techniques/Overview';
10 import { compareTranslation } from '../../helpers/Technique';
11 import i18n from '../../i18n';
12
13 const Techniques = () => {
14         const [error, setError] = React.useState(null);
15         const [loading, setLoading] = React.useState(true);
16         const [techniques, setTechniques] = React.useState([]);
17
18         React.useEffect(() => {
19                 const ctrl = new AbortController();
20                 setLoading(true);
21                 window.document.title = i18n.t('techniques.heading');
22                 axios
23                         .get(`/api/tech`, {
24                                 params: {
25                                         type: 'tech',
26                                 },
27                                 signal: ctrl.signal
28                         })
29                         .then(response => {
30                                 setError(null);
31                                 setLoading(false);
32                                 setTechniques(response.data.sort(compareTranslation('title', i18n.language)));
33                         })
34                         .catch(error => {
35                                 setError(error);
36                                 setLoading(false);
37                                 setTechniques([]);
38                         });
39                 return () => {
40                         ctrl.abort();
41                 };
42         }, []);
43
44         React.useEffect(() => {
45                 window.document.title = i18n.t('techniques.heading');
46                 setTechniques(t => [...t].sort(compareTranslation('title', i18n.language)));
47         }, [i18n.language]);
48
49         if (loading) {
50                 return <Loading />;
51         }
52
53         if (error) {
54                 return <ErrorMessage error={error} />;
55         }
56
57         if (!techniques || !techniques.length) {
58                 return <NotFound />;
59         }
60
61         return <ErrorBoundary>
62                 <Overview techniques={techniques} />
63         </ErrorBoundary>;
64 };
65
66 export default withTranslation()(Techniques);