@@ -4,19 +4,14 @@ import {
4
4
Container ,
5
5
Typography ,
6
6
Button ,
7
- FormControl ,
8
- InputLabel ,
9
- Select ,
10
- MenuItem ,
11
7
Grid ,
12
8
IconButton ,
13
9
Stack ,
14
10
SvgIcon ,
15
11
} from "@mui/material" ;
16
12
import { useEffect , useState } from "react" ;
17
- import Head from "next/head" ;
18
13
import DeleteIcon from "@mui/icons-material/Delete" ;
19
- import { useForm } from "react-hook-form" ;
14
+ import { useForm , useWatch } from "react-hook-form" ;
20
15
import CippButtonCard from "../../../../components/CippCards/CippButtonCard" ;
21
16
import CippFormComponent from "../../../../components/CippComponents/CippFormComponent" ;
22
17
import { ArrowLeftIcon } from "@mui/x-date-pickers" ;
@@ -25,6 +20,7 @@ import { CippFormCondition } from "../../../../components/CippComponents/CippFor
25
20
import { ApiGetCall , ApiPostCall } from "../../../../api/ApiCall" ;
26
21
import { CippApiResults } from "../../../../components/CippComponents/CippApiResults" ;
27
22
import { CippHead } from "../../../../components/CippComponents/CippHead" ;
23
+ import { Add , Save } from "@mui/icons-material" ;
28
24
29
25
const Page = ( ) => {
30
26
const router = useRouter ( ) ;
@@ -55,18 +51,28 @@ const Page = () => {
55
51
template . name = `${ template . name } (Clone)` ;
56
52
}
57
53
setLayoutMode ( template . style ) ;
58
- //if the template style is tenant, create enough cards to hold the frontend fields
59
- if ( template . style === "Tenant" ) {
60
- setBlockCards (
61
- template . Fields . map ( ( field , index ) => {
62
- return {
63
- id : `block-${ index } ` ,
64
- } ;
65
- } )
66
- ) ;
67
- }
54
+ setBlockCards (
55
+ template . Fields . map ( ( field , index ) => {
56
+ return {
57
+ id : `block-${ index } ` ,
58
+ } ;
59
+ } )
60
+ ) ;
61
+
62
+ const convertedTemplate = {
63
+ ...template ,
64
+ Fields : template . Fields . map ( ( field ) => ( {
65
+ ...field ,
66
+ ExtractFields : field . ExtractFields
67
+ ? field . ExtractFields . map ( ( ef ) => ( {
68
+ label : ef ,
69
+ value : ef ,
70
+ } ) )
71
+ : [ ] ,
72
+ } ) ) ,
73
+ } ;
68
74
69
- formControl . reset ( template ) ;
75
+ formControl . reset ( convertedTemplate ) ;
70
76
}
71
77
}
72
78
} , [ bpaTemplateList . isSuccess ] ) ;
@@ -152,10 +158,9 @@ const Page = () => {
152
158
addBPATemplate . mutate ( { url : "/api/AddBPATemplate" , data : cleanedData } ) ;
153
159
} ;
154
160
155
- const handleLayoutModeChange = ( event ) => {
156
- const newMode = event . target . value ;
161
+ const handleLayoutModeChange = ( newMode ) => {
157
162
setLayoutMode ( newMode ) ;
158
- formControl . setValue ( "style" , newMode ) ;
163
+ // formControl.setValue("style", newMode);
159
164
160
165
// Reset cards based on the layout mode
161
166
if ( newMode === "Table" ) {
@@ -175,6 +180,14 @@ const Page = () => {
175
180
}
176
181
} ;
177
182
183
+ const style = useWatch ( { control : formControl . control , name : "style" } ) ;
184
+
185
+ useEffect ( ( ) => {
186
+ if ( style && style !== layoutMode ) {
187
+ handleLayoutModeChange ( style ) ;
188
+ }
189
+ } , [ style ] ) ;
190
+
178
191
const onSubmit = ( data ) => { } ;
179
192
return (
180
193
< >
@@ -209,55 +222,43 @@ const Page = () => {
209
222
< CippButtonCard
210
223
CardButton = {
211
224
< >
212
- < Button variant = "contained" onClick = { saveConfig } sx = { { ml : 2 } } >
225
+ < Button variant = "contained" onClick = { saveConfig } startIcon = { < Save /> } >
213
226
Save Report
214
227
</ Button >
215
228
</ >
216
229
}
217
230
title = "Report Settings"
218
231
>
219
- < Grid container spacing = { 4 } >
232
+ < Grid container spacing = { 2 } >
220
233
{ /* First item for Report Name and Layout Mode */ }
221
234
< Grid item xs = { 12 } sm = { 12 } md = { 12 } >
222
235
< CippFormComponent label = "Report Name" name = { `name` } formControl = { formControl } />
223
236
</ Grid >
224
237
225
238
< Grid item xs = { 12 } sm = { 12 } md = { 12 } >
226
- < FormControl fullWidth >
227
- < InputLabel id = "layout-mode-label" > Layout Mode</ InputLabel >
228
- < Select
229
- labelId = "layout-mode-label"
230
- id = "layout-mode"
231
- value = { layoutMode }
232
- onChange = { handleLayoutModeChange }
233
- fullWidth
234
- >
235
- < MenuItem value = "Tenant" > Block</ MenuItem >
236
- < MenuItem value = "Table" > Table</ MenuItem >
237
- </ Select >
238
- </ FormControl >
239
+ < CippFormComponent
240
+ label = "Layout Mode"
241
+ name = "style"
242
+ type = "select"
243
+ options = { [
244
+ { label : "Block" , value : "Tenant" } ,
245
+ { label : "Table" , value : "Table" }
246
+ ] }
247
+ formControl = { formControl }
248
+ />
239
249
</ Grid >
240
250
< Grid item xs = { 12 } sm = { 12 } md = { 12 } >
241
251
< CippApiResults apiObject = { addBPATemplate } />
242
252
</ Grid >
243
-
244
- { /* Third item for Buttons */ }
245
- { layoutMode === "Tenant" && (
246
- < Grid item xs = { 12 } sm = { 12 } md = { 12 } >
247
- < Box sx = { { mt : 2 } } >
248
- < Button variant = "contained" onClick = { handleAddBlock } >
249
- Add Frontend Card
250
- </ Button >
251
- </ Box >
252
- </ Grid >
253
- ) }
254
253
</ Grid >
255
254
</ CippButtonCard >
256
255
257
- { /* Canvas Area */ }
258
- < Typography variant = "h6" gutterBottom >
259
- Canvas
260
- </ Typography >
256
+ < Stack direction = "row" justifyContent = "space-between" alignItems = "center" sx = { { my : 3 } } >
257
+ < Typography variant = "h5" > Fields</ Typography >
258
+ < Button variant = "outlined" size = "small" onClick = { handleAddBlock } startIcon = { < Add /> } >
259
+ Add Field
260
+ </ Button >
261
+ </ Stack >
261
262
262
263
< Grid container spacing = { 2 } >
263
264
{ blockCards . map ( ( block , index ) => (
@@ -269,16 +270,16 @@ const Page = () => {
269
270
key = { block . id }
270
271
>
271
272
< CippButtonCard
272
- title = { block . id === "table-card" ? `Default Table Card` : `BPA Report` }
273
+ title = { `${ layoutMode === "Table" ? "Table Column" : "Block" } - ${
274
+ watch ( `Fields.${ index } .FrontendFields.0.name` ) || "New Field"
275
+ } `}
273
276
cardActions = {
274
- layoutMode === "Tenant" && (
275
- < IconButton
276
- aria-label = "delete"
277
- onClick = { ( ) => handleRemoveBlock ( block . id ) } // Remove block on click
278
- >
279
- < DeleteIcon />
280
- </ IconButton >
281
- )
277
+ < IconButton
278
+ aria-label = "delete"
279
+ onClick = { ( ) => handleRemoveBlock ( block . id ) } // Remove block on click
280
+ >
281
+ < DeleteIcon />
282
+ </ IconButton >
282
283
}
283
284
>
284
285
{ /* Form inside each card */ }
@@ -310,7 +311,8 @@ const Page = () => {
310
311
</ Grid >
311
312
< CippFormCondition
312
313
field = { `Fields.${ index } .UseExistingInfo` } // Corrected condition field
313
- compareValue = { false }
314
+ compareType = "isNot"
315
+ compareValue = { true }
314
316
formControl = { formControl }
315
317
>
316
318
< Grid item xs = { 12 } >
@@ -477,13 +479,17 @@ const Page = () => {
477
479
type = "autoComplete"
478
480
formControl = { formControl }
479
481
multiple = { false }
480
- api = { {
481
- queryKey : `ListBPA` ,
482
- url : "/api/ListBPA" ,
483
- dataKey : "Keys" ,
484
- labelField : ( option ) => `${ option } ` ,
485
- valueField : ( option ) => `${ option } ` ,
486
- } }
482
+ options = {
483
+ bpaTemplateList . data
484
+ ?. flatMap (
485
+ ( template ) => template . Fields ?. map ( ( field ) => field . name ) ?? [ ]
486
+ )
487
+ . filter (
488
+ ( value , index , self ) => value && self . indexOf ( value ) === index
489
+ )
490
+ . map ( ( field ) => ( { label : field , value : field } ) )
491
+ . sort ( ( a , b ) => a . label . localeCompare ( b . label ) ) ?? [ ]
492
+ }
487
493
/>
488
494
</ Grid >
489
495
</ CippFormCondition >
0 commit comments