@@ -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 ,
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 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 ;
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 > ;
@@ -132,13 +127,20 @@ runtime.registerComponent(CustomerGroupSlot, {
132
127
133
128
if ( ! data ) return [ ] ;
134
129
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
+ ] ;
142
144
} catch ( error ) {
143
145
// eslint-disable-next-line no-console
144
146
console . error ( 'Error fetching customer group options:' , error ) ;
@@ -184,6 +186,7 @@ runtime.registerComponent(CustomerGroupSlot, {
184
186
}
185
187
} ,
186
188
} ) ,
189
+ defaultSlot : Slot ( ) ,
187
190
noGroupSlot : Slot ( ) ,
188
191
} ,
189
192
} ) ;
0 commit comments