]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/pages/Technique.js
tech data model
[alttp.git] / resources / js / components / pages / Technique.js
diff --git a/resources/js/components/pages/Technique.js b/resources/js/components/pages/Technique.js
new file mode 100644 (file)
index 0000000..19b56b7
--- /dev/null
@@ -0,0 +1,57 @@
+import axios from 'axios';
+import React, { useEffect, useState } from 'react';
+import { useParams } from 'react-router-dom';
+
+import ErrorBoundary from '../common/ErrorBoundary';
+import ErrorMessage from '../common/ErrorMessage';
+import Loading from '../common/Loading';
+import NotFound from '../pages/NotFound';
+import Detail from '../techniques/Detail';
+
+const Technique = () => {
+       const params = useParams();
+       const { name } = params;
+
+       const [error, setError] = useState(null);
+       const [loading, setLoading] = useState(true);
+       const [technique, setTechnique] = useState(null);
+
+       useEffect(() => {
+               const ctrl = new AbortController();
+               setLoading(true);
+               axios
+                       .get(`/api/tech/${name}`, { signal: ctrl.signal })
+                       .then(response => {
+                               setError(null);
+                               setLoading(false);
+                               setTechnique(response.data);
+                               window.document.title = response.data.title;
+                       })
+                       .catch(error => {
+                               setError(error);
+                               setLoading(false);
+                               setTechnique(null);
+                       });
+               return () => {
+                       ctrl.abort();
+               };
+       }, [name]);
+
+       if (loading) {
+               return <Loading />;
+       }
+
+       if (error) {
+               return <ErrorMessage error={error} />;
+       }
+
+       if (!technique) {
+               return <NotFound />;
+       }
+
+       return <ErrorBoundary>
+               <Detail technique={technique} />
+       </ErrorBoundary>;
+};
+
+export default Technique;