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+
41+ } , 1000 * 5 ) // reload every 5 seconds
42+
43+ return ( ) => clearInterval ( interval ) ;
44+
45+ } ) ;
46+
47+ const getCommunications = ( ) => {
48+ if ( state . client ) {
49+ // try to read communications from FHIR server
50+ state . client
51+ . request ( `Communication?recipient=${ props . token ?. userId } ` , {
52+ graph : false ,
53+ flat : true
54+ } )
55+ . then ( bundle => {
56+ loadCommunications ( bundle ) ;
57+ } ) ;
58+ }
59+ }
60+
61+ const deleteCommunication = ( id ) => {
62+ debugLog ( 'deleteCommunication: ' + id ) ;
63+ if ( id ) {
64+ state . client . delete ( `Communication/${ id } ` ) . then ( ( ) => {
65+ debugLog ( `Deleted communication: ${ id } ` ) ;
66+ } ) ;
67+ }
68+ }
69+
70+ const loadCommunications = ( bundle ) => {
71+ let count = bundle . length ;
72+ setState ( prevState => ( { ...prevState , communicationCount : count , communications : bundle } ) ) ;
73+ } ;
74+
75+ const handleClose = ( ) => {
76+ setState ( prevState => ( { ...prevState , open : false } ) ) ;
77+ } ;
78+
79+ const Item = styled ( Paper ) ( ( { theme } ) => ( {
80+ backgroundColor : theme . palette . mode === 'dark' ? '#1A2027' : '#EDF6FF' ,
81+ ...theme . typography . body2 ,
82+ padding : theme . spacing ( 1 ) ,
83+ textAlign : 'left' ,
84+ color : theme . palette . text . secondary ,
85+ } ) ) ;
86+
87+ const renderCommunications = ( ) => {
88+ return (
89+ < Grid container spacing = { 2 } sx = { { mt : 2 } } >
90+ { state . communications . map ( communication => {
91+ return (
92+ < Grid item xs = { 12 } sm = { 12 } >
93+ < Item > < Communication communication = { communication } deleteCommunication = { deleteCommunication } /> </ Item >
94+ </ Grid >
95+ ) ;
96+ } ) }
97+ </ Grid >
98+ ) ;
99+ }
100+
101+ return (
102+ < span >
103+ < span onClick = { ( ) => {
104+ setState ( prevState => ( { ...prevState , open : true , initialLoad : true } ) ) ;
105+ } } >
106+ < Badge badgeContent = { state . communicationCount } color = "primary" >
107+ < NotificationsIcon sx = { { fontSize : 26 , verticalAlign : 'middle' } } />
108+ </ Badge > </ span >
109+ < Dialog fullWidth maxWidth = 'md' onClose = { handleClose } open = { state . open } >
110+ < DialogTitle >
111+ < Grid container >
112+ < Grid item xs = { 10 } >
113+ < NotificationsIcon sx = { { fontSize : 26 , verticalAlign : 'middle' } } />
114+ Communications ({ state . communicationCount } )
115+ </ Grid >
116+ < Grid item xs = { 2 } >
117+ < Button
118+ variant = "contained"
119+ startIcon = { < Refresh /> }
120+ onClick = { ( ) => {
121+ getCommunications ( ) ;
122+ } }
123+ >
124+ Refresh
125+ </ Button >
126+ </ Grid >
127+ </ Grid >
128+ </ DialogTitle >
129+
130+ < DialogContent >
131+ { renderCommunications ( ) }
132+ </ DialogContent >
133+
134+ </ Dialog >
135+ </ span >
136+ ) ;
137+ } ;
138+
139+ export default CommunicationsDialog ;
0 commit comments