|
| 1 | +//@flow |
| 2 | + |
| 3 | +import React, { type Node, type ComponentType } from "react"; |
| 4 | +import { observer } from "mobx-react-lite"; |
| 5 | +import SubSampleModel from "../../../stores/models/SubSampleModel"; |
| 6 | +import Grid from "@mui/material/Grid"; |
| 7 | +import Collapse from "@mui/material/Collapse"; |
| 8 | +import ExpandCollapseIcon from "../../../components/ExpandCollapseIcon"; |
| 9 | +import IconButton from "@mui/material/IconButton"; |
| 10 | +import Toolbar from "@mui/material/Toolbar"; |
| 11 | +import AppBar from "@mui/material/AppBar"; |
| 12 | +import Card from "@mui/material/Card"; |
| 13 | +import CardContent from "@mui/material/CardContent"; |
| 14 | +import CardActions from "@mui/material/CardActions"; |
| 15 | +import Link from "@mui/material/Link"; |
| 16 | +import { styled, useTheme, darken, alpha } from "@mui/material/styles"; |
| 17 | +import LocationField from "../../components/Fields/Location"; |
| 18 | +import Box from "@mui/material/Box"; |
| 19 | +import GlobalId from "../../../components/GlobalId"; |
| 20 | +import QuantityField from "../../Subsample/Fields/Quantity"; |
| 21 | +import Stack from "@mui/material/Stack"; |
| 22 | +import Notes from "../../Subsample/Fields/Notes/Notes"; |
| 23 | +import MobileStepper from "@mui/material/MobileStepper"; |
| 24 | +import Button, { buttonClasses } from "@mui/material/Button"; |
| 25 | +import KeyboardArrowLeft from "@mui/icons-material/KeyboardArrowLeft"; |
| 26 | +import KeyboardArrowRight from "@mui/icons-material/KeyboardArrowRight"; |
| 27 | +import { type Search } from "../../../stores/definitions/Search"; |
| 28 | +import { doNotAwait, modulo } from "../../../util/Util"; |
| 29 | +import { svgIconClasses } from "@mui/material/SvgIcon"; |
| 30 | +import { Link as ReactRouterLink } from "react-router-dom"; |
| 31 | +import Typography from "@mui/material/Typography"; |
| 32 | + |
| 33 | +const CustomStepper = styled(MobileStepper)(({ theme }) => ({ |
| 34 | + backgroundColor: theme.palette.record.subSample.lighter, |
| 35 | + borderBottomLeftRadius: "4px", |
| 36 | + borderBottomRightRadius: "4px", |
| 37 | + border: `2px solid ${theme.palette.record.subSample.bg}`, |
| 38 | + borderTop: "none", |
| 39 | + color: alpha(darken(theme.palette.record.subSample.bg, 0.5), 0.7), |
| 40 | + fontWeight: "700", |
| 41 | + letterSpacing: "0.03em", |
| 42 | + [`& .${buttonClasses.root}`]: { |
| 43 | + [`& .${svgIconClasses.root}`]: { |
| 44 | + color: theme.palette.record.subSample.bg, |
| 45 | + }, |
| 46 | + [`&.${buttonClasses.disabled}`]: { |
| 47 | + opacity: 0.3, |
| 48 | + }, |
| 49 | + }, |
| 50 | +})); |
| 51 | + |
| 52 | +const Wrapper = ({ children }: {| children: Node |}) => { |
| 53 | + const [sectionOpen, setSectionOpen] = React.useState(true); |
| 54 | + return ( |
| 55 | + <Grid container direction="row" flexWrap="nowrap" spacing={1}> |
| 56 | + <Grid item sx={{ pl: 0, ml: -2 }}> |
| 57 | + <IconButton |
| 58 | + onClick={() => setSectionOpen(!sectionOpen)} |
| 59 | + sx={{ p: 1.25 }} |
| 60 | + > |
| 61 | + <ExpandCollapseIcon open={sectionOpen} /> |
| 62 | + </IconButton> |
| 63 | + </Grid> |
| 64 | + <Grid item flexGrow={1}> |
| 65 | + <Collapse in={sectionOpen} collapsedSize={50}> |
| 66 | + {children} |
| 67 | + </Collapse> |
| 68 | + </Grid> |
| 69 | + </Grid> |
| 70 | + ); |
| 71 | +}; |
| 72 | + |
| 73 | +type SubsampleDetailsArgs = {| |
| 74 | + search: Search, |
| 75 | +|}; |
| 76 | + |
| 77 | +function SubsampleDetails({ search }: SubsampleDetailsArgs) { |
| 78 | + const theme = useTheme(); |
| 79 | + |
| 80 | + const subsample = search.activeResult; |
| 81 | + if (subsample === null || typeof subsample === "undefined") |
| 82 | + return <Wrapper>No subsamples</Wrapper>; |
| 83 | + const index = search.filteredResults.findIndex( |
| 84 | + (x) => x.globalId === subsample.globalId |
| 85 | + ); |
| 86 | + |
| 87 | + if (!(subsample instanceof SubSampleModel)) |
| 88 | + throw new Error("All Subsamples must be instances of SubSampleModel"); |
| 89 | + |
| 90 | + return ( |
| 91 | + <Wrapper> |
| 92 | + <> |
| 93 | + <Card |
| 94 | + variant="outlined" |
| 95 | + sx={{ |
| 96 | + border: `2px solid ${theme.palette.record.subSample.bg}`, |
| 97 | + borderBottomLeftRadius: 0, |
| 98 | + borderBottomRightRadius: 0, |
| 99 | + }} |
| 100 | + > |
| 101 | + <AppBar |
| 102 | + position="relative" |
| 103 | + open={true} |
| 104 | + sx={{ |
| 105 | + backgroundColor: theme.palette.record.subSample.bg, |
| 106 | + boxShadow: "none", |
| 107 | + }} |
| 108 | + > |
| 109 | + <Toolbar |
| 110 | + variant="dense" |
| 111 | + disableGutters |
| 112 | + sx={{ |
| 113 | + px: 1.5, |
| 114 | + }} |
| 115 | + > |
| 116 | + {subsample.name} |
| 117 | + <Box flexGrow={1}></Box> |
| 118 | + <GlobalId record={subsample} /> |
| 119 | + </Toolbar> |
| 120 | + </AppBar> |
| 121 | + <CardContent> |
| 122 | + <Stack spacing={2}> |
| 123 | + <LocationField fieldOwner={subsample} /> |
| 124 | + <QuantityField |
| 125 | + fieldOwner={subsample} |
| 126 | + quantityCategory={subsample.quantityCategory} |
| 127 | + onErrorStateChange={() => {}} |
| 128 | + /> |
| 129 | + <Notes record={subsample} onErrorStateChange={() => {}} /> |
| 130 | + </Stack> |
| 131 | + </CardContent> |
| 132 | + <CardActions> |
| 133 | + <Typography align="center" sx={{ width: "100%" }}> |
| 134 | + <Link component={ReactRouterLink} to={subsample.permalinkURL}> |
| 135 | + See full details of <strong>{subsample.name}</strong> |
| 136 | + </Link> |
| 137 | + </Typography> |
| 138 | + </CardActions> |
| 139 | + </Card> |
| 140 | + <CustomStepper |
| 141 | + variant="text" |
| 142 | + steps={search.count} |
| 143 | + activeStep={ |
| 144 | + index + search.fetcher.pageSize * search.fetcher.pageNumber |
| 145 | + } |
| 146 | + position="static" |
| 147 | + nextButton={ |
| 148 | + <Button |
| 149 | + size="small" |
| 150 | + onClick={doNotAwait(async () => { |
| 151 | + if (index + 1 > search.filteredResults.length - 1) |
| 152 | + await search.setPage(search.fetcher.pageNumber + 1); |
| 153 | + await search.setActiveResult( |
| 154 | + search.filteredResults[(index + 1) % search.fetcher.pageSize] |
| 155 | + ); |
| 156 | + })} |
| 157 | + disabled={ |
| 158 | + index + |
| 159 | + search.fetcher.pageSize * search.fetcher.pageNumber + |
| 160 | + 1 >= |
| 161 | + search.count |
| 162 | + } |
| 163 | + > |
| 164 | + <KeyboardArrowRight /> |
| 165 | + </Button> |
| 166 | + } |
| 167 | + backButton={ |
| 168 | + <Button |
| 169 | + size="small" |
| 170 | + onClick={doNotAwait(async () => { |
| 171 | + if (index === 0) |
| 172 | + await search.setPage(search.fetcher.pageNumber - 1); |
| 173 | + await search.setActiveResult( |
| 174 | + search.filteredResults[ |
| 175 | + modulo(index - 1, search.fetcher.pageSize) |
| 176 | + ] |
| 177 | + ); |
| 178 | + })} |
| 179 | + disabled={ |
| 180 | + index + search.fetcher.pageSize * search.fetcher.pageNumber === |
| 181 | + 0 |
| 182 | + } |
| 183 | + > |
| 184 | + <KeyboardArrowLeft /> |
| 185 | + </Button> |
| 186 | + } |
| 187 | + /> |
| 188 | + </> |
| 189 | + </Wrapper> |
| 190 | + ); |
| 191 | +} |
| 192 | + |
| 193 | +export default (observer( |
| 194 | + SubsampleDetails |
| 195 | +): ComponentType<SubsampleDetailsArgs>); |
0 commit comments