Skip to content

Commit ac549ea

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

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

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

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
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 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;
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>;
@@ -132,13 +127,20 @@ runtime.registerComponent(CustomerGroupSlot, {
132127

133128
if (!data) return [];
134129

135-
return data
136-
.map((d) => ({
137-
id: `${d.id}`,
138-
label: d.name,
139-
value: `${d.id}`,
140-
}))
141-
.filter((option) => option.label.toLowerCase().includes(query.toLowerCase()));
130+
return [
131+
{
132+
id: NO_GROUP_ID,
133+
label: 'No group',
134+
value: NO_GROUP_ID,
135+
},
136+
...data
137+
.map((d) => ({
138+
id: `${d.id}`,
139+
label: d.name,
140+
value: `${d.id}`,
141+
}))
142+
.filter((option) => option.label.toLowerCase().includes(query.toLowerCase())),
143+
];
142144
} catch (error) {
143145
// eslint-disable-next-line no-console
144146
console.error('Error fetching customer group options:', error);
@@ -184,6 +186,7 @@ runtime.registerComponent(CustomerGroupSlot, {
184186
}
185187
},
186188
}),
189+
defaultSlot: Slot(),
187190
noGroupSlot: Slot(),
188191
},
189192
});

0 commit comments

Comments
 (0)