diff --git a/interfaces/product.ts b/interfaces/product.ts index a93bdea..9b5968d 100644 --- a/interfaces/product.ts +++ b/interfaces/product.ts @@ -16,6 +16,5 @@ export interface IProduct extends IProductDefinition { cardTemplate: Record; external: { rules: string; - faq: Record; }; } diff --git a/src/app/card/card.page.ts b/src/app/card/card.page.ts index 93e850f..417b48d 100644 --- a/src/app/card/card.page.ts +++ b/src/app/card/card.page.ts @@ -34,7 +34,7 @@ export class CardPage implements OnInit { const cardData = this.cardData(); if (!cardData) return []; - return this.faqService.getCardFAQ(cardData.product, cardData.name); + return this.faqService.getCardFAQ(cardData.game, cardData.name); }); ngOnInit() { diff --git a/src/app/faq.service.ts b/src/app/faq.service.ts index 737254e..e791748 100644 --- a/src/app/faq.service.ts +++ b/src/app/faq.service.ts @@ -1,24 +1,15 @@ import { inject, Injectable, signal, type WritableSignal } from '@angular/core'; import { sortBy } from 'lodash'; import type { ICardFAQ, ICardFAQEntry } from '../../interfaces'; +import { environment } from '../environments/environment'; import { LocaleService } from './locale.service'; -import { MetaService } from './meta.service'; @Injectable({ providedIn: 'root', }) export class FAQService { - private metaService = inject(MetaService); private localeService = inject(LocaleService); - private allFAQs: WritableSignal< - Array<{ - productId: string; - locale: string; - url: string; - }> - > = signal([]); - private faqByProductIdAndLocale: WritableSignal< Record> > = signal({}); @@ -28,41 +19,38 @@ export class FAQService { > = signal({}); public async init() { - this.allFAQs.set(this.metaService.getAllFAQs()); - await this.loadLocaleFAQs(); + const faqData = await fetch(`${environment.baseUrl}/faq.json`); + const realData = await faqData.json(); + + this.parseLocaleFAQs(realData); } - private async loadLocaleFAQs() { + private parseLocaleFAQs(faqData: Record>) { const baseFAQs = this.faqByProductIdAndLocale(); + const faqByProductLocaleCard = this.faqByProductLocaleCard(); - await Promise.all( - this.allFAQs().map(async (faq) => { - if (baseFAQs[faq.productId]?.[faq.locale]) return; - - baseFAQs[faq.productId] ??= {}; - - const faqData = await fetch(faq.url); - const realData = await faqData.json(); + Object.keys(faqData).forEach((productId) => { + baseFAQs[productId] ??= {}; - baseFAQs[faq.productId][faq.locale] = sortBy(realData, 'card'); + Object.keys(faqData[productId]).forEach((locale) => { + baseFAQs[productId][locale] = sortBy( + faqData[productId][locale], + 'card' + ); - this.faqByProductIdAndLocale.set({ - ...this.faqByProductIdAndLocale(), - ...baseFAQs, - }); - - const faqByProductLocaleCard = this.faqByProductLocaleCard(); - - realData.forEach((cardFAQ: ICardFAQ) => { - faqByProductLocaleCard[faq.productId] ??= {}; - faqByProductLocaleCard[faq.productId][faq.locale] ??= {}; - faqByProductLocaleCard[faq.productId][faq.locale][cardFAQ.card] ??= + faqData[productId][locale].forEach((cardFAQ) => { + faqByProductLocaleCard[productId] ??= {}; + faqByProductLocaleCard[productId][locale] ??= {}; + faqByProductLocaleCard[productId][locale][cardFAQ.card] ??= cardFAQ.faq; }); + }); + }); + + this.faqByProductIdAndLocale.set(baseFAQs); + this.faqByProductLocaleCard.set(faqByProductLocaleCard); - this.faqByProductLocaleCard.set(faqByProductLocaleCard); - }) - ); + console.log(baseFAQs, faqByProductLocaleCard); } public getFAQs(): Array<{ diff --git a/src/app/meta.service.ts b/src/app/meta.service.ts index 8080ba6..25edc28 100644 --- a/src/app/meta.service.ts +++ b/src/app/meta.service.ts @@ -39,7 +39,6 @@ export class MetaService { this.templatesByProductId[product.id] = product.cardTemplate; this.rulesByProductId[product.id] = product.external?.rules ?? ''; this.filtersByProductId[product.id] = product.filters; - this.faqByProductId[product.id] = product.external?.faq ?? {}; }); } @@ -68,20 +67,4 @@ export class MetaService { public getAllFilters(): IProductFilter[] { return Object.values(this.filtersByProductId).flat(); } - - public getAllFAQs(): Array<{ - productId: string; - locale: string; - url: string; - }> { - return Object.keys(this.faqByProductId) - .map((productId) => - Object.keys(this.faqByProductId[productId]).map((locale) => ({ - productId, - locale, - url: this.faqByProductId[productId][locale], - })) - ) - .flat(); - } }