Skip to content

Commit 616c78a

Browse files
committed
fix: default group added, no-groups added to option of targeted groups control
1 parent dd1f48d commit 616c78a

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

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

Lines changed: 32 additions & 36 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,27 @@ 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,
45+
simulatedGroup: string | undefined,
5446
customerGroupId: number | undefined,
55-
noGroupSlot: ReactNode,
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 customerGroup = customerGroupId || DEFAULT_GROUP_ID;
50+
const simulatedSlot =
51+
targetedSlots?.find((s) => {
52+
if (!s.group) return false;
6353

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

6864
return simulateGroup ? simulatedSlot : actualSlot;
6965
}
@@ -80,11 +76,11 @@ interface Props {
8076
className: string;
8177
slots?: Array<{ group?: string; slot: ReactNode }>;
8278
simulatedGroup?: string;
83-
noGroupSlot: ReactNode;
79+
defaultSlot: ReactNode;
8480
}
8581

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

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

105101
const customerGroupId = data?.customer?.customerGroupId;
106102
const groupSlot = getGroupSlot(
107-
allSlots,
103+
targetedSlots,
108104
isInBuilder,
109105
simulatedGroup,
110106
customerGroupId,
111-
noGroupSlot,
107+
defaultSlot,
112108
);
113109

114110
return <div className={className}>{groupSlot}</div>;
@@ -141,9 +137,9 @@ runtime.registerComponent(CustomerGroupSlot, {
141137
.filter((option) => option.label.toLowerCase().includes(query.toLowerCase()));
142138
} catch (error) {
143139
// eslint-disable-next-line no-console
144-
console.error('Error fetching customer group options:', error);
145-
146-
return [];
140+
throw new Error(
141+
`Error fetching customer group options. Make sure you are using the correct Storefront API key: ${String(error)}`,
142+
);
147143
}
148144
},
149145
}),
@@ -164,9 +160,9 @@ runtime.registerComponent(CustomerGroupSlot, {
164160

165161
return [
166162
{
167-
id: NO_GROUP_ID,
168-
label: 'No group',
169-
value: NO_GROUP_ID,
163+
id: DEFAULT_GROUP_ID,
164+
label: 'Default',
165+
value: DEFAULT_GROUP_ID,
170166
},
171167
...data
172168
.map((d) => ({
@@ -178,12 +174,12 @@ runtime.registerComponent(CustomerGroupSlot, {
178174
];
179175
} catch (error) {
180176
// eslint-disable-next-line no-console
181-
console.error('Error fetching customer group options:', error);
182-
183-
return [];
177+
throw new Error(
178+
`Error fetching customer group options. Make sure you are using the correct Storefront API key: ${String(error)}`,
179+
);
184180
}
185181
},
186182
}),
187-
noGroupSlot: Slot(),
183+
defaultSlot: Slot(),
188184
},
189185
});

0 commit comments

Comments
 (0)