-
Notifications
You must be signed in to change notification settings - Fork 3
/
VehicleSelectDialog.tsx
127 lines (120 loc) · 3.07 KB
/
VehicleSelectDialog.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import {
Alert,
Box,
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
Divider,
IconButton
} from "@mui/material";
import React, { useEffect, useState } from "react";
import CloseIcon from "@mui/icons-material/Close";
import { CombinedVehicleProps } from "./VehicleSelectDialog.types";
import type { SelectedVehicle } from "../VehicleSelect/VehicleSelect.types";
import VehicleSelect from "../VehicleSelect/VehicleSelect";
const VehicleSelectDialog = ({
onCancelClick = () => {},
onSaveClick = () => {},
errorMessage = "",
title = "Some title",
cancelText = "cancel",
saveText = "Save",
open = true,
width = "400px",
showCloseIcon = true,
variants = [],
flexDirection = "column",
flexWrap = "nowrap",
gates = []
}: CombinedVehicleProps) => {
// internal state to manage selected vehicles
const [value, setValue] = useState<SelectedVehicle[]>([]);
// check if all fields are filled for each selected vehicle
const isSaveDisabled =
value.length === 0 ||
value.some(
vehicle =>
!vehicle._id ||
!vehicle.gate ||
!vehicle.modelYear ||
!vehicle.projectCode ||
!vehicle.variant
);
// handle save click will return callback with selected vehicles
const handleSaveClick = () => {
if (!isSaveDisabled) {
onSaveClick(value);
}
};
// reset the state when the dialog is opened
useEffect(() => {
if (open) {
setValue([]);
}
}, [open]);
// render the dialog with the vehicle select component
return (
<Dialog
open={open}
data-testid="action-dialog"
sx={{
"& .MuiDialog-container": {
"& .MuiPaper-root": {
maxWidth: width,
width: "100%"
}
}
}}
>
<DialogTitle fontWeight={600} sx={{ px: 2 }}>
{title}
{showCloseIcon ? (
<IconButton
data-testid="close-icon"
aria-label="close"
onClick={onCancelClick}
sx={{
color: theme => theme.palette.grey[500],
position: "absolute",
right: 8,
top: 8
}}
>
<CloseIcon />
</IconButton>
) : null}
</DialogTitle>
<Divider />
<DialogContent sx={{ pt: 1 }}>
<VehicleSelect
variants={variants}
value={value}
flexDirection={flexDirection}
flexWrap={flexWrap}
gates={gates}
onChange={setValue}
/>
{errorMessage ? (
<Box display={"flex"} mt={2}>
<Alert variant="filled" severity="error">
{errorMessage}
</Alert>
</Box>
) : null}
</DialogContent>
<DialogActions sx={{ pb: 3, pt: 2, px: 3 }}>
<Button onClick={onCancelClick}>{cancelText}</Button>
<Button
variant="contained"
onClick={handleSaveClick}
disabled={isSaveDisabled}
>
{saveText}
</Button>
</DialogActions>
</Dialog>
);
};
export default VehicleSelectDialog;