@@ -14,37 +14,35 @@ import { ExpandMoreIcon } from './ExpandMoreIcon';
14
14
import { VariantMappingComponent } from './VariantMapping' ;
15
15
import { VariantComponent } from './Variant' ;
16
16
17
- export function ProductCard ( { product, variantMappingEnabled } ) {
17
+ export default function ProductCard ( { product, variantMappingEnabled } ) {
18
18
const queryClient = useQueryClient ( ) ;
19
19
20
20
function updateOrReplace ( fdcVariants , updatedVariant , variables ) {
21
- const method = variables . fetchInit . method ;
21
+ const { method } = variables . fetchInit ;
22
22
if ( method === 'PUT' ) {
23
23
return [ ...fdcVariants , updatedVariant ] ;
24
- } else if ( method === 'POST' ) {
25
- return fdcVariants . map ( variant => {
26
- return variant . id === updatedVariant . id ? updatedVariant : variant ;
27
- } )
28
- } else if ( method === 'DELETE' ) {
29
- return fdcVariants . filter ( variant => variant . id !== variables . variantId ) ;
30
- } else {
31
- throw new Error ( `dont know how to handle ${ method$ } ` ) ;
24
+ } if ( method === 'POST' && Array . isArray ( updatedVariant ) ) {
25
+ return updatedVariant ;
26
+ } if ( method === 'POST' ) {
27
+ return fdcVariants . map ( ( variant ) => ( variant . id === updatedVariant . id ? updatedVariant : variant ) ) ;
28
+ } if ( method === 'DELETE' ) {
29
+ return fdcVariants . filter ( ( variant ) => variant . id !== variables . variantId ) ;
32
30
}
31
+ throw new Error ( `dont know how to handle ${ method } ` ) ;
33
32
}
34
33
35
- const { mutateAsync : mutateMapping , isFetching : productsLoading } = useAppMutation ( {
34
+ const { mutateAsync : mutateMapping , isLoading , isFetching : productsLoading } = useAppMutation ( {
36
35
reactQueryOptions : {
37
36
onSuccess : ( updatedVariant , variables ) => {
38
37
queryClient . setQueryData ( '/api/products' , ( query ) => {
39
- const updatedProducts = query ?. products ?. map ( existingProduct => {
38
+ const updatedProducts = query ?. products ?. map ( ( existingProduct ) => {
40
39
if ( existingProduct . id === product . id ) {
41
40
return {
42
41
...existingProduct ,
43
42
fdcVariants : updateOrReplace ( existingProduct . fdcVariants , updatedVariant , variables )
44
- }
45
- } else {
46
- return existingProduct ;
43
+ } ;
47
44
}
45
+ return existingProduct ;
48
46
} ) ;
49
47
50
48
return {
@@ -56,70 +54,83 @@ export function ProductCard({ product, variantMappingEnabled }) {
56
54
}
57
55
} ) ;
58
56
59
- const {
60
- mutateAsync : updateVariantMappings ,
61
- status : variantMappingUpdateStatus ,
62
- isFetching : variantMappingsBeingUpdated
63
- } = useAppMutation ( {
64
- reactQueryOptions : {
65
- onSuccess : async ( ) => {
66
- await queryClient . invalidateQueries ( '/api/products' ) ;
67
- }
68
- }
69
- } ) ;
70
-
71
57
const isFdcProduct = ! ! product . fdcVariants . find ( ( { enabled } ) => enabled ) ;
72
58
const hasVariantMapped = ! ! product . fdcVariants [ 0 ] ;
59
+ const allVariantsEnabled = product . fdcVariants . length > 0 &&
60
+ product . variants . length === product . fdcVariants . filter ( ( { enabled } ) => enabled ) . length ;
61
+
62
+ const colour = hasVariantMapped && isFdcProduct ? 'green' : isFdcProduct ? 'red' : 'gray' ;
63
+
64
+ const toggleAllVariants = async ( ) => {
65
+ await mutateMapping ( {
66
+ url : `/api/products/${ product . id } /fdcStatus` ,
67
+ fetchInit : {
68
+ method : 'POST' ,
69
+ headers : {
70
+ 'Content-Type' : 'application/json'
71
+ } ,
72
+ body : JSON . stringify ( { enabled : ! allVariantsEnabled , variants : product . variants . map ( ( { id } ) => id ) } )
73
+ }
74
+
75
+ } ) ;
76
+ } ;
73
77
74
78
return (
75
79
< Accordion key = { product . id } slotProps = { { transition : { unmountOnExit : true } } } >
76
80
< AccordionSummary expandIcon = { < ExpandMoreIcon /> } >
77
81
< Stack direction = "row" justifyContent = "space-between" width = "100%" >
78
- < Typography variant = "h6" > { product . title } </ Typography >
82
+ < Typography variant = "h6" >
83
+ { product . title }
84
+ { ' ' }
85
+ -
86
+ { ' ' }
87
+ < span style = { { color : colour , fontSize : 15 , verticalAlign : 'text-top' } } > { isFdcProduct ? 'Has FDC Enabled Variants' : 'Not FDC Product' } </ span >
88
+ </ Typography >
79
89
< Stack spacing = "20px" direction = "row" alignItems = "center" >
90
+ { ! variantMappingEnabled && (
80
91
< FormControlLabel
81
- style = { { pointerEvents : " none" } }
82
- control = {
92
+ style = { { pointerEvents : ' none' } }
93
+ control = { (
83
94
< Checkbox
84
95
style = { {
85
96
width : '50px' ,
86
97
pointerEvents : 'auto'
87
98
} }
88
- disabled = { true }
89
- checked = { isFdcProduct }
99
+ disabled = { productsLoading || isLoading }
100
+ onClick = { toggleAllVariants }
101
+ checked = { allVariantsEnabled }
90
102
/>
91
- }
92
- sx = { {
93
- '& .MuiFormControlLabel-label' : {
94
- color : hasVariantMapped && isFdcProduct ? 'green !important' : isFdcProduct ? 'red !important' : 'gray'
95
- }
96
- } }
97
- label = { isFdcProduct ? 'Has FDC Enabled Variants' : 'Not FDC Product' }
103
+ ) }
104
+ label = "Toggle FDC status"
98
105
labelPlacement = "start"
99
106
/>
107
+ ) }
100
108
</ Stack >
101
109
102
110
</ Stack >
103
111
</ AccordionSummary >
104
112
< AccordionDetails >
105
113
< Stack spacing = "12px" >
106
- { variantMappingEnabled ?
114
+ { variantMappingEnabled ? (
107
115
< VariantMappingComponent
108
- key = { product . id + ' _variant' + ( product . fdcVariants . length ? '' : '_missing' ) }
116
+ key = { ` ${ product . id } _variant${ product . fdcVariants . length ? '' : '_missing' } ` }
109
117
mutateMapping = { mutateMapping }
110
118
product = { product }
111
119
variant = { product . fdcVariants [ 0 ] }
112
- loadingInProgress = { variantMappingsBeingUpdated || variantMappingUpdateStatus === 'loading' || productsLoading }
120
+ loadingInProgress = { isLoading || productsLoading }
113
121
/>
122
+ )
114
123
:
115
- product . variants . map ( ( variant , i ) =>
116
- ( < VariantComponent
117
- key = { `${ product . id } _variant_${ i } '` }
118
- product = { product }
119
- variant = { variant }
120
- fdcVariants = { product . fdcVariants }
121
- mutateMapping = { mutateMapping }
122
- /> ) )
124
+ product . variants . map ( ( variant ) =>
125
+ (
126
+ < VariantComponent
127
+ key = { `${ product . id } _variant_${ variant . id } '` }
128
+ product = { product }
129
+ variant = { variant }
130
+ fdcVariants = { product . fdcVariants }
131
+ mutateMapping = { mutateMapping }
132
+ />
133
+ ) )
123
134
}
124
135
</ Stack >
125
136
</ AccordionDetails >
0 commit comments