@@ -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,27 @@ 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 ,
45
+ simulatedGroup : string | undefined ,
54
46
customerGroupId : number | undefined ,
55
- noGroupSlot : ReactNode ,
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 customerGroup = customerGroupId || DEFAULT_GROUP_ID ;
50
+ const simulatedSlot =
51
+ targetedSlots ?. find ( ( s ) => {
52
+ if ( ! s . group ) return false ;
63
53
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 ;
67
63
68
64
return simulateGroup ? simulatedSlot : actualSlot ;
69
65
}
@@ -80,11 +76,11 @@ interface Props {
80
76
className : string ;
81
77
slots ?: Array < { group ?: string ; slot : ReactNode } > ;
82
78
simulatedGroup ?: string ;
83
- noGroupSlot : ReactNode ;
79
+ defaultSlot : ReactNode ;
84
80
}
85
81
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 } ) ;
88
84
const isInBuilder = useIsInBuilder ( ) ;
89
85
90
86
const { data, isLoading, error } = useSWR < GetCustomerGroupResponse | undefined , Error > (
@@ -104,11 +100,11 @@ function CustomerGroupSlot({ className, slots, simulatedGroup = NO_GROUP_ID, noG
104
100
105
101
const customerGroupId = data ?. customer ?. customerGroupId ;
106
102
const groupSlot = getGroupSlot (
107
- allSlots ,
103
+ targetedSlots ,
108
104
isInBuilder ,
109
105
simulatedGroup ,
110
106
customerGroupId ,
111
- noGroupSlot ,
107
+ defaultSlot ,
112
108
) ;
113
109
114
110
return < div className = { className } > { groupSlot } </ div > ;
@@ -141,9 +137,9 @@ runtime.registerComponent(CustomerGroupSlot, {
141
137
. filter ( ( option ) => option . label . toLowerCase ( ) . includes ( query . toLowerCase ( ) ) ) ;
142
138
} catch ( error ) {
143
139
// 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
+ ) ;
147
143
}
148
144
} ,
149
145
} ) ,
@@ -164,9 +160,9 @@ runtime.registerComponent(CustomerGroupSlot, {
164
160
165
161
return [
166
162
{
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 ,
170
166
} ,
171
167
...data
172
168
. map ( ( d ) => ( {
@@ -178,12 +174,12 @@ runtime.registerComponent(CustomerGroupSlot, {
178
174
] ;
179
175
} catch ( error ) {
180
176
// 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
+ ) ;
184
180
}
185
181
} ,
186
182
} ) ,
187
- noGroupSlot : Slot ( ) ,
183
+ defaultSlot : Slot ( ) ,
188
184
} ,
189
185
} ) ;
0 commit comments