Skip to content

Commit

Permalink
closes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed May 8, 2024
1 parent 003dec1 commit 108816d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 55 deletions.
1 change: 0 additions & 1 deletion interfaces/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ export interface IProduct extends IProductDefinition {
cardTemplate: Record<string, string>;
external: {
rules: string;
faq: Record<string, string>;
};
}
2 changes: 1 addition & 1 deletion src/app/card/card.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
60 changes: 24 additions & 36 deletions src/app/faq.service.ts
Original file line number Diff line number Diff line change
@@ -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<string, Record<string, ICardFAQ[]>>
> = signal({});
Expand All @@ -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<string, Record<string, ICardFAQ[]>>) {
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<{
Expand Down
17 changes: 0 additions & 17 deletions src/app/meta.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? {};
});
}

Expand Down Expand Up @@ -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();
}
}

0 comments on commit 108816d

Please sign in to comment.