Skip to content

Commit 79bcaf3

Browse files
committed
fix: default group added, no-groups added to option of targeted groups control
1 parent 5c198be commit 79bcaf3

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

core/lib/makeswift/components/customer-group-slot/customer-group-slot.makeswift.tsx

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { runtime } from '~/lib/makeswift/runtime';
1111

1212
import { CustomerGroupSchema, CustomerGroupsSchema, CustomerGroupsType } from './schema';
1313

14-
const NO_GROUP_ID = 'no-group';
14+
const DEFAULT_GROUP_ID = 'default-group';
1515

1616
async function getAllCustomerGroups(): Promise<CustomerGroupsType | null> {
1717
const response = await fetch('/api/customer/groups');
@@ -39,31 +39,26 @@ async function fetchCustomerGroupData(): Promise<GetCustomerGroupResponse | unde
3939
return group;
4040
}
4141

42-
function UntargetedGroup() {
43-
return (
44-
<div className="p-4 text-center text-lg text-gray-400">
45-
This group needs to be added to "Targeted customer groups".
46-
</div>
47-
);
48-
}
49-
5042
function getGroupSlot(
51-
allSlots: Array<{ group?: string; slot: ReactNode }> | undefined,
43+
targetedSlots: Array<{ group?: string; slot: ReactNode }> | undefined,
5244
simulateGroup: boolean,
53-
simulatedGroup: string,
54-
customerGroupId: number | undefined,
55-
noGroupSlot: ReactNode,
45+
simulatedGroup: string | undefined,
46+
customerGroupId: string | undefined = DEFAULT_GROUP_ID,
47+
defaultSlot: ReactNode,
5648
): ReactNode {
57-
const simulatedSlot = allSlots?.find((s) => s.group === simulatedGroup)?.slot ?? (
58-
<UntargetedGroup />
59-
);
60-
const actualSlot = allSlots?.find((s) => s.group === `${customerGroupId}`)?.slot ?? (
61-
<UntargetedGroup />
62-
);
49+
const simulatedSlot =
50+
targetedSlots?.find((s) => {
51+
if (!s.group) return false;
6352

64-
if (!customerGroupId && !simulateGroup) {
65-
return noGroupSlot;
66-
}
53+
return s.group === simulatedGroup;
54+
})?.slot ?? defaultSlot;
55+
56+
const actualSlot =
57+
targetedSlots?.find((s) => {
58+
if (!s.group) return false;
59+
60+
return s.group === `${customerGroupId}`;
61+
})?.slot ?? defaultSlot;
6762

6863
return simulateGroup ? simulatedSlot : actualSlot;
6964
}
@@ -80,11 +75,11 @@ interface Props {
8075
className: string;
8176
slots?: Array<{ group?: string; slot: ReactNode }>;
8277
simulatedGroup?: string;
83-
noGroupSlot: ReactNode;
78+
defaultSlot: ReactNode;
8479
}
8580

86-
function CustomerGroupSlot({ className, slots, simulatedGroup = NO_GROUP_ID, noGroupSlot }: Props) {
87-
const allSlots = slots?.concat({ group: NO_GROUP_ID, slot: noGroupSlot });
81+
function CustomerGroupSlot({ className, slots, simulatedGroup, defaultSlot }: Props) {
82+
const targetedSlots = slots?.concat({ group: DEFAULT_GROUP_ID, slot: defaultSlot });
8883
const isInBuilder = useIsInBuilder();
8984

9085
const { data, isLoading, error } = useSWR<GetCustomerGroupResponse | undefined, Error>(
@@ -104,11 +99,11 @@ function CustomerGroupSlot({ className, slots, simulatedGroup = NO_GROUP_ID, noG
10499

105100
const customerGroupId = data?.customer?.customerGroupId;
106101
const groupSlot = getGroupSlot(
107-
allSlots,
102+
targetedSlots,
108103
isInBuilder,
109104
simulatedGroup,
110105
customerGroupId,
111-
noGroupSlot,
106+
defaultSlot,
112107
);
113108

114109
return <div className={className}>{groupSlot}</div>;
@@ -140,10 +135,9 @@ runtime.registerComponent(CustomerGroupSlot, {
140135
}))
141136
.filter((option) => option.label.toLowerCase().includes(query.toLowerCase()));
142137
} catch (error) {
143-
// eslint-disable-next-line no-console
144-
console.error('Error fetching customer group options:', error);
145-
146-
return [];
138+
throw new Error(
139+
`Error fetching customer group options. Make sure you are using the correct Storefront API key: ${String(error)}`,
140+
);
147141
}
148142
},
149143
}),
@@ -164,9 +158,9 @@ runtime.registerComponent(CustomerGroupSlot, {
164158

165159
return [
166160
{
167-
id: NO_GROUP_ID,
168-
label: 'No group',
169-
value: NO_GROUP_ID,
161+
id: DEFAULT_GROUP_ID,
162+
label: 'Default',
163+
value: DEFAULT_GROUP_ID,
170164
},
171165
...data
172166
.map((d) => ({
@@ -177,13 +171,12 @@ runtime.registerComponent(CustomerGroupSlot, {
177171
.filter((option) => option.label.toLowerCase().includes(query.toLowerCase())),
178172
];
179173
} catch (error) {
180-
// eslint-disable-next-line no-console
181-
console.error('Error fetching customer group options:', error);
182-
183-
return [];
174+
throw new Error(
175+
`Error fetching customer group options. Make sure you are using the correct Storefront API key: ${String(error)}`,
176+
);
184177
}
185178
},
186179
}),
187-
noGroupSlot: Slot(),
180+
defaultSlot: Slot(),
188181
},
189182
});

0 commit comments

Comments
 (0)