From db1fb41154e5f195655c24d3019603c4766494c0 Mon Sep 17 00:00:00 2001 From: Kyle Kemp Date: Mon, 29 Apr 2024 12:40:51 -0500 Subject: [PATCH] closes #26 --- src/app/faq.service.ts | 73 +++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/src/app/faq.service.ts b/src/app/faq.service.ts index 5db60b8..41c5204 100644 --- a/src/app/faq.service.ts +++ b/src/app/faq.service.ts @@ -1,5 +1,4 @@ import { - effect, inject, Injectable, Injector, @@ -33,49 +32,46 @@ export class FAQService { Record> > = signal({}); - private faqByProductLocaleCard: Record< - string, - Record> - > = {}; + private faqByProductLocaleCard: WritableSignal< + Record>> + > = signal({}); public async init() { this.allFAQs.set(this.metaService.getAllFAQs()); - - effect( - () => { - const locale = this.localeService.currentLocale(); - this.loadLocaleFAQs(locale); - }, - { injector: this.injector, allowSignalWrites: true } - ); + await this.loadLocaleFAQs(); } - private loadLocaleFAQs(locale: string) { + private async loadLocaleFAQs() { const baseFAQs = this.faqByProductIdAndLocale(); - this.allFAQs().forEach(async (faq) => { - if (faq.locale !== locale) return; - if (baseFAQs[faq.productId]?.[faq.locale]) return; + await Promise.all( + this.allFAQs().map(async (faq) => { + if (baseFAQs[faq.productId]?.[faq.locale]) return; - baseFAQs[faq.productId] ??= {}; + baseFAQs[faq.productId] ??= {}; - const faqData = await fetch(faq.url); - const realData = await faqData.json(); + const faqData = await fetch(faq.url); + const realData = await faqData.json(); - baseFAQs[faq.productId][faq.locale] = realData; + baseFAQs[faq.productId][faq.locale] = realData; - this.faqByProductIdAndLocale.set({ - ...this.faqByProductIdAndLocale(), - ...baseFAQs, - }); + this.faqByProductIdAndLocale.set({ + ...this.faqByProductIdAndLocale(), + ...baseFAQs, + }); - realData.forEach((cardFAQ: ICardFAQ) => { - this.faqByProductLocaleCard[faq.productId] ??= {}; - this.faqByProductLocaleCard[faq.productId][faq.locale] ??= {}; - this.faqByProductLocaleCard[faq.productId][faq.locale][cardFAQ.card] ??= - cardFAQ.faq; - }); - }); + const faqByProductLocaleCard = this.faqByProductLocaleCard(); + + realData.forEach((cardFAQ: ICardFAQ) => { + faqByProductLocaleCard[faq.productId] ??= {}; + faqByProductLocaleCard[faq.productId][faq.locale] ??= {}; + faqByProductLocaleCard[faq.productId][faq.locale][cardFAQ.card] ??= + cardFAQ.faq; + }); + + this.faqByProductLocaleCard.set(faqByProductLocaleCard); + }) + ); if (!this.isReady()) this.isReady.set(true); } @@ -109,10 +105,13 @@ export class FAQService { } public getCardFAQ(productId: string, card: string): ICardFAQEntry[] { - return ( - this.faqByProductLocaleCard[productId]?.[ - this.localeService.currentLocale() - ]?.[card] ?? [] - ); + if (!this.isReady()) return []; + + const faq = this.faqByProductLocaleCard(); + const locale = this.localeService.currentLocale(); + + console.log({ faq, locale }, 4); + + return faq[productId]?.[locale]?.[card] ?? []; } }