]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/pages/Techniques.js
add tech index
[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`, { signal: ctrl.signal })
24                         .then(response => {
25                                 setError(null);
26                                 setLoading(false);
27                                 setTechniques(response.data.sort(compareTranslation('title', i18n.language)));
28                         })
29                         .catch(error => {
30                                 setError(error);
31                                 setLoading(false);
32                                 setTechniques([]);
33                         });
34                 return () => {
35                         ctrl.abort();
36                 };
37         }, []);
38
39         React.useEffect(() => {
40                 window.document.title = i18n.t('techniques.heading');
41                 setTechniques(t => [...t].sort(compareTranslation('title', i18n.language)));
42         }, [i18n.language]);
43
44         if (loading) {
45                 return <Loading />;
46         }
47
48         if (error) {
49                 return <ErrorMessage error={error} />;
50         }
51
52         if (!techniques || !techniques.length) {
53                 return <NotFound />;
54         }
55
56         return <ErrorBoundary>
57                 <Overview techniques={techniques} />
58         </ErrorBoundary>;
59 };
60
61 export default withTranslation()(Techniques);