]> git.localhorst.tv Git - alttp.git/commitdiff
add tech index
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 2 Jan 2023 17:38:36 +0000 (18:38 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 2 Jan 2023 17:38:36 +0000 (18:38 +0100)
app/Http/Controllers/SitemapXmlController.php
app/Http/Controllers/TechniqueController.php
resources/js/components/App.js
resources/js/components/pages/Techniques.js [new file with mode: 0644]
resources/js/components/techniques/List.js [new file with mode: 0644]
resources/js/components/techniques/Overview.js [new file with mode: 0644]
resources/js/helpers/Technique.js
resources/js/i18n/de.js
resources/js/i18n/en.js
resources/sass/techniques.scss
routes/api.php

index ebb7b621a15c64230504bcdb70d4f748b1071dd5..1bd7749589507c3b5aad322bb2b914848a3cc097 100644 (file)
@@ -22,11 +22,18 @@ class SitemapXmlController extends Controller
                        $urls[] = $url;
                }
 
+               $url = new SitemapUrl();
+               $url->path = '/tech';
+               $url->lastmod = Technique::latest()->first()->created_at;
+               $url->changefreq = 'monthly';
+               $url->priority = 0.5;
+               $urls[] = $url;
+
                foreach (Technique::where('index', true)->get() as $tech) {
                        $url = new SitemapUrl();
                        $url->path = '/tech/'.rawurlencode($tech->name);
                        $url->lastmod = $tech->updated_at ? $tech->updated_at : ($tech->created_at ? $tech->created_at : now());
-                       $url->changefreq = 'monthly';
+                       $url->changefreq = 'never';
                        $url->priority = $tech->priority;
                        $urls[] = $url;
                }
index cce39cea34f5e628218393d6d5ab04a01b853490..a1ba20e59ac214877b46d815bcd6084d150a7df0 100644 (file)
@@ -3,11 +3,30 @@
 namespace App\Http\Controllers;
 
 use App\Models\Technique;
+use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Http\Request;
 
 class TechniqueController extends Controller
 {
 
+       public function search(Request $request) {
+               $validatedData = $request->validate([
+                       'phrase' => 'string|nullable',
+               ]);
+
+               $techs = Technique::where('index', '=', 1);
+
+               if (!empty($validatedData['phrase'])) {
+                       $search = $validatedData['phrase'];
+                       $techs = $techs->where(function (Builder $query) use ($search) {
+                               $query->where('title', 'LIKE', '%'.$search.'%')
+                               ->orWhere('short', 'LIKE', '%'.$search.'%');
+                       });
+               }
+
+               return $techs->get()->toJson();
+       }
+
        public function single(Request $request, Technique $tech) {
                $this->authorize('view', $tech);
                $tech->load('chapters');
index 0d11f902641a365d526fe4ca71321f775e7f9bba..6d4030357082ad7e4c985c62349ea06b8519f629 100644 (file)
@@ -6,6 +6,7 @@ import Header from './common/Header';
 import AlttpSeed from './pages/AlttpSeed';
 import Front from './pages/Front';
 import Technique from './pages/Technique';
+import Techniques from './pages/Techniques';
 import Tournament from './pages/Tournament';
 import User from './pages/User';
 import AlttpBaseRomProvider from '../helpers/AlttpBaseRomContext';
@@ -57,6 +58,7 @@ const App = () => {
                                <Header doLogout={doLogout} />
                                <Routes>
                                        <Route path="h/:hash" element={<AlttpSeed />} />
+                                       <Route path="tech" element={<Techniques />} />
                                        <Route path="tech/:name" element={<Technique />} />
                                        <Route path="tournaments/:id" element={<Tournament />} />
                                        <Route path="users/:id" element={<User />} />
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);
diff --git a/resources/js/components/techniques/List.js b/resources/js/components/techniques/List.js
new file mode 100644 (file)
index 0000000..0549cd3
--- /dev/null
@@ -0,0 +1,28 @@
+import PropTypes from 'prop-types';
+import React from 'react';
+import { Link } from 'react-router-dom';
+
+import { getTranslation } from '../../helpers/Technique';
+import i18n from '../../i18n';
+
+const List = ({ techniques }) => <ul className="tech-list">
+       {techniques.map(tech =>
+               <li key={tech.id}>
+                       <h2>
+                               <Link to={`/tech/${tech.name}`}>
+                                       {getTranslation(tech, 'title', i18n.language)}
+                               </Link>
+                       </h2>
+                       <p>{getTranslation(tech, 'short', i18n.language)}</p>
+               </li>
+       )}
+</ul>;
+
+List.propTypes = {
+       techniques: PropTypes.arrayOf(PropTypes.shape({
+               id: PropTypes.number,
+               name: PropTypes.string,
+       })),
+};
+
+export default List;
diff --git a/resources/js/components/techniques/Overview.js b/resources/js/components/techniques/Overview.js
new file mode 100644 (file)
index 0000000..8c7cd4d
--- /dev/null
@@ -0,0 +1,19 @@
+import PropTypes from 'prop-types';
+import React from 'react';
+import { Container } from 'react-bootstrap';
+import { withTranslation } from 'react-i18next';
+
+import List from './List';
+import i18n from '../../i18n';
+
+const Overview = ({ techniques }) => <Container>
+       <h1>{i18n.t('techniques.heading')}</h1>
+       <List techniques={techniques} />
+</Container>;
+
+Overview.propTypes = {
+       techniques: PropTypes.arrayOf(PropTypes.shape({
+       })),
+};
+
+export default withTranslation()(Overview);
index ea60cc71653d91c50887050af7843e671dfe2647..7ec22d3b90e01df1aa9b3918643d37f9200829ff 100644 (file)
@@ -10,6 +10,9 @@ export const getTranslation = (tech, prop, lang) => {
        return tech[prop];
 };
 
+export const compareTranslation = (prop, lang) => (a, b) =>
+       getTranslation(a, prop, lang).localeCompare(getTranslation(b, prop, lang));
+
 export default {
        getTranslation,
 };
index f794876a041dd3bad29cb9011c2f00a16c570add..c8be7aed4cebac29fc7e4e7b3eead7dff716d5eb 100644 (file)
@@ -460,6 +460,9 @@ export default {
                        unlockError: 'Fehler beim Entsperren',
                        unlockSuccess: 'Runde entsperrt',
                },
+               techniques: {
+                       heading: 'Techniken',
+               },
                tournaments: {
                        admins: 'Organisation',
                        applicationDenied: 'Antrag wurde abgelehnt',
index dbe37f15765215db510f0e7adc55429860072a42..62e486b8d49500e108da695c7acd3fadd3b03ee2 100644 (file)
@@ -460,6 +460,9 @@ export default {
                        unlockError: 'Error unlocking round',
                        unlockSuccess: 'Round unlocked',
                },
+               techniques: {
+                       heading: 'Techniques',
+               },
                tournaments: {
                        admins: 'Admins',
                        applicationDenied: 'Application denied',
index c4fa505032ceb4333c7391b85b3b482cbac4f272..19dcf417be16f743721df2aa4624f7e3335efd31 100644 (file)
@@ -1,3 +1,19 @@
+.tech-list {
+       margin: 1em 0;
+       padding: 0;
+       list-style: none;
+
+       li {
+               margin: 1ex 0;
+               padding: 1ex;
+               border-top: thin solid silver;
+       }
+
+       h2 > a {
+               text-decoration: none;
+       }
+}
+
 .tech-outline {
        float: right;
 }
index 3d7062adbe6661e7ee3aaad9ab232697a04c9be5..0f7aa3e3febc80d330b6f6c948d0ff35a00e375e 100644 (file)
@@ -43,6 +43,7 @@ Route::post('rounds/{round}/lock', 'App\Http\Controllers\RoundController@lock');
 Route::post('rounds/{round}/setSeed', 'App\Http\Controllers\RoundController@setSeed');
 Route::post('rounds/{round}/unlock', 'App\Http\Controllers\RoundController@unlock');
 
+Route::get('tech', 'App\Http\Controllers\TechniqueController@search');
 Route::get('tech/{tech:name}', 'App\Http\Controllers\TechniqueController@single');
 
 Route::get('tournaments/{id}', 'App\Http\Controllers\TournamentController@single');