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