@@ -11,7 +11,7 @@ import { runtime } from '~/lib/makeswift/runtime';
11
11
12
12
import { CustomerGroupSchema , CustomerGroupsSchema , CustomerGroupsType } from './schema' ;
13
13
14
- const NO_GROUP_ID = 'no -group' ;
14
+ const DEFAULT_GROUP_ID = 'default -group' ;
15
15
16
16
async function getAllCustomerGroups ( ) : Promise < CustomerGroupsType | null > {
17
17
const response = await fetch ( '/api/customer/groups' ) ;
@@ -39,31 +39,26 @@ async function fetchCustomerGroupData(): Promise<GetCustomerGroupResponse | unde
39
39
return group ;
40
40
}
41
41
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
-
50
42
function getGroupSlot (
51
- allSlots : Array < { group ?: string ; slot : ReactNode } > | undefined ,
43
+ targetedSlots : Array < { group ?: string ; slot : ReactNode } > | undefined ,
52
44
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 ,
56
48
) : 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 ;
63
52
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 ;
67
62
68
63
return simulateGroup ? simulatedSlot : actualSlot ;
69
64
}
@@ -80,11 +75,11 @@ interface Props {
80
75
className : string ;
81
76
slots ?: Array < { group ?: string ; slot : ReactNode } > ;
82
77
simulatedGroup ?: string ;
83
- noGroupSlot : ReactNode ;
78
+ defaultSlot : ReactNode ;
84
79
}
85
80
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 } ) ;
88
83
const isInBuilder = useIsInBuilder ( ) ;
89
84
90
85
const { data, isLoading, error } = useSWR < GetCustomerGroupResponse | undefined , Error > (
@@ -104,11 +99,11 @@ function CustomerGroupSlot({ className, slots, simulatedGroup = NO_GROUP_ID, noG
104
99
105
100
const customerGroupId = data ?. customer ?. customerGroupId ;
106
101
const groupSlot = getGroupSlot (
107
- allSlots ,
102
+ targetedSlots ,
108
103
isInBuilder ,
109
104
simulatedGroup ,
110
105
customerGroupId ,
111
- noGroupSlot ,
106
+ defaultSlot ,
112
107
) ;
113
108
114
109
return < div className = { className } > { groupSlot } </ div > ;
@@ -140,10 +135,9 @@ runtime.registerComponent(CustomerGroupSlot, {
140
135
} ) )
141
136
. filter ( ( option ) => option . label . toLowerCase ( ) . includes ( query . toLowerCase ( ) ) ) ;
142
137
} 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
+ ) ;
147
141
}
148
142
} ,
149
143
} ) ,
@@ -164,9 +158,9 @@ runtime.registerComponent(CustomerGroupSlot, {
164
158
165
159
return [
166
160
{
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 ,
170
164
} ,
171
165
...data
172
166
. map ( ( d ) => ( {
@@ -177,13 +171,12 @@ runtime.registerComponent(CustomerGroupSlot, {
177
171
. filter ( ( option ) => option . label . toLowerCase ( ) . includes ( query . toLowerCase ( ) ) ) ,
178
172
] ;
179
173
} 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
+ ) ;
184
177
}
185
178
} ,
186
179
} ) ,
187
- noGroupSlot : Slot ( ) ,
180
+ defaultSlot : Slot ( ) ,
188
181
} ,
189
182
} ) ;
0 commit comments