|
1 | 1 | import { useMutation } from '@apollo/client';
|
2 |
| -import { Box, Typography } from '@mui/material'; |
3 |
| -import { many, Many } from '@seedcompany/common'; |
| 2 | +import { Box, Stack, Typography } from '@mui/material'; |
| 3 | +import { groupBy, many, Many } from '@seedcompany/common'; |
4 | 4 | import { FORM_ERROR } from 'final-form';
|
5 | 5 | import { pick } from 'lodash';
|
6 | 6 | import { ComponentType, useMemo } from 'react';
|
@@ -225,28 +225,57 @@ export const UpdateProjectDialog = ({
|
225 | 225 | });
|
226 | 226 | }}
|
227 | 227 | errorHandlers={{
|
228 |
| - EngagementDateOverrideConflict: ({ engagements }) => { |
| 228 | + DateOverrideConflict: ({ conflicts, object }) => { |
229 | 229 | const rendered = (
|
230 |
| - <> |
231 |
| - <Typography variant="body2" gutterBottom> |
232 |
| - {engagements.length === 1 |
233 |
| - ? 'An engagement has a date outside the new range' |
234 |
| - : 'Some engagements have dates outside the new range'} |
235 |
| - </Typography> |
236 |
| - <Box component="ul" sx={{ m: 0, paddingInlineStart: 4 }}> |
237 |
| - {engagements.map((eng) => ( |
238 |
| - <li key={eng.id + eng.point}> |
239 |
| - {eng.point === 'start' ? 'Start' : 'End'} date of{' '} |
240 |
| - <Link to={`/engagements/${eng.id}`} color="inherit"> |
241 |
| - {eng.label} |
242 |
| - </Link>{' '} |
243 |
| - is <FormattedDate date={CalendarDate.fromISO(eng.date)} /> |
244 |
| - </li> |
245 |
| - ))} |
246 |
| - </Box> |
247 |
| - </> |
| 230 | + <Stack gap={2}> |
| 231 | + {groupBy(conflicts, (conflict) => conflict.__typename).map( |
| 232 | + (conflicts) => { |
| 233 | + const type = conflicts[0].__typename; |
| 234 | + const labels = |
| 235 | + type === 'Partnership' |
| 236 | + ? (['A partnership', 'Some partnerships'] as const) |
| 237 | + : type.endsWith('Engagement') |
| 238 | + ? (['An engagement', 'Some engagements'] as const) |
| 239 | + : (['An object', 'Some objects'] as const); |
| 240 | + const getUrl = |
| 241 | + type === 'Partnership' |
| 242 | + ? () => `/projects/${object.id}/partnerships` |
| 243 | + : type.endsWith('Engagement') |
| 244 | + ? (id: string) => `/engagements/${id}` |
| 245 | + : null; |
| 246 | + return ( |
| 247 | + <div key={type}> |
| 248 | + <Typography variant="body2" gutterBottom> |
| 249 | + {conflicts.length === 1 |
| 250 | + ? `${labels[0]} has a date outside the new range` |
| 251 | + : `${labels[1]} have dates outside the new range`} |
| 252 | + </Typography> |
| 253 | + <Box component="ul" sx={{ m: 0, paddingInlineStart: 4 }}> |
| 254 | + {conflicts.map((conflict) => ( |
| 255 | + <li key={conflict.id + conflict.point}> |
| 256 | + {conflict.point === 'start' ? 'Start' : 'End'} date |
| 257 | + of{' '} |
| 258 | + {getUrl ? ( |
| 259 | + <Link to={getUrl(conflict.id)} color="inherit"> |
| 260 | + {conflict.label} |
| 261 | + </Link> |
| 262 | + ) : ( |
| 263 | + conflict.label |
| 264 | + )}{' '} |
| 265 | + is{' '} |
| 266 | + <FormattedDate |
| 267 | + date={CalendarDate.fromISO(conflict.date)} |
| 268 | + /> |
| 269 | + </li> |
| 270 | + ))} |
| 271 | + </Box> |
| 272 | + </div> |
| 273 | + ); |
| 274 | + } |
| 275 | + )} |
| 276 | + </Stack> |
248 | 277 | );
|
249 |
| - const points = new Set(engagements.map((eng) => eng.point)); |
| 278 | + const points = new Set(conflicts.map((conflict) => conflict.point)); |
250 | 279 | return {
|
251 | 280 | [FORM_ERROR]: rendered,
|
252 | 281 | // Mark the field(s) as invalid,
|
|
0 commit comments