Skip to content

Commit

Permalink
Merge branch 'dynamic_load'
Browse files Browse the repository at this point in the history
  • Loading branch information
anwolosz committed Jan 18, 2025
2 parents 31bff51 + 6774dc2 commit acefea5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 27 deletions.
19 changes: 13 additions & 6 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import NotFoundView from '@/views/NotFoundView.vue'
import Section from '@/components/Section.vue'
import DailyGospelView from '@/components/DailyGospelView.vue'
import { useSynopsisStore } from "@/stores/SynopsisStore"

const options: { [key: string]: string[] } = {
"hu": ["SZIT", "KG", "KNB", "RUF"],
"en": ["ESV", "EU", "BT", "BJW", "RSP", "SBLGNT", "NV"]
}
import { options } from '@/utils/options'

const languageDefaultRedirect = (path : string) =>
Object.keys(options).map(language => ({
Expand Down Expand Up @@ -128,8 +124,19 @@ const router = createRouter({
},
})

router.beforeEach((to, from, next) => {
router.beforeEach(async (to, from, next) => {

useSynopsisStore().setupLanguage(to.params.language)
let translations = []
for (let i = 0; i< useSynopsisStore().synopses.length; i++)
{
translations.push(useSynopsisStore().synopses[i].translation)
}
if (to.params.translation && !translations.includes(to.params.translation as string))
{
await useSynopsisStore().loadSynopsis((to.params.translation as string).toLowerCase())
}
useSynopsisStore().loadSynopses()
useSynopsisStore().setupTranslation(to.params.translation, options)

const requiresLanguage = to.matched.some((record) => {
Expand Down
79 changes: 58 additions & 21 deletions src/stores/SynopsisStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,45 @@ import { fetchGospel, isValidDate} from '@/utils/calendarGospel';


import synopsisSZIT from '@/assets/translations/szit.json'
import synopsisKG from '@/assets/translations/kg.json'
import synopsisKNB from '@/assets/translations/knb.json'
import synopsisRUF from '@/assets/translations/ruf.json'
// import synopsisKG from '@/assets/translations/kg.json'
// import synopsisKNB from '@/assets/translations/knb.json'
// import synopsisRUF from '@/assets/translations/ruf.json'
import synopsisESV from '@/assets/translations/esv.json'
import synopsisEU from '@/assets/translations/eu.json'
import synopsisBT from '@/assets/translations/bt.json'
import synopsisBJW from '@/assets/translations/bjw.json'
import synopsisRSP from '@/assets/translations/rsp.json'
import synopsisSBLGNT from '@/assets/translations/sblgnt.json'
import synopsisNV from '@/assets/translations/nv.json'
// import synopsisEU from '@/assets/translations/eu.json'
// import synopsisBT from '@/assets/translations/bt.json'
// import synopsisBJW from '@/assets/translations/bjw.json'
// import synopsisRSP from '@/assets/translations/rsp.json'
// import synopsisSBLGNT from '@/assets/translations/sblgnt.json'
// import synopsisNV from '@/assets/translations/nv.json'
import dictionaryEn from '@/assets/languages/en.json'
import dictionaryHu from '@/assets/languages/hu.json'
import type { QuoteScheme } from '@/interfaces/dailyGospelInterface';
import { ErrorCode } from '@/enums/ErrorCode';

import { options } from '@/utils/options';

export const useSynopsisStore = defineStore('synopsis', {
state: () => {
return {
currentDictionary: dictionaryHu,
currentLanguage: "hu",
currentTranslation: "",
currentSynopsis: synopsisKG,
currentSynopsis: synopsisSZIT,
dictionary: {
hu: dictionaryHu as DictionaryScheme,
en: dictionaryEn as DictionaryScheme
},
synopses: [
synopsisSZIT as SynopsisScheme,
synopsisKG as SynopsisScheme,
synopsisKNB as SynopsisScheme,
synopsisRUF as SynopsisScheme,
// synopsisKG as SynopsisScheme,
// synopsisKNB as SynopsisScheme,
// synopsisRUF as SynopsisScheme,
synopsisESV as SynopsisScheme,
synopsisEU as SynopsisScheme,
synopsisBT as SynopsisScheme,
synopsisBJW as SynopsisScheme,
synopsisRSP as SynopsisScheme,
synopsisSBLGNT as SynopsisScheme,
synopsisNV as SynopsisScheme
// synopsisEU as SynopsisScheme,
// synopsisBT as SynopsisScheme,
// synopsisBJW as SynopsisScheme,
// synopsisRSP as SynopsisScheme,
// synopsisSBLGNT as SynopsisScheme,
// synopsisNV as SynopsisScheme
],
isLoading: false,
dailyGospel: null as null | QuoteScheme,
Expand Down Expand Up @@ -335,6 +335,43 @@ export const useSynopsisStore = defineStore('synopsis', {

return false;
}
}
},
getTranslationsList(){
const translationsToLoad : string[] = Object.values(options)
.flat()
.map((value) => value);
return translationsToLoad
},
sortTranslaions(){
const translations = this.getTranslationsList()
this.synopses.sort((a, b) => {
const indexA = translations.indexOf(a.translation);
const indexB = translations.indexOf(b.translation);
return (indexA === -1 ? Number.MAX_VALUE : indexA) - (indexB === -1 ? Number.MAX_VALUE : indexB);
});
},
async loadSynopsis(translation : string)
{
const synopsis = (await import(`@/assets/translations/${translation}.json`)).default;
this.synopses.push(synopsis)
this.sortTranslaions()
},
async loadSynopses() {
const translationsToLoad = this.getTranslationsList()
if (this.synopses.length < translationsToLoad.length)
{
for (const path of translationsToLoad) {
try {
const translation = (await import(`@/assets/translations/${path.toLowerCase()}.json`)).default;
if (!this.synopses.includes(translation)) {
this.synopses.push(translation);
this.sortTranslaions()
}
} catch (error) {
console.error(`Error loading translation from ${path.toLowerCase()}:`, error);
}
}
}
},
}
})
4 changes: 4 additions & 0 deletions src/utils/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const options: { [key: string]: string[] } = {
"hu": ["SZIT", "KG", "KNB", "RUF"],
"en": ["ESV", "EU", "BT", "BJW", "RSP", "SBLGNT", "NV"]
}

0 comments on commit acefea5

Please sign in to comment.