]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/pages/Techniques.js
add tech index
[alttp.git] / resources / js / components / pages / Techniques.js
diff --git a/resources/js/components/pages/Techniques.js b/resources/js/components/pages/Techniques.js
new file mode 100644 (file)
index 0000000..9334155
--- /dev/null
@@ -0,0 +1,61 @@
+import axios from 'axios';
+import React from 'react';
+import { withTranslation } from 'react-i18next';
+
+import NotFound from './NotFound';
+import ErrorBoundary from '../common/ErrorBoundary';
+import ErrorMessage from '../common/ErrorMessage';
+import Loading from '../common/Loading';
+import Overview from '../techniques/Overview';
+import { compareTranslation } from '../../helpers/Technique';
+import i18n from '../../i18n';
+
+const Techniques = () => {
+       const [error, setError] = React.useState(null);
+       const [loading, setLoading] = React.useState(true);
+       const [techniques, setTechniques] = React.useState([]);
+
+       React.useEffect(() => {
+               const ctrl = new AbortController();
+               setLoading(true);
+               window.document.title = i18n.t('techniques.heading');
+               axios
+                       .get(`/api/tech`, { signal: ctrl.signal })
+                       .then(response => {
+                               setError(null);
+                               setLoading(false);
+                               setTechniques(response.data.sort(compareTranslation('title', i18n.language)));
+                       })
+                       .catch(error => {
+                               setError(error);
+                               setLoading(false);
+                               setTechniques([]);
+                       });
+               return () => {
+                       ctrl.abort();
+               };
+       }, []);
+
+       React.useEffect(() => {
+               window.document.title = i18n.t('techniques.heading');
+               setTechniques(t => [...t].sort(compareTranslation('title', i18n.language)));
+       }, [i18n.language]);
+
+       if (loading) {
+               return <Loading />;
+       }
+
+       if (error) {
+               return <ErrorMessage error={error} />;
+       }
+
+       if (!techniques || !techniques.length) {
+               return <NotFound />;
+       }
+
+       return <ErrorBoundary>
+               <Overview techniques={techniques} />
+       </ErrorBoundary>;
+};
+
+export default withTranslation()(Techniques);