|
| 1 | +import React, { useState, Fragment, useEffect } from 'react'; |
| 2 | +import Button from '@mui/material/Button'; |
| 3 | +import Dialog from '@mui/material/Dialog'; |
| 4 | +import DialogActions from '@mui/material/DialogActions'; |
| 5 | +import DialogContent from '@mui/material/DialogContent'; |
| 6 | +import DialogTitle from '@mui/material/DialogTitle'; |
| 7 | +import FormGroup from '@mui/material/FormGroup'; |
| 8 | + |
| 9 | +import { Autocomplete } from '@mui/material'; |
| 10 | +import Chip from '@mui/material/Chip'; |
| 11 | +import { Tag } from '../../../models/schemas/Tag'; |
| 12 | +import { debug } from '../../../services/logger_services'; |
| 13 | +import { db } from '../../../models/db/dexieDb'; |
| 14 | +import { |
| 15 | + hideLoader, |
| 16 | + processNotification, |
| 17 | + showLoader, |
| 18 | +} from '../../../utilities/loaders'; |
| 19 | +import { StyledTextField } from '../../inputs/StyledTextField'; |
| 20 | +import { Configuration } from '../../../models/schemas/Configuration'; |
| 21 | +import { getUtcNow } from '../../../utilities/transformers'; |
| 22 | +import { Rapport } from '../../../models/schemas/Rapport'; |
| 23 | + |
| 24 | + |
| 25 | +export default function AddTagsMultiplFormDialog(props) { |
| 26 | + const { refreshRows, rapports } = props; |
| 27 | + const [open, setOpen] = React.useState(false); |
| 28 | + |
| 29 | + const [tags, setTags] = useState([]); |
| 30 | + const [userAddedTags, setUserAddedTags] = useState([]); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + setOpen(props.isOpen); |
| 34 | + }, [props.isOpen]); |
| 35 | + |
| 36 | + const handleClose = () => { |
| 37 | + setOpen(false); |
| 38 | + props.setIsOpen(false); |
| 39 | + }; |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + async function fetchData() { |
| 43 | + setTags(await db.tag.toArray()); |
| 44 | + } |
| 45 | + fetchData(); |
| 46 | + setOpen(props.isOpen); |
| 47 | + }, [props.isOpen]); |
| 48 | + |
| 49 | + |
| 50 | + const handleSave = async () => { |
| 51 | + try { |
| 52 | + showLoader(); |
| 53 | + const userTags = [...new Set(userAddedTags)].map((t) => new Tag(t)); |
| 54 | + await db.tag.bulkPut(userTags); |
| 55 | + for(const rapport of rapports){ |
| 56 | + const tagNames = rapport.tags.map(rt => rt.name).concat(userTags.map(ut => ut.name)) |
| 57 | + rapport.tags = [...new Set(tagNames)].map((t) => new Tag(t)); |
| 58 | + await Rapport.put(rapport); |
| 59 | + }; |
| 60 | + |
| 61 | + const configuration = await Configuration.getConfiguration(); |
| 62 | + configuration.updatedOn = getUtcNow(); |
| 63 | + await Configuration.setConfiguration(configuration); |
| 64 | + refreshRows(); |
| 65 | + processNotification({ |
| 66 | + title: 'Tag(s) Added', |
| 67 | + message: `Tag(s) have been added.`, |
| 68 | + type: 'success', |
| 69 | + }); |
| 70 | + } catch (e) { |
| 71 | + debug(e.toString()); |
| 72 | + processNotification({ |
| 73 | + title: 'Tag(s) Add Error', |
| 74 | + message: e.toString(), |
| 75 | + type: 'danger', |
| 76 | + }); |
| 77 | + } finally { |
| 78 | + setOpen(false); |
| 79 | + props.setIsOpen(false); |
| 80 | + hideLoader(); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <Fragment> |
| 86 | + <Dialog |
| 87 | + open={open} |
| 88 | + onClose={handleClose} |
| 89 | + aria-labelledby="form-dialog-title" |
| 90 | + > |
| 91 | + <DialogTitle id="form-dialog-title">Add Tag(s)</DialogTitle> |
| 92 | + <DialogContent> |
| 93 | + <form noValidate> |
| 94 | + <FormGroup> |
| 95 | + <Autocomplete |
| 96 | + sx={{ pt: 1 }} |
| 97 | + multiple |
| 98 | + id="tags" |
| 99 | + name="tags" |
| 100 | + defaultValue={[]} |
| 101 | + options={tags?.map((tag) => tag.name.toLowerCase())} |
| 102 | + freeSolo |
| 103 | + renderTags={(value, getTagProps) => |
| 104 | + value.map((option, index) => { |
| 105 | + const { key, ...tagProps } = getTagProps({ index }); |
| 106 | + return ( |
| 107 | + <Chip |
| 108 | + variant="outlined" |
| 109 | + label={option} |
| 110 | + key={key} |
| 111 | + {...tagProps} |
| 112 | + /> |
| 113 | + ); |
| 114 | + }) |
| 115 | + } |
| 116 | + renderInput={(params) => ( |
| 117 | + <StyledTextField |
| 118 | + {...params} |
| 119 | + label="Assign Tags.." |
| 120 | + helperText={ |
| 121 | + 'PRESS ENTER to add a tag. Multiple tags can set.' |
| 122 | + } |
| 123 | + /> |
| 124 | + )} |
| 125 | + onChange={(event, newValue) => { |
| 126 | + setUserAddedTags(newValue); |
| 127 | + }} |
| 128 | + /> |
| 129 | + </FormGroup> |
| 130 | + </form> |
| 131 | + </DialogContent> |
| 132 | + <DialogActions> |
| 133 | + <Button onClick={handleClose} variant={'contained'} color={'cancel'}> |
| 134 | + Cancel |
| 135 | + </Button> |
| 136 | + <Button onClick={handleSave} color="primary" variant={'contained'}> |
| 137 | + Save |
| 138 | + </Button> |
| 139 | + </DialogActions> |
| 140 | + </Dialog> |
| 141 | + </Fragment> |
| 142 | + ); |
| 143 | +} |
0 commit comments