35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import { cache } from 'react';
|
|
|
|
import { listProductTypes, listProducts, listRegions } from "@lib/data";
|
|
|
|
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 productTypesLoader() {
|
|
const { productTypes } = await listProductTypes();
|
|
return productTypes ?? [];
|
|
}
|
|
export const loadProductTypes = cache(productTypesLoader);
|
|
|
|
async function analysisPackagesLoader() {
|
|
const [countryCodes, productTypes] = await Promise.all([loadCountryCodes(), loadProductTypes()]);
|
|
const countryCode = countryCodes[0]!;
|
|
|
|
const productType = productTypes.find(({ metadata }) => metadata?.handle === 'analysis-packages');
|
|
if (!productType) {
|
|
return { analysisPackages: [], countryCode };
|
|
}
|
|
|
|
const { response } = await listProducts({
|
|
countryCode,
|
|
queryParams: { limit: 100, "type_id[0]": productType.id },
|
|
});
|
|
return { analysisPackages: response.products, countryCode };
|
|
}
|
|
export const loadAnalysisPackages = cache(analysisPackagesLoader);
|