From: Daniel Karbach Date: Thu, 5 Jan 2023 19:41:27 +0000 (+0100) Subject: tech relations X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=0586e04204885088f31ac9861446eb0759cc8d2f;p=alttp.git tech relations --- diff --git a/app/Http/Controllers/TechniqueController.php b/app/Http/Controllers/TechniqueController.php index 268c3f6..9862325 100644 --- a/app/Http/Controllers/TechniqueController.php +++ b/app/Http/Controllers/TechniqueController.php @@ -34,7 +34,7 @@ class TechniqueController extends Controller public function single(Request $request, Technique $tech) { $this->authorize('view', $tech); - $tech->load('chapters'); + $tech->load(['chapters', 'relations']); return $tech->toJson(); } diff --git a/app/Models/Technique.php b/app/Models/Technique.php index 256afc0..f0a29d3 100644 --- a/app/Models/Technique.php +++ b/app/Models/Technique.php @@ -17,6 +17,13 @@ class Technique extends Model ->using(TechniqueChapter::class); } + public function relations() { + return $this + ->belongsToMany(Technique::class, 'technique_relations', 'from_id', 'to_id') + ->withPivot('type') + ->using(TechniqueRelation::class); + } + public function translations() { return $this->hasMany(TechniqueTranslation::class); } diff --git a/app/Models/TechniqueRelation.php b/app/Models/TechniqueRelation.php new file mode 100644 index 0000000..96c73c3 --- /dev/null +++ b/app/Models/TechniqueRelation.php @@ -0,0 +1,10 @@ +id(); + $table->foreignId('from_id')->references('id')->on('techniques')->constrained(); + $table->foreignId('to_id')->references('id')->on('techniques')->constrained(); + $table->string('type')->default('related'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('technique_relations'); + } +}; diff --git a/resources/js/components/techniques/Detail.js b/resources/js/components/techniques/Detail.js index 303921c..488205b 100644 --- a/resources/js/components/techniques/Detail.js +++ b/resources/js/components/techniques/Detail.js @@ -3,9 +3,15 @@ import React from 'react'; import { Container } from 'react-bootstrap'; import { withTranslation } from 'react-i18next'; +import List from './List'; import Outline from './Outline'; import RawHTML from '../common/RawHTML'; -import { getTranslation } from '../../helpers/Technique'; +import { + getRelations, + getTranslation, + hasRelations, + sorted, +} from '../../helpers/Technique'; import i18n from '../../i18n'; const Detail = ({ technique }) => @@ -24,6 +30,10 @@ const Detail = ({ technique }) => ) : null} + {hasRelations(technique, 'related') ? <> +

{i18n.t('techniques.seeAlso')}

+ + : null}
; Detail.propTypes = { diff --git a/resources/js/helpers/Technique.js b/resources/js/helpers/Technique.js index 7ec22d3..410f015 100644 --- a/resources/js/helpers/Technique.js +++ b/resources/js/helpers/Technique.js @@ -1,3 +1,12 @@ +import i18n from '../i18n'; + +export const getRelations = (tech, type) => { + const rs = (tech && tech.relations) || []; + return type ? rs.filter(r => r && r.pivot && r.pivot.type === type) : rs; +}; + +export const hasRelations = (tech, type) => getRelations(tech, type).length > 0; + export const getTranslation = (tech, prop, lang) => { const direct = tech.translations.find(t => t.locale === lang); if (direct) { @@ -13,6 +22,12 @@ export const getTranslation = (tech, prop, lang) => { export const compareTranslation = (prop, lang) => (a, b) => getTranslation(a, prop, lang).localeCompare(getTranslation(b, prop, lang)); +export const sorted = (techs) => [...techs].sort(compareTranslation('title', i18n.language)); + export default { + compareTranslation, + getRelations, getTranslation, + hasRelations, + sorted, }; diff --git a/resources/js/i18n/de.js b/resources/js/i18n/de.js index c28c1b2..f3fbe96 100644 --- a/resources/js/i18n/de.js +++ b/resources/js/i18n/de.js @@ -480,6 +480,7 @@ export default { }, techniques: { heading: 'Techniken', + seeAlso: 'Siehe auch', }, tournaments: { admins: 'Organisation', diff --git a/resources/js/i18n/en.js b/resources/js/i18n/en.js index dfff38b..7088dd4 100644 --- a/resources/js/i18n/en.js +++ b/resources/js/i18n/en.js @@ -480,6 +480,7 @@ export default { }, techniques: { heading: 'Techniques', + seeAlso: 'See also', }, tournaments: { admins: 'Admins',