Skip to content

Commit 0a83e9f

Browse files
committed
fix: update changset
1 parent 074da7c commit 0a83e9f

File tree

2 files changed

+41
-57
lines changed

2 files changed

+41
-57
lines changed

.changeset/shy-owls-move.md

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -78,51 +78,33 @@ export const ProductOptionsFragment = graphql(
7878
```
7979

8080
### Step 3
81-
Update `product-detail-form.tsx` to handle checkbox persist logic:
82-
83-
```ts
84-
case 'checkbox':
85-
return (
86-
<Checkbox
87-
checked={controls.value === field.checkedValue} // add this
88-
errors={formField.errors}
89-
key={formField.id}
90-
label={field.label}
91-
name={formField.name}
92-
onBlur={controls.blur}
93-
onCheckedChange=(value) => handleChange(value ? field.checkedValue.toString() : ''); // add this
94-
onFocus={controls.focus}
95-
required={formField.required}
96-
value={controls.value ?? ''}
97-
/>
98-
);
99-
```
100-
101-
Simplify `handleChange`:
102-
103-
```ts
104-
const handleChange = useCallback(
105-
(value: string) => {
106-
void setParams({ [field.name]: value || null }); // Passing `null` to remove the value from the query params if fieldValue is falsey
107-
controls.change(value || ''); // If fieldValue is falsey, we set it to an empty string
108-
},
109-
[setParams, field, controls],
110-
);
111-
```
112-
113-
Update default value logic to handle checkbox case:
81+
Update `product-detail-form.tsx` to include separate handing of the checkbox field:
11482

11583
```ts
11684
const defaultValue = fields.reduce<{
11785
[Key in keyof SchemaRawShape]?: z.infer<SchemaRawShape[Key]>;
11886
}>(
11987
(acc, field) => {
120-
// Checkbox fields need to be handled differently since we keep track of the checkedValue and not the boolean value of the default value.
88+
// Checkbox field has to be handled separately because we want to convert checked or unchecked value to true or undefined respectively.
89+
// This is because the form expects a boolean value, but we want to store the checked or unchecked value in the query params.
12190
if (field.type === 'checkbox') {
91+
if (params[field.name] === field.checkedValue) {
92+
return {
93+
...acc,
94+
[field.name]: 'true',
95+
};
96+
}
97+
98+
if (params[field.name] === field.uncheckedValue) {
99+
return {
100+
...acc,
101+
[field.name]: undefined,
102+
};
103+
}
104+
122105
return {
123106
...acc,
124-
[field.name]:
125-
(params[field.name] ?? field.defaultValue === 'true') ? field.checkedValue : '',
107+
[field.name]: field.defaultValue, // Default value is either 'true' or undefined
126108
};
127109
}
128110

@@ -133,34 +115,33 @@ const defaultValue = fields.reduce<{
133115
},
134116
{ quantity: minQuantity ?? 1 },
135117
);
118+
119+
...
120+
121+
const handleChange = useCallback(
122+
(value: string) => {
123+
// Checkbox field has to be handled separately because we want to convert 'true' or '' to the checked or unchecked value respectively.
124+
if (field.type === 'checkbox') {
125+
void setParams({ [field.name]: value ? field.checkedValue : field.uncheckedValue });
126+
} else {
127+
void setParams({ [field.name]: value || null }); // Passing `null` to remove the value from the query params if fieldValue is falsey
128+
}
129+
130+
controls.change(value || ''); // If fieldValue is falsey, we set it to an empty string
131+
},
132+
[setParams, field, controls],
133+
);
136134
```
137135

138136
### Step 4
139137

140-
Include update schema in `core/vibes/soul/sections/product-detail/schema.ts`:
138+
Update schema in `core/vibes/soul/sections/product-detail/schema.ts`:
141139

142140
```ts
143141
type CheckboxField = {
144142
type: 'checkbox';
145143
defaultValue?: string;
146-
checkedValue: string;
147-
uncheckedValue: string;
144+
checkedValue: string; // add
145+
uncheckedValue: string; // add
148146
} & FormField;
149147
```
150-
151-
### Step 5
152-
Simplify `core/app/[locale]/(default)/product/[slug]/_actions/add-to-cart.tsx`:
153-
154-
```ts
155-
case 'checkbox':
156-
checkboxOptionInput = {
157-
optionEntityId: Number(field.name),
158-
optionValueEntityId: Number(optionValueEntityId),
159-
};
160-
161-
if (accum.checkboxes) {
162-
return { ...accum, checkboxes: [...accum.checkboxes, checkboxOptionInput] };
163-
}
164-
165-
return { ...accum, checkboxes: [checkboxOptionInput] };
166-
```

core/app/[locale]/(default)/product/[slug]/_actions/add-to-cart.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ export const addToCart = async (
7575
case 'checkbox':
7676
checkboxOptionInput = {
7777
optionEntityId: Number(field.name),
78-
optionValueEntityId: Number(optionValueEntityId),
78+
optionValueEntityId:
79+
optionValueEntityId === 'true'
80+
? Number(field.checkedValue)
81+
: Number(field.uncheckedValue),
7982
};
8083

8184
if (accum.checkboxes) {

0 commit comments

Comments
 (0)