mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 22:16:58 +00:00
feat: variant selector
This commit is contained in:
parent
e0e67f4270
commit
cfe181ac41
@ -12,7 +12,6 @@ export async function generateMetadata({
|
|||||||
}: {
|
}: {
|
||||||
params: { page: string };
|
params: { page: string };
|
||||||
}): Promise<Metadata> {
|
}): Promise<Metadata> {
|
||||||
console.log(params);
|
|
||||||
const page: any = null;
|
const page: any = null;
|
||||||
|
|
||||||
if (!page) return notFound();
|
if (!page) return notFound();
|
||||||
@ -36,7 +35,6 @@ export async function generateMetadata({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default async function Page({ params }: { params: { page: string } }) {
|
export default async function Page({ params }: { params: { page: string } }) {
|
||||||
console.log(params);
|
|
||||||
const page: any = null;
|
const page: any = null;
|
||||||
|
|
||||||
if (!page) return notFound();
|
if (!page) return notFound();
|
||||||
|
@ -90,6 +90,7 @@ export function VariantSelector({
|
|||||||
{option.values.map((value) => {
|
{option.values.map((value) => {
|
||||||
// Base option params on selected variant params.
|
// Base option params on selected variant params.
|
||||||
const optionParams = new URLSearchParams(selectedVariantParams);
|
const optionParams = new URLSearchParams(selectedVariantParams);
|
||||||
|
|
||||||
// Update the params using the current option to reflect how the url would change.
|
// Update the params using the current option to reflect how the url would change.
|
||||||
optionParams.set(option.name.toLowerCase(), value);
|
optionParams.set(option.name.toLowerCase(), value);
|
||||||
|
|
||||||
|
@ -9,7 +9,8 @@ import {
|
|||||||
Product,
|
Product,
|
||||||
ProductCollection,
|
ProductCollection,
|
||||||
ProductOption,
|
ProductOption,
|
||||||
ProductVariant
|
ProductVariant,
|
||||||
|
SelectedOption
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
// const endpoint = `${process.env.MEDUSA_BACKEND_API!}`;
|
// const endpoint = `${process.env.MEDUSA_BACKEND_API!}`;
|
||||||
@ -84,11 +85,12 @@ const reshapeProduct = (product: MedusaProduct): Product => {
|
|||||||
altText: product.images?.[0]?.id ?? ''
|
altText: product.images?.[0]?.id ?? ''
|
||||||
};
|
};
|
||||||
const availableForSale = true;
|
const availableForSale = true;
|
||||||
const variants = product.variants.map((variant) => reshapeProductVariant(variant));
|
const variants = product.variants.map((variant) =>
|
||||||
|
reshapeProductVariant(variant, product.options)
|
||||||
|
);
|
||||||
|
|
||||||
let options;
|
let options;
|
||||||
product.options && (options = product.options.map((option) => reshapeProductOption(option)));
|
product.options && (options = product.options.map((option) => reshapeProductOption(option)));
|
||||||
// console.log({ options });
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...product,
|
...product,
|
||||||
@ -117,14 +119,28 @@ const reshapeProductOption = (productOption: MedusaProductOption): ProductOption
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const reshapeProductVariant = (productVariant: MedusaProductVariant): ProductVariant => {
|
const mapOptionIds = (productOptions: MedusaProductOption[]) => {
|
||||||
console.log({ productVariant });
|
const map: Record<string, string> = {};
|
||||||
const availableForSale = !!productVariant.inventory_quantity;
|
productOptions.forEach((option) => {
|
||||||
const selectedOptions =
|
map[option.id] = option.title;
|
||||||
productVariant.options?.map((option) => ({
|
});
|
||||||
name: option.option?.title ?? '',
|
return map;
|
||||||
|
};
|
||||||
|
|
||||||
|
const reshapeProductVariant = (
|
||||||
|
productVariant: MedusaProductVariant,
|
||||||
|
productOptions?: MedusaProductOption[]
|
||||||
|
): ProductVariant => {
|
||||||
|
let selectedOptions: SelectedOption[] = [];
|
||||||
|
if (productOptions && productVariant.options) {
|
||||||
|
const optionIdMap = mapOptionIds(productOptions);
|
||||||
|
selectedOptions = productVariant.options.map((option) => ({
|
||||||
|
name: optionIdMap[option.option_id] ?? '',
|
||||||
value: option.value
|
value: option.value
|
||||||
})) || [];
|
}));
|
||||||
|
}
|
||||||
|
const availableForSale = !!productVariant.inventory_quantity;
|
||||||
|
|
||||||
const price = {
|
const price = {
|
||||||
amount: productVariant.prices?.[0]?.amount.toString() ?? '',
|
amount: productVariant.prices?.[0]?.amount.toString() ?? '',
|
||||||
currencyCode: productVariant.prices?.[0]?.currency_code ?? ''
|
currencyCode: productVariant.prices?.[0]?.currency_code ?? ''
|
||||||
|
@ -159,11 +159,13 @@ export type MedusaProductVariant = {
|
|||||||
|
|
||||||
export type ProductVariant = MedusaProductVariant & {
|
export type ProductVariant = MedusaProductVariant & {
|
||||||
availableForSale: boolean;
|
availableForSale: boolean;
|
||||||
selectedOptions: {
|
selectedOptions: SelectedOption[];
|
||||||
|
price: Money;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SelectedOption = {
|
||||||
name: string;
|
name: string;
|
||||||
value: string;
|
value: string;
|
||||||
}[];
|
|
||||||
price: Money;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type MedusaProductOption = {
|
export type MedusaProductOption = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user