74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
import { cache } from 'react';
|
|
|
|
import { getProductCategories } from '@lib/data/categories';
|
|
import { listProducts, listProductTypes } from '@lib/data/products';
|
|
import { listRegions } from '@lib/data/regions';
|
|
|
|
import { OrderAnalysisCard } from '../../_components/order-analyses-cards';
|
|
|
|
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, is_active',
|
|
});
|
|
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',
|
|
);
|
|
const categoryProducts = category
|
|
? await listProducts({
|
|
countryCode,
|
|
queryParams: { limit: 100, category_id: category.id },
|
|
})
|
|
: null;
|
|
|
|
const serviceCategories = productCategories.filter(
|
|
({ parent_category }) => parent_category?.handle === 'tto-categories',
|
|
);
|
|
|
|
return {
|
|
analyses:
|
|
categoryProducts?.response.products.map<OrderAnalysisCard>(
|
|
({ title, description, subtitle, variants, status, metadata }) => {
|
|
const variant = variants![0]!;
|
|
return {
|
|
title,
|
|
description,
|
|
subtitle,
|
|
variant: {
|
|
id: variant.id,
|
|
},
|
|
isAvailable:
|
|
status === 'published' && !!metadata?.analysisIdOriginal,
|
|
price: variant.calculated_price?.calculated_amount ?? null,
|
|
};
|
|
},
|
|
) ?? [],
|
|
countryCode,
|
|
};
|
|
}
|
|
export const loadAnalyses = cache(analysesLoader);
|