-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMultiRankAssignmentDialog.tsx
More file actions
178 lines (170 loc) · 5.36 KB
/
Copy pathMultiRankAssignmentDialog.tsx
File metadata and controls
178 lines (170 loc) · 5.36 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Button,
TextField,
Paper,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
Typography,
} from '@mui/material';
import { useTheme } from '@mui/material/styles';
import React, { ChangeEvent } from 'react';
import { BasicUserDetails } from 'generated/sdk';
import { FapAssignedMember } from './AssignFapMemberToProposalModal';
type MultiRankAssignmentDialogProps = {
users: BasicUserDetails[];
open: boolean;
setOpen: (open: boolean) => void;
assign: (usersWithRank: FapAssignedMember[]) => void;
};
type BasicUserDetailsWithRank = BasicUserDetails & {
rank: number | null; // Rank can be null if not assigned
};
export const MultiRankAssignmentDialog = ({
users,
open,
setOpen,
assign,
}: MultiRankAssignmentDialogProps) => {
const theme = useTheme();
const [usersWithRank, setUsersWithRank] = React.useState<
BasicUserDetailsWithRank[]
>(
users.map((user) => ({
...user,
rank: null,
}))
);
return (
<Dialog
open={open}
onClose={() => {}}
maxWidth="sm"
fullWidth
data-cy="multi-rank-assignment-dialog"
>
<DialogTitle>Mass Assignments with Ranking</DialogTitle>
<DialogContent>
<Typography
variant="body2"
color="textSecondary"
gutterBottom
margin={2}
>
Please assign a unique rank to each user, Ranks must be whole numbers
and greater than 1.
</Typography>
<TableContainer component={Paper}>
<Table aria-label="rank-table">
<TableHead>
<TableRow>
<TableCell>First Name</TableCell>
<TableCell align="left">Surname</TableCell>
<TableCell align="right">Rank</TableCell>
</TableRow>
</TableHead>
<TableBody>
{usersWithRank.map((row) => (
<TableRow
key={row.id}
sx={{
'&:last-child td, &:last-child th': { border: 0 },
backgroundColor: usersWithRank.some(
(user) =>
user.id !== row.id &&
user.rank === row.rank &&
row.rank !== null
)
? theme.palette.error.light
: 'inherit',
}}
>
<TableCell component="th" scope="row">
{row.preferredname ? row.preferredname : row.firstname}
</TableCell>
<TableCell align="left">{row.lastname}</TableCell>
<TableCell align="right">
<TextField
type="number"
variant="outlined"
data-cy={`rank-${row.lastname}`}
InputProps={{
inputProps: { min: 1, step: 1 },
}}
error={
usersWithRank.some(
(user) =>
user.id !== row.id &&
user.rank === row.rank &&
row.rank !== null
) ||
(row.rank !== null && row.rank < 1) ||
(row.rank !== null && isNaN(row.rank))
}
sx={{ width: 100 }}
onChange={(e: ChangeEvent<HTMLInputElement>) => {
const rank = Number(e.target.value); // Non numeric values become 0
setUsersWithRank((prev) =>
prev.map((user) =>
user.id === row.id
? {
...user,
rank: !Number.isInteger(rank) ? 0 : rank,
} // If integer, set to 0 (invalid)
: user
)
);
}}
required
fullWidth
margin="none"
/>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</DialogContent>
<DialogActions>
<Button
onClick={() => {
setOpen(false);
}}
color="primary"
>
Cancel
</Button>
<Button
onClick={() => {
setOpen(false);
assign(usersWithRank);
}}
color="primary"
data-cy="save-ranks"
disabled={
usersWithRank.some((user) => user.rank === null) ||
usersWithRank.some((user) => user.rank !== null && user.rank < 1) ||
usersWithRank.some(
(user) => user.rank !== null && !Number.isInteger(user.rank)
) ||
new Set(
usersWithRank
.filter((user) => user.rank !== null)
.map((user) => user.rank)
).size !== usersWithRank.length
} // Disable if any rank is null, less than 1, NaN, or not unique
>
Submit Mass Assignments
</Button>
</DialogActions>
</Dialog>
);
};