-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
213 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface ICardFAQEntry { | ||
q: string; | ||
a: string; | ||
} | ||
|
||
export interface ICardFAQ { | ||
card: string; | ||
faq: ICardFAQEntry[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './carddata'; | ||
export * from './cardhelp'; | ||
export * from './faq'; | ||
export * from './product'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { effect, inject, Injectable, Injector, signal } from '@angular/core'; | ||
import type { ICardFAQ, ICardFAQEntry } from '../../interfaces'; | ||
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 injector = inject(Injector); | ||
|
||
private isReady = signal<boolean>(false); | ||
public ready$ = this.isReady.asReadonly(); | ||
|
||
private allFAQs: Array<{ | ||
productId: string; | ||
locale: string; | ||
url: string; | ||
}> = []; | ||
|
||
private faqByProductIdAndLocale: Record<string, Record<string, ICardFAQ[]>> = | ||
{}; | ||
|
||
private faqByProductLocaleCard: Record< | ||
string, | ||
Record<string, Record<string, ICardFAQEntry[]>> | ||
> = {}; | ||
|
||
public async init() { | ||
this.allFAQs = this.metaService.getAllFAQs(); | ||
|
||
effect( | ||
() => { | ||
const locale = this.localeService.currentLocale(); | ||
this.loadLocaleFAQs(locale); | ||
}, | ||
{ injector: this.injector, allowSignalWrites: true } | ||
); | ||
} | ||
|
||
private loadLocaleFAQs(locale: string) { | ||
this.allFAQs.forEach(async (faq) => { | ||
if (faq.locale !== locale) return; | ||
if (this.faqByProductIdAndLocale[faq.productId]?.[faq.locale]) return; | ||
|
||
this.faqByProductIdAndLocale[faq.productId] ??= {}; | ||
|
||
const faqData = await fetch(faq.url); | ||
const realData = await faqData.json(); | ||
|
||
this.faqByProductIdAndLocale[faq.productId][faq.locale] = realData; | ||
|
||
realData.forEach((cardFAQ: ICardFAQ) => { | ||
this.faqByProductLocaleCard[faq.productId] ??= {}; | ||
this.faqByProductLocaleCard[faq.productId][faq.locale] ??= {}; | ||
this.faqByProductLocaleCard[faq.productId][faq.locale][cardFAQ.card] ??= | ||
cardFAQ.faq; | ||
}); | ||
}); | ||
|
||
if (!this.isReady()) this.isReady.set(true); | ||
} | ||
|
||
public getCardFAQ(productId: string, card: string): ICardFAQEntry[] { | ||
return ( | ||
this.faqByProductLocaleCard[productId]?.[ | ||
this.localeService.currentLocale() | ||
]?.[card] ?? [] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Injectable, signal } from '@angular/core'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class LocaleService { | ||
private locale = signal<string>('en-US'); | ||
|
||
public get currentLocale() { | ||
return this.locale.asReadonly(); | ||
} | ||
|
||
public async init() {} | ||
} |
Oops, something went wrong.