X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=resources%2Fjs%2Fcomponents%2Fpages%2FTechniques.js;h=ddecf5603e7752130588f2edad28fe90d9db3ebe;hb=f1c8c3cc53a09d1c261875d2f482b6e19bc48a9a;hp=9334155a92ae284310726e8baa650d7f6e4592e7;hpb=6c51a1601e74bc822ccafe984d525d0e9a35ca28;p=alttp.git
diff --git a/resources/js/components/pages/Techniques.js b/resources/js/components/pages/Techniques.js
index 9334155..ddecf56 100644
--- a/resources/js/components/pages/Techniques.js
+++ b/resources/js/components/pages/Techniques.js
@@ -1,4 +1,5 @@
import axios from 'axios';
+import PropTypes from 'prop-types';
import React from 'react';
import { withTranslation } from 'react-i18next';
@@ -10,36 +11,61 @@ import Overview from '../techniques/Overview';
import { compareTranslation } from '../../helpers/Technique';
import i18n from '../../i18n';
-const Techniques = () => {
+const Techniques = ({ namespace, type }) => {
const [error, setError] = React.useState(null);
+ const [filter, setFilter] = React.useState({});
const [loading, setLoading] = React.useState(true);
const [techniques, setTechniques] = React.useState([]);
+ React.useEffect(() => {
+ const savedFilter = localStorage.getItem(`content.filter.${type}`);
+ if (savedFilter) {
+ setFilter(JSON.parse(savedFilter));
+ } else {
+ setFilter(filter => filter ? {} : filter);
+ }
+ }, [type]);
+
+ const updateFilter = React.useCallback(newFilter => {
+ localStorage.setItem(`content.filter.${type}`, JSON.stringify(newFilter));
+ setFilter(newFilter);
+ }, [type]);
+
React.useEffect(() => {
const ctrl = new AbortController();
- setLoading(true);
- window.document.title = i18n.t('techniques.heading');
+ if (!techniques.length) {
+ setLoading(true);
+ }
+ window.document.title = i18n.t(`${namespace}.heading`);
axios
- .get(`/api/tech`, { signal: ctrl.signal })
+ .get(`/api/content`, {
+ params: {
+ type,
+ ...filter,
+ },
+ signal: ctrl.signal
+ })
.then(response => {
setError(null);
setLoading(false);
setTechniques(response.data.sort(compareTranslation('title', i18n.language)));
})
.catch(error => {
- setError(error);
- setLoading(false);
- setTechniques([]);
+ if (!axios.isCancel(error)) {
+ setError(error);
+ setLoading(false);
+ setTechniques([]);
+ }
});
return () => {
ctrl.abort();
};
- }, []);
+ }, [filter, namespace, type]);
React.useEffect(() => {
- window.document.title = i18n.t('techniques.heading');
+ window.document.title = i18n.t(`${namespace}.heading`);
setTechniques(t => [...t].sort(compareTranslation('title', i18n.language)));
- }, [i18n.language]);
+ }, [namespace, i18n.language]);
if (loading) {
return ;
@@ -54,8 +80,19 @@ const Techniques = () => {
}
return
-
+
;
};
+Techniques.propTypes = {
+ namespace: PropTypes.string,
+ type: PropTypes.string,
+};
+
export default withTranslation()(Techniques);