|
1 | 1 | import { useQuery } from '@apollo/client';
|
2 |
| -import { Edit } from '@mui/icons-material'; |
3 |
| -import { Box, Skeleton, Typography } from '@mui/material'; |
| 2 | +import { TabContext, TabList, TabPanel } from '@mui/lab'; |
| 3 | +import { Box, Skeleton, Stack, Typography } from '@mui/material'; |
4 | 4 | import { Helmet } from 'react-helmet-async';
|
5 | 5 | import { useParams } from 'react-router-dom';
|
6 |
| -import { canEditAny } from '~/common'; |
7 |
| -import { useDialog } from '~/components/Dialog'; |
8 |
| -import { |
9 |
| - DisplaySimpleProperty, |
10 |
| - DisplaySimplePropertyProps, |
11 |
| -} from '~/components/DisplaySimpleProperty'; |
12 |
| -import { EditFieldRegion } from '~/components/FieldRegion'; |
13 |
| -import { Link } from '~/components/Routing'; |
| 6 | +import { Tab, TabsContainer } from '~/components/Tabs'; |
| 7 | +import { EnumParam, makeQueryHandler, withDefault } from '~/hooks'; |
14 | 8 | import { Error } from '../../../components/Error';
|
15 |
| -import { Fab } from '../../../components/Fab'; |
16 | 9 | import { Redacted } from '../../../components/Redacted';
|
17 | 10 | import { FieldRegionDetailDocument } from './FieldRegionDetail.graphql';
|
| 11 | +import { FieldRegionProfile } from './Tabs/Profile/FieldRegionProfile'; |
| 12 | +import { FieldRegionProjects } from './Tabs/Projects/FieldRegionProjects'; |
| 13 | + |
| 14 | +const useFieldRegionDetailsFilters = makeQueryHandler({ |
| 15 | + tab: withDefault(EnumParam(['profile', 'projects']), 'profile'), |
| 16 | +}); |
18 | 17 |
|
19 | 18 | export const FieldRegionDetail = () => {
|
20 | 19 | const { fieldRegionId = '' } = useParams();
|
21 | 20 |
|
22 |
| - const [editRegionState, editRegion] = useDialog(); |
23 |
| - |
24 | 21 | const { data, error } = useQuery(FieldRegionDetailDocument, {
|
25 | 22 | variables: { fieldRegionId },
|
26 | 23 | });
|
27 | 24 |
|
| 25 | + const [filters, setFilters] = useFieldRegionDetailsFilters(); |
| 26 | + |
28 | 27 | const fieldRegion = data?.fieldRegion;
|
29 | 28 |
|
30 | 29 | return (
|
31 |
| - <Box |
| 30 | + <Stack |
32 | 31 | component="main"
|
33 | 32 | sx={{
|
34 |
| - flex: 1, |
| 33 | + overflowY: 'auto', |
35 | 34 | p: 4,
|
| 35 | + gap: 3, |
| 36 | + flex: 1, |
| 37 | + maxWidth: (theme) => theme.breakpoints.values.xl, |
36 | 38 | }}
|
37 | 39 | >
|
38 |
| - <Helmet title={fieldRegion?.name.value || undefined} /> |
39 | 40 | <Error error={error}>
|
40 | 41 | {{
|
41 | 42 | NotFound: 'Could not find field region',
|
42 | 43 | Default: 'Error loading field region',
|
43 | 44 | }}
|
44 | 45 | </Error>
|
| 46 | + <Helmet title={fieldRegion?.name.value ?? undefined} /> |
| 47 | + |
45 | 48 | {!error && (
|
46 |
| - <Box |
47 |
| - sx={{ |
48 |
| - maxWidth: (theme) => theme.breakpoints.values.md, |
49 |
| - display: 'flex', |
50 |
| - flexDirection: 'column', |
51 |
| - gap: 3, |
52 |
| - }} |
53 |
| - > |
| 49 | + <> |
54 | 50 | <Box
|
55 |
| - component="header" |
56 | 51 | sx={{
|
57 |
| - flex: 1, |
58 | 52 | display: 'flex',
|
| 53 | + gap: 1, |
59 | 54 | }}
|
60 | 55 | >
|
61 | 56 | <Typography
|
62 | 57 | variant="h2"
|
63 | 58 | sx={{
|
64 |
| - mr: 4, |
65 |
| - width: !fieldRegion?.name.value ? '40%' : undefined, |
| 59 | + mr: 2, |
| 60 | + lineHeight: 'inherit', |
66 | 61 | }}
|
67 | 62 | >
|
68 | 63 | {!fieldRegion ? (
|
69 |
| - <Skeleton width="100%" /> |
| 64 | + <Skeleton width="20ch" /> |
70 | 65 | ) : (
|
71 | 66 | fieldRegion.name.value ?? (
|
72 | 67 | <Redacted
|
73 | 68 | info="You don't have permission to view this field region's name"
|
74 |
| - width="40%" |
| 69 | + width="20ch" |
75 | 70 | />
|
76 | 71 | )
|
77 | 72 | )}
|
78 | 73 | </Typography>
|
79 |
| - {canEditAny(fieldRegion, true) && ( |
80 |
| - <Fab |
81 |
| - color="primary" |
82 |
| - aria-label="edit region" |
83 |
| - onClick={editRegion} |
84 |
| - loading={!fieldRegion} |
85 |
| - > |
86 |
| - <Edit /> |
87 |
| - </Fab> |
88 |
| - )} |
89 |
| - </Box> |
90 |
| - <Box |
91 |
| - sx={{ |
92 |
| - display: 'flex', |
93 |
| - }} |
94 |
| - > |
95 |
| - <Typography variant="h4"> |
96 |
| - {fieldRegion ? 'Field Region' : <Skeleton width={200} />} |
97 |
| - </Typography> |
98 | 74 | </Box>
|
99 |
| - <DisplayProperty |
100 |
| - label="Field Zone" |
101 |
| - value={ |
102 |
| - <Link to={`/field-zones/${fieldRegion?.fieldZone.value?.id}`}> |
103 |
| - {fieldRegion?.fieldZone.value?.name.value} |
104 |
| - </Link> |
105 |
| - } |
106 |
| - loading={!fieldRegion} |
107 |
| - /> |
108 |
| - <DisplayProperty |
109 |
| - label="Director" |
110 |
| - value={ |
111 |
| - <Link to={`/users/${fieldRegion?.director.value?.id}`}> |
112 |
| - {fieldRegion?.director.value?.fullName} |
113 |
| - </Link> |
114 |
| - } |
115 |
| - loading={!fieldRegion} |
116 |
| - /> |
117 |
| - </Box> |
118 |
| - )} |
119 |
| - {fieldRegion && ( |
120 |
| - <EditFieldRegion fieldRegion={fieldRegion} {...editRegionState} /> |
| 75 | + <TabsContainer> |
| 76 | + <TabContext value={filters.tab}> |
| 77 | + <TabList |
| 78 | + onChange={(_e, tab) => setFilters({ ...filters, tab })} |
| 79 | + aria-label="field region navigation tabs" |
| 80 | + variant="scrollable" |
| 81 | + > |
| 82 | + <Tab label="Profile" value="profile" /> |
| 83 | + <Tab label="Projects" value="projects" /> |
| 84 | + </TabList> |
| 85 | + <TabPanel value="profile"> |
| 86 | + {fieldRegion && ( |
| 87 | + <FieldRegionProfile fieldRegion={fieldRegion} /> |
| 88 | + )} |
| 89 | + </TabPanel> |
| 90 | + <TabPanel value="projects"> |
| 91 | + {fieldRegion && <FieldRegionProjects />} |
| 92 | + </TabPanel> |
| 93 | + </TabContext> |
| 94 | + </TabsContainer> |
| 95 | + </> |
121 | 96 | )}
|
122 |
| - </Box> |
| 97 | + </Stack> |
123 | 98 | );
|
124 | 99 | };
|
125 |
| - |
126 |
| -const DisplayProperty = (props: DisplaySimplePropertyProps) => |
127 |
| - !props.value && !props.loading ? null : ( |
128 |
| - <DisplaySimpleProperty |
129 |
| - variant="body1" |
130 |
| - {...{ component: 'div' }} |
131 |
| - {...props} |
132 |
| - loading={ |
133 |
| - props.loading ? ( |
134 |
| - <> |
135 |
| - <Typography variant="body2"> |
136 |
| - <Skeleton width="10%" /> |
137 |
| - </Typography> |
138 |
| - <Typography variant="body1"> |
139 |
| - <Skeleton width="40%" /> |
140 |
| - </Typography> |
141 |
| - </> |
142 |
| - ) : null |
143 |
| - } |
144 |
| - LabelProps={{ |
145 |
| - color: 'textSecondary', |
146 |
| - variant: 'body2', |
147 |
| - ...props.LabelProps, |
148 |
| - }} |
149 |
| - ValueProps={{ |
150 |
| - color: 'textPrimary', |
151 |
| - ...props.ValueProps, |
152 |
| - }} |
153 |
| - /> |
154 |
| - ); |
0 commit comments