Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve sku selector to exclude attributes #52

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions components/product/ProductVariantSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ function VariantSelector({ product }: Props) {
<span class="text-sm">{name}</span>
<ul class="flex flex-row gap-3">
{Object.entries(possibilities[name]).map(([value, link]) => {
// TODO: bring it back on fresh 1.5.3
// const partial = usePartialSection({ href: link });
const partial = usePartialSection({ href: link });

return (
<li>
<a href={link}>
<button {...partial}>
<Avatar
content={value}
variant={link === url
Expand All @@ -33,7 +32,7 @@ function VariantSelector({ product }: Props) {
? "default"
: "disabled"}
/>
</a>
</button>
</li>
);
})}
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"$store/": "./",
"deco/": "https://denopkg.com/deco-cx/[email protected]/",
"apps/": "https://denopkg.com/deco-cx/[email protected]/",
"$fresh/": "https://deno.land/x/fresh@1.5.2/",
"$fresh/": "https://denopkg.com/denoland/fresh@7ad4610e3a42aba42638cbc1041b96ee58a9b29e/",
"preact": "https://esm.sh/[email protected]",
"preact/": "https://esm.sh/[email protected]/",
"preact-render-to-string": "https://esm.sh/*[email protected]",
Expand Down
35 changes: 19 additions & 16 deletions sdk/useVariantPossiblities.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
import type { ProductLeaf } from "apps/commerce/types.ts";
import type { ProductLeaf, PropertyValue } from "apps/commerce/types.ts";

export type Possibilities = Record<string, Record<string, string | undefined>>;

const hash = ({ name, value }: PropertyValue) => `${name}::${value}`;

const omit = new Set(["category", "cluster", "RefId"]);

export const useVariantPossibilities = (
variants: ProductLeaf[],
selected: ProductLeaf,
): Possibilities => {
const possibilities: Possibilities = {};
const selectedSpecs = new Map(
(selected.additionalProperty ?? [])
.map((s) => [s.name, s] as const),
);
const selectedSpecs = new Set(selected.additionalProperty?.map(hash));

for (const variant of variants) {
const { url, additionalProperty: specs = [] } = variant;
const { url, additionalProperty = [], productID } = variant;
const isSelected = productID === selected.productID;
const specs = additionalProperty.filter(({ name }) => !omit.has(name!));

for (let it = 0; it < specs.length; it++) {
const name = specs[it].name!;
const value = specs[it].value!;

if (omit.has(name)) continue;

if (!possibilities[name]) {
possibilities[name] = {};
}

if (!possibilities[name][value]) {
possibilities[name][value] = it === 0 ? url : undefined;
}

const isSelectable = specs.every((s) =>
s.name === name || selectedSpecs.get(s.name)?.value === s.value
);
// First row is always selectable
const isSelectable = it === 0 ||
specs.every((s) => s.name === name || selectedSpecs.has(hash(s)));

if (isSelectable) {
possibilities[name][value] = url;
}
possibilities[name][value] = isSelected
? url
: isSelectable
? possibilities[name][value] || url
: possibilities[name][value];
}
}

Expand Down