|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | + |
| 3 | +import { Button, Grid } from '@mui/material'; |
| 4 | +import NotificationsIcon from '@mui/icons-material/Notifications'; |
| 5 | +import Badge from '@mui/material/Badge'; |
| 6 | +import Dialog from '@mui/material/Dialog'; |
| 7 | +import DialogTitle from '@mui/material/DialogTitle'; |
| 8 | +import DialogContent from '@mui/material/DialogContent'; |
| 9 | +import { Refresh } from '@mui/icons-material'; |
| 10 | + |
| 11 | +import { styled } from '@mui/material/styles'; |
| 12 | +import Paper from '@mui/material/Paper'; |
| 13 | +import Communication from './Communication'; |
| 14 | + |
| 15 | +const CommunicationsDialog = props => { |
| 16 | + const { client, token } = props; |
| 17 | + const [state, setState] = useState({ |
| 18 | + client: client, |
| 19 | + token: token, |
| 20 | + initialLoad: true, |
| 21 | + communicationCount: 0, |
| 22 | + communications: [], |
| 23 | + open: false |
| 24 | + }); |
| 25 | + |
| 26 | + const debugLog = message => { |
| 27 | + console.log('CommunicationsDialog: ' + message); |
| 28 | + }; |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + // reload on page load and dialog open |
| 32 | + if (state.initialLoad) { |
| 33 | + setState(prevState => ({ ...prevState, initialLoad: false })); |
| 34 | + getCommunications(); |
| 35 | + } |
| 36 | + |
| 37 | + const interval = setInterval(() => { |
| 38 | + // page load... |
| 39 | + getCommunications(); |
| 40 | + }, 1000 * 5); // reload every 5 seconds |
| 41 | + |
| 42 | + return () => clearInterval(interval); |
| 43 | + }); |
| 44 | + |
| 45 | + const getCommunications = () => { |
| 46 | + if (state.client) { |
| 47 | + // try to read communications from FHIR server |
| 48 | + state.client |
| 49 | + .request(`Communication?recipient=${props.token?.userId}`, { |
| 50 | + graph: false, |
| 51 | + flat: true |
| 52 | + }) |
| 53 | + .then(bundle => { |
| 54 | + loadCommunications(bundle); |
| 55 | + }); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + const deleteCommunication = id => { |
| 60 | + debugLog('deleteCommunication: ' + id); |
| 61 | + if (id) { |
| 62 | + state.client.delete(`Communication/${id}`).then(() => { |
| 63 | + debugLog(`Deleted communication: ${id}`); |
| 64 | + getCommunications(); |
| 65 | + }); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + const loadCommunications = bundle => { |
| 70 | + let count = bundle.length; |
| 71 | + setState(prevState => ({ ...prevState, communicationCount: count, communications: bundle })); |
| 72 | + }; |
| 73 | + |
| 74 | + const handleClose = () => { |
| 75 | + setState(prevState => ({ ...prevState, open: false })); |
| 76 | + }; |
| 77 | + |
| 78 | + const Item = styled(Paper)(({ theme }) => ({ |
| 79 | + backgroundColor: theme.palette.mode === 'dark' ? '#1A2027' : '#EDF6FF', |
| 80 | + ...theme.typography.body2, |
| 81 | + padding: theme.spacing(1), |
| 82 | + textAlign: 'left', |
| 83 | + color: theme.palette.text.secondary |
| 84 | + })); |
| 85 | + |
| 86 | + const renderCommunications = () => { |
| 87 | + return ( |
| 88 | + <Grid container spacing={2} sx={{ mt: 2 }}> |
| 89 | + {state.communications.map(communication => { |
| 90 | + return ( |
| 91 | + <Grid item xs={12} sm={12}> |
| 92 | + <Item> |
| 93 | + <Communication |
| 94 | + communication={communication} |
| 95 | + deleteCommunication={deleteCommunication} |
| 96 | + /> |
| 97 | + </Item> |
| 98 | + </Grid> |
| 99 | + ); |
| 100 | + })} |
| 101 | + </Grid> |
| 102 | + ); |
| 103 | + }; |
| 104 | + |
| 105 | + return ( |
| 106 | + <span> |
| 107 | + <span |
| 108 | + onClick={() => { |
| 109 | + setState(prevState => ({ ...prevState, open: true, initialLoad: true })); |
| 110 | + }} |
| 111 | + > |
| 112 | + <Badge badgeContent={state.communicationCount} color="primary"> |
| 113 | + <NotificationsIcon sx={{ fontSize: 26, verticalAlign: 'middle' }} /> |
| 114 | + </Badge> |
| 115 | + </span> |
| 116 | + <Dialog fullWidth maxWidth="md" onClose={handleClose} open={state.open}> |
| 117 | + <DialogTitle> |
| 118 | + <Grid container> |
| 119 | + <Grid item xs={10}> |
| 120 | + <NotificationsIcon sx={{ fontSize: 26, verticalAlign: 'middle' }} /> |
| 121 | + Communications ({state.communicationCount}) |
| 122 | + </Grid> |
| 123 | + <Grid item xs={2}> |
| 124 | + <Button |
| 125 | + variant="contained" |
| 126 | + startIcon={<Refresh />} |
| 127 | + onClick={() => { |
| 128 | + getCommunications(); |
| 129 | + }} |
| 130 | + > |
| 131 | + Refresh |
| 132 | + </Button> |
| 133 | + </Grid> |
| 134 | + </Grid> |
| 135 | + </DialogTitle> |
| 136 | + |
| 137 | + <DialogContent>{renderCommunications()}</DialogContent> |
| 138 | + </Dialog> |
| 139 | + </span> |
| 140 | + ); |
| 141 | +}; |
| 142 | + |
| 143 | +export default CommunicationsDialog; |
0 commit comments