|
| 1 | +import React from "react"; |
| 2 | +import Box from "@material-ui/core/Box"; |
| 3 | +import { makeStyles } from "@material-ui/core/styles/index"; |
| 4 | +import Button from '@material-ui/core/Button'; |
| 5 | +import Dialog from '@material-ui/core/Dialog'; |
| 6 | +import DialogActions from '@material-ui/core/DialogActions'; |
| 7 | +import DialogContent from '@material-ui/core/DialogContent'; |
| 8 | +import DialogTitle from '@material-ui/core/DialogTitle'; |
| 9 | +import Typography from "@material-ui/core/Typography"; |
| 10 | +import MenuItem from '@material-ui/core/MenuItem'; |
| 11 | +import TextField from '@material-ui/core/TextField'; |
| 12 | +import ArrowForwardIcon from '@material-ui/icons/ArrowForward'; |
| 13 | +import ArrowBack from '@material-ui/icons/ArrowBack'; |
| 14 | +import { AddIcon } from "../../../assets/icons"; |
| 15 | +import Grid from '@material-ui/core/Grid'; |
| 16 | +import { useMutation } from "@apollo/react-hooks"; |
| 17 | +import { UPDATE_PRACTICE_RESOURCES } from "../../../graphql"; |
| 18 | + |
| 19 | +const useStyles = makeStyles((theme) => ({ |
| 20 | + root: { |
| 21 | + display: "flex", |
| 22 | + flexDirection: "row", |
| 23 | + padding: theme.spacing(1), |
| 24 | + margin: theme.spacing(1), |
| 25 | + }, |
| 26 | + container: { |
| 27 | + flexShrink: 0, |
| 28 | + }, |
| 29 | + space: { |
| 30 | + paddingRight: theme.spacing(2), |
| 31 | + }, |
| 32 | + drawerPaper: { |
| 33 | + borderRadius: "17px", |
| 34 | + borderWidth: "3px", |
| 35 | + borderStyle: "solid", |
| 36 | + display: "flex", |
| 37 | + textAlign: "center", |
| 38 | + padding: theme.spacing(3), |
| 39 | + }, |
| 40 | + |
| 41 | + dialogText: { |
| 42 | + margin: theme.spacing(1), |
| 43 | + }, |
| 44 | + submitButton: { |
| 45 | + margin: theme.spacing(1), |
| 46 | + borderRadius: "32px", |
| 47 | + borderStyle: "solid", |
| 48 | + borderWidth: "2px", |
| 49 | + borderColor: "#596562", |
| 50 | + }, |
| 51 | + arrowForward: { |
| 52 | + top: ".25em", |
| 53 | + position: "relative", |
| 54 | + }, |
| 55 | + btnText: { |
| 56 | + padding: theme.spacing(1), |
| 57 | + color: theme.palette.common.black, |
| 58 | + }, |
| 59 | + color: { |
| 60 | + color: theme.palette.common.black, |
| 61 | + }, |
| 62 | +})); |
| 63 | + |
| 64 | +export default function ResourceAddLink(props) { |
| 65 | + const classes = useStyles(); |
| 66 | + |
| 67 | + const [updatePracticeResources] = useMutation(UPDATE_PRACTICE_RESOURCES); |
| 68 | + |
| 69 | + |
| 70 | + const [open, setOpen] = React.useState(false); |
| 71 | + const [linkType, setLinkType] = React.useState(''); |
| 72 | + const refLinkUrl = React.useRef(); |
| 73 | + const refLinkDesc = React.useRef(); |
| 74 | + const [thankYouOpen, setThankYouOpen] = React.useState(false); |
| 75 | + |
| 76 | + const handleSubmit = async () => { |
| 77 | + const prevResourceList = props.prevResources.map(resource => { |
| 78 | + return { |
| 79 | + link: resource.link, |
| 80 | + linkType: resource.linkType, |
| 81 | + description: resource.description |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + const additionalResource = [ |
| 86 | + { |
| 87 | + link: refLinkUrl.current.value, |
| 88 | + linkType: linkType, |
| 89 | + description: refLinkDesc.current.value, |
| 90 | + } |
| 91 | + ]; |
| 92 | + const newResources = prevResourceList.concat(additionalResource); |
| 93 | + const { data } = await updatePracticeResources({ |
| 94 | + variables: { |
| 95 | + practiceId: props.practiceId, |
| 96 | + newResources, |
| 97 | + }, |
| 98 | + }); |
| 99 | + if (data) { |
| 100 | + console.log('Updated!'); |
| 101 | + handleClose(true); |
| 102 | + setThankYouOpen(true); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + const handleClickListItem = (event) => { |
| 107 | + setLinkType(event.target.value) |
| 108 | + }; |
| 109 | + |
| 110 | + const handleClickOpen = () => { |
| 111 | + setOpen(true); |
| 112 | + }; |
| 113 | + |
| 114 | + const handleClose = (event) => { |
| 115 | + setOpen(false); |
| 116 | + setLinkType(''); |
| 117 | + refLinkUrl.current.value = ""; |
| 118 | + refLinkDesc.current.value = ""; |
| 119 | + }; |
| 120 | + |
| 121 | + const handleThankYouClose = () => { |
| 122 | + setThankYouOpen(false); |
| 123 | + }; |
| 124 | + |
| 125 | + const handleThankYouSubmit = () => { |
| 126 | + setThankYouOpen(false); |
| 127 | + setOpen(true); |
| 128 | + }; |
| 129 | + |
| 130 | + return ( |
| 131 | + <> |
| 132 | + <Box className={classes.root}> |
| 133 | + <Box>{props.children}</Box> |
| 134 | + <Box> |
| 135 | + <Button |
| 136 | + variant="text" |
| 137 | + color="secondary" |
| 138 | + className={classes.button} |
| 139 | + startIcon={<AddIcon fill="#1975ff"/>} |
| 140 | + onClick={handleClickOpen} |
| 141 | + data-testid="addResourcesButton" |
| 142 | + > |
| 143 | + <Typography variant={"body1"}> |
| 144 | + Add a link |
| 145 | + </Typography> |
| 146 | + </Button> |
| 147 | + <Dialog |
| 148 | + open={open} |
| 149 | + onClose={handleClose} |
| 150 | + aria-labelledby="link-type-dialog-title" |
| 151 | + PaperProps={{ |
| 152 | + className: classes.drawerPaper, |
| 153 | + }} |
| 154 | + > |
| 155 | + <DialogTitle disableTypography={true} id="link-type-dialog-title"> |
| 156 | + <Typography |
| 157 | + variant="subtitle2" |
| 158 | + className={classes.dialogText} |
| 159 | + data-testid="addResourceForm" |
| 160 | + > |
| 161 | + Add a link you love! |
| 162 | + </Typography> |
| 163 | + </DialogTitle> |
| 164 | + <DialogContent className={classes.container}> |
| 165 | + <Grid container spacing={2}> |
| 166 | + <Grid item xs={12} sm={5}> |
| 167 | + <TextField |
| 168 | + id="selectedLinkType" |
| 169 | + select |
| 170 | + required |
| 171 | + label="Link Type" |
| 172 | + variant="outlined" |
| 173 | + margin="dense" |
| 174 | + value={linkType} |
| 175 | + onChange={handleClickListItem} |
| 176 | + fullWidth |
| 177 | + > |
| 178 | + {props.linkTypes.map((option, index) => ( |
| 179 | + <MenuItem |
| 180 | + key={index} |
| 181 | + value={option} |
| 182 | + > |
| 183 | + {option} |
| 184 | + </MenuItem> |
| 185 | + ))} |
| 186 | + </TextField> |
| 187 | + </Grid> |
| 188 | + <Grid item xs={12} sm={7}> |
| 189 | + <TextField |
| 190 | + margin="dense" |
| 191 | + id="link" |
| 192 | + label="Link URL*" |
| 193 | + type="url" |
| 194 | + variant="outlined" |
| 195 | + inputRef={refLinkUrl} |
| 196 | + fullWidth |
| 197 | + /> |
| 198 | + </Grid> |
| 199 | + <Grid item xs={12}> |
| 200 | + <TextField |
| 201 | + margin="dense" |
| 202 | + id="description" |
| 203 | + label="Link Description*" |
| 204 | + type="text" |
| 205 | + variant="outlined" |
| 206 | + inputRef={refLinkDesc} |
| 207 | + fullWidth |
| 208 | + /> |
| 209 | + </Grid> |
| 210 | + </Grid> |
| 211 | + </DialogContent> |
| 212 | + <DialogActions> |
| 213 | + <Grid |
| 214 | + container |
| 215 | + direction="row" |
| 216 | + justify="space-between" |
| 217 | + alignItems="center" |
| 218 | + > |
| 219 | + <Grid item> |
| 220 | + <Button |
| 221 | + onClick={handleSubmit} |
| 222 | + variant="contained" |
| 223 | + className={classes.submitButton} |
| 224 | + > |
| 225 | + <Typography |
| 226 | + variant="button" |
| 227 | + className={classes.btnText} |
| 228 | + > |
| 229 | + Contribute this link <ArrowForwardIcon className={classes.arrowForward} /> |
| 230 | + </Typography> |
| 231 | + </Button> |
| 232 | + </Grid> |
| 233 | + <Grid item> |
| 234 | + <Typography variant="overline"> |
| 235 | + *Required fields :) |
| 236 | + </Typography> |
| 237 | + </Grid> |
| 238 | + </Grid> |
| 239 | + </DialogActions> |
| 240 | + </Dialog> |
| 241 | + <Dialog |
| 242 | + open={thankYouOpen} |
| 243 | + onClose={handleThankYouClose} |
| 244 | + aria-labelledby="thank-you-dialog" |
| 245 | + PaperProps={{ |
| 246 | + className: classes.drawerPaper, |
| 247 | + }} |
| 248 | + > |
| 249 | + <Grid |
| 250 | + container |
| 251 | + direction="column" |
| 252 | + justify="center" |
| 253 | + alignItems="center" |
| 254 | + > |
| 255 | + <Grid item> |
| 256 | + <DialogTitle disableTypography={true} id="thank-you-dialog"> |
| 257 | + <Typography |
| 258 | + variant="subtitle2" |
| 259 | + className={classes.dialogText} |
| 260 | + > |
| 261 | + Awesome.<br/>Thanks for that! |
| 262 | + </Typography> |
| 263 | + </DialogTitle> |
| 264 | + </Grid> |
| 265 | + <Grid item> |
| 266 | + <DialogActions> |
| 267 | + <Button |
| 268 | + onClick={handleThankYouSubmit} |
| 269 | + variant="contained" |
| 270 | + className={classes.submitButton} |
| 271 | + > |
| 272 | + <Typography |
| 273 | + variant="button" |
| 274 | + className={classes.btnText} |
| 275 | + > |
| 276 | + <ArrowBack className={classes.arrowForward} /> Add another link |
| 277 | + </Typography> |
| 278 | + </Button> |
| 279 | + </DialogActions> |
| 280 | + </Grid> |
| 281 | + </Grid> |
| 282 | + </Dialog> |
| 283 | + </Box> |
| 284 | + </Box> |
| 285 | + </> |
| 286 | + ); |
| 287 | +} |
0 commit comments