42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import { cache } from 'react';
|
|
|
|
import { listProductTypes } from "@lib/data/products";
|
|
import { listRegions } from '@lib/data/regions';
|
|
import { getProductCategories } from '@lib/data/categories';
|
|
|
|
async function countryCodesLoader() {
|
|
const countryCodes = await listRegions().then((regions) =>
|
|
regions?.map((r) => r.countries?.map((c) => c.iso_2)).flat(),
|
|
);
|
|
return countryCodes ?? [];
|
|
}
|
|
export const loadCountryCodes = cache(countryCodesLoader);
|
|
|
|
async function productCategoriesLoader() {
|
|
const productCategories = await getProductCategories({ fields: "*products, *products.variants" });
|
|
return productCategories.product_categories ?? [];
|
|
}
|
|
export const loadProductCategories = cache(productCategoriesLoader);
|
|
|
|
async function productTypesLoader() {
|
|
const { productTypes } = await listProductTypes();
|
|
return productTypes ?? [];
|
|
}
|
|
export const loadProductTypes = cache(productTypesLoader);
|
|
|
|
async function analysesLoader() {
|
|
const [countryCodes, productCategories] = await Promise.all([
|
|
loadCountryCodes(),
|
|
loadProductCategories(),
|
|
]);
|
|
const countryCode = countryCodes[0]!;
|
|
|
|
const category = productCategories.find(({ metadata }) => metadata?.page === 'order-analysis');
|
|
|
|
return {
|
|
analyses: category?.products ?? [],
|
|
countryCode,
|
|
}
|
|
}
|
|
export const loadAnalyses = cache(analysesLoader);
|