feat(MED-100): analysis package products by product type
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { cache } from 'react';
|
||||
|
||||
import { listCollections, listProducts, listRegions } from "@lib/data";
|
||||
import { listProductTypes, listProducts, listRegions } from "@lib/data";
|
||||
|
||||
async function countryCodesLoader() {
|
||||
const countryCodes = await listRegions().then((regions) =>
|
||||
@@ -10,26 +10,24 @@ async function countryCodesLoader() {
|
||||
}
|
||||
export const loadCountryCodes = cache(countryCodesLoader);
|
||||
|
||||
async function collectionsLoader() {
|
||||
const { collections } = await listCollections({
|
||||
fields: 'id, handle',
|
||||
});
|
||||
return collections ?? [];
|
||||
async function productTypesLoader() {
|
||||
const { productTypes } = await listProductTypes();
|
||||
return productTypes ?? [];
|
||||
}
|
||||
export const loadCollections = cache(collectionsLoader);
|
||||
export const loadProductTypes = cache(productTypesLoader);
|
||||
|
||||
async function analysisPackagesLoader() {
|
||||
const [countryCodes, collections] = await Promise.all([loadCountryCodes(), loadCollections()]);
|
||||
const [countryCodes, productTypes] = await Promise.all([loadCountryCodes(), loadProductTypes()]);
|
||||
const countryCode = countryCodes[0]!;
|
||||
|
||||
const collection = collections.find(({ handle }) => handle === 'analysis-packages');
|
||||
if (!collection) {
|
||||
const productType = productTypes.find(({ metadata }) => metadata?.handle === 'analysis-packages');
|
||||
if (!productType) {
|
||||
return { analysisPackages: [], countryCode };
|
||||
}
|
||||
|
||||
const { response } = await listProducts({
|
||||
countryCode,
|
||||
queryParams: { limit: 100, collection_id: collection?.id },
|
||||
queryParams: { limit: 100, "type_id[0]": productType.id },
|
||||
});
|
||||
return { analysisPackages: response.products, countryCode };
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ export const listProducts = async ({
|
||||
regionId,
|
||||
}: {
|
||||
pageParam?: number
|
||||
queryParams?: HttpTypes.FindParams & HttpTypes.StoreProductParams & { collection_id?: string }
|
||||
queryParams?: HttpTypes.FindParams & HttpTypes.StoreProductParams & { "type_id[0]"?: string }
|
||||
countryCode?: string
|
||||
regionId?: string
|
||||
}): Promise<{
|
||||
|
||||
Reference in New Issue
Block a user