Skip to content

Commit 4f61491

Browse files
authored
Merge pull request #130 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents ecfa093 + bf9d2b5 commit 4f61491

File tree

1 file changed

+73
-67
lines changed

1 file changed

+73
-67
lines changed

src/pages/tenant/standards/bpa-report/builder.js

+73-67
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@ import {
44
Container,
55
Typography,
66
Button,
7-
FormControl,
8-
InputLabel,
9-
Select,
10-
MenuItem,
117
Grid,
128
IconButton,
139
Stack,
1410
SvgIcon,
1511
} from "@mui/material";
1612
import { useEffect, useState } from "react";
17-
import Head from "next/head";
1813
import DeleteIcon from "@mui/icons-material/Delete";
19-
import { useForm } from "react-hook-form";
14+
import { useForm, useWatch } from "react-hook-form";
2015
import CippButtonCard from "../../../../components/CippCards/CippButtonCard";
2116
import CippFormComponent from "../../../../components/CippComponents/CippFormComponent";
2217
import { ArrowLeftIcon } from "@mui/x-date-pickers";
@@ -25,6 +20,7 @@ import { CippFormCondition } from "../../../../components/CippComponents/CippFor
2520
import { ApiGetCall, ApiPostCall } from "../../../../api/ApiCall";
2621
import { CippApiResults } from "../../../../components/CippComponents/CippApiResults";
2722
import { CippHead } from "../../../../components/CippComponents/CippHead";
23+
import { Add, Save } from "@mui/icons-material";
2824

2925
const Page = () => {
3026
const router = useRouter();
@@ -55,18 +51,28 @@ const Page = () => {
5551
template.name = `${template.name} (Clone)`;
5652
}
5753
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+
};
6874

69-
formControl.reset(template);
75+
formControl.reset(convertedTemplate);
7076
}
7177
}
7278
}, [bpaTemplateList.isSuccess]);
@@ -152,10 +158,9 @@ const Page = () => {
152158
addBPATemplate.mutate({ url: "/api/AddBPATemplate", data: cleanedData });
153159
};
154160

155-
const handleLayoutModeChange = (event) => {
156-
const newMode = event.target.value;
161+
const handleLayoutModeChange = (newMode) => {
157162
setLayoutMode(newMode);
158-
formControl.setValue("style", newMode);
163+
//formControl.setValue("style", newMode);
159164

160165
// Reset cards based on the layout mode
161166
if (newMode === "Table") {
@@ -175,6 +180,14 @@ const Page = () => {
175180
}
176181
};
177182

183+
const style = useWatch({ control: formControl.control, name: "style" });
184+
185+
useEffect(() => {
186+
if (style && style !== layoutMode) {
187+
handleLayoutModeChange(style);
188+
}
189+
}, [style]);
190+
178191
const onSubmit = (data) => {};
179192
return (
180193
<>
@@ -209,55 +222,43 @@ const Page = () => {
209222
<CippButtonCard
210223
CardButton={
211224
<>
212-
<Button variant="contained" onClick={saveConfig} sx={{ ml: 2 }}>
225+
<Button variant="contained" onClick={saveConfig} startIcon={<Save />}>
213226
Save Report
214227
</Button>
215228
</>
216229
}
217230
title="Report Settings"
218231
>
219-
<Grid container spacing={4}>
232+
<Grid container spacing={2}>
220233
{/* First item for Report Name and Layout Mode */}
221234
<Grid item xs={12} sm={12} md={12}>
222235
<CippFormComponent label="Report Name" name={`name`} formControl={formControl} />
223236
</Grid>
224237

225238
<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+
/>
239249
</Grid>
240250
<Grid item xs={12} sm={12} md={12}>
241251
<CippApiResults apiObject={addBPATemplate} />
242252
</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-
)}
254253
</Grid>
255254
</CippButtonCard>
256255

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>
261262

262263
<Grid container spacing={2}>
263264
{blockCards.map((block, index) => (
@@ -269,16 +270,16 @@ const Page = () => {
269270
key={block.id}
270271
>
271272
<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+
}`}
273276
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>
282283
}
283284
>
284285
{/* Form inside each card */}
@@ -310,7 +311,8 @@ const Page = () => {
310311
</Grid>
311312
<CippFormCondition
312313
field={`Fields.${index}.UseExistingInfo`} // Corrected condition field
313-
compareValue={false}
314+
compareType="isNot"
315+
compareValue={true}
314316
formControl={formControl}
315317
>
316318
<Grid item xs={12}>
@@ -477,13 +479,17 @@ const Page = () => {
477479
type="autoComplete"
478480
formControl={formControl}
479481
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+
}
487493
/>
488494
</Grid>
489495
</CippFormCondition>

0 commit comments

Comments
 (0)