|
| 1 | +import React, { useCallback, useMemo, useState } from 'react'; |
| 2 | +import { useSelector } from 'react-redux'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | +import { Box, Flex, Text } from 'theme-ui'; |
| 5 | +import { colors as vizColors } from '@tidepool/viz'; |
| 6 | +import KeyboardArrowLeftIcon from '@material-ui/icons/KeyboardArrowLeft'; |
| 7 | +import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown'; |
| 8 | + |
| 9 | +import Table from '../../../../components/elements/Table'; |
| 10 | + |
| 11 | +import { PatientCell } from '../Cells'; |
| 12 | +import { DexcomConnectionStatusCell, DaysSinceLastDataCell, MoreMenuCell } from './Cells'; |
| 13 | +import TagListCell from '../../components/TagListCell'; |
| 14 | +import EmptyContentNode from '../EmptyContentNode'; |
| 15 | +import useTideReportNoDataPatients from './useTideReportNoDataPatients'; |
| 16 | +import EditPatientDialogController from './EditPatientDialogController'; |
| 17 | +import DataConnectionsModalController from './DataConnectionsModalController'; |
| 18 | +import { useGetPatientFromClinicQuery } from './tideDashboardLegacyApi'; |
| 19 | +import Icon from '../../../../components/elements/Icon'; |
| 20 | + |
| 21 | +const usePatientFromClinic = (patientId) => { |
| 22 | + const selectedClinicId = useSelector(state => state.blip.selectedClinicId); |
| 23 | + |
| 24 | + const { data: patient } = useGetPatientFromClinicQuery( |
| 25 | + { clinicId: selectedClinicId, patientId }, |
| 26 | + { skip: !selectedClinicId || !patientId } |
| 27 | + ); |
| 28 | + |
| 29 | + return patient; |
| 30 | +}; |
| 31 | + |
| 32 | +const DataIssues = ({ api }) => { |
| 33 | + const { t } = useTranslation(); |
| 34 | + const { patients } = useTideReportNoDataPatients(); |
| 35 | + |
| 36 | + const [activePatientId, setActivePatientId] = useState(null); |
| 37 | + const [isAccordionOpen, setIsAccordionOpen] = useState(false); |
| 38 | + const [isEditPatientDialogOpen, setIsEditPatientDialogOpen] = useState(false); |
| 39 | + const [isDataConnectionsModalOpen, setIsDataConnectionsModalOpen] = useState(false); |
| 40 | + |
| 41 | + const activePatient = usePatientFromClinic(activePatientId); |
| 42 | + |
| 43 | + const handleOpenEditPatientDialog = useCallback((patientId) => { |
| 44 | + setActivePatientId(patientId); |
| 45 | + setIsEditPatientDialogOpen(true); |
| 46 | + }, []); |
| 47 | + |
| 48 | + const handleOpenDataConnectionsModal = (patientId) => { |
| 49 | + setActivePatientId(patientId); |
| 50 | + setIsDataConnectionsModalOpen(true); |
| 51 | + }; |
| 52 | + |
| 53 | + const handleCloseModals = () => { |
| 54 | + setIsEditPatientDialogOpen(false); |
| 55 | + setIsDataConnectionsModalOpen(false); |
| 56 | + setActivePatientId(null); |
| 57 | + }; |
| 58 | + |
| 59 | + const columns = useMemo(() => ([ |
| 60 | + { |
| 61 | + title: t('Patient Details'), |
| 62 | + field: 'fullName', |
| 63 | + align: 'left', |
| 64 | + render: patient => <PatientCell patient={patient} />, |
| 65 | + }, |
| 66 | + { |
| 67 | + title: t('Dexcom Connection Status'), |
| 68 | + field: 'dexcomConnectionStatus', |
| 69 | + align: 'left', |
| 70 | + render: patient => <DexcomConnectionStatusCell patient={patient} onOpenDataConnectionsModal={handleOpenDataConnectionsModal} />, |
| 71 | + }, |
| 72 | + { |
| 73 | + title: t('Days Since Last Data'), |
| 74 | + field: 'daysSinceLastData', |
| 75 | + align: 'center', |
| 76 | + render: patient => <DaysSinceLastDataCell patient={patient} />, |
| 77 | + }, |
| 78 | + { |
| 79 | + title: t('Tags'), |
| 80 | + field: 'tags', |
| 81 | + align: 'center', |
| 82 | + render: patient => <TagListCell patient={patient} />, |
| 83 | + }, |
| 84 | + { |
| 85 | + title: '', |
| 86 | + field: 'moreMenu', |
| 87 | + align: 'center', |
| 88 | + render: patient => ( |
| 89 | + <MoreMenuCell |
| 90 | + patient={patient} |
| 91 | + onOpenEditPatientDialog={handleOpenEditPatientDialog} |
| 92 | + onOpenDataConnectionsModal={handleOpenDataConnectionsModal} |
| 93 | + /> |
| 94 | + ), |
| 95 | + }, |
| 96 | + ]), [t, handleOpenEditPatientDialog, handleOpenDataConnectionsModal]); |
| 97 | + |
| 98 | + if (!patients?.length) return null; |
| 99 | + |
| 100 | + return ( |
| 101 | + <Box id="tide-dashboard-data-issues" my={5}> |
| 102 | + <Flex |
| 103 | + onClick={() => setIsAccordionOpen(isOpen => !isOpen)} |
| 104 | + className="data-issues-section-label" |
| 105 | + sx={{ |
| 106 | + justifyContent: 'space-between', |
| 107 | + transition: 'all 0.2s ease', |
| 108 | + borderBottom: '1px solid', |
| 109 | + borderColor: isAccordionOpen ? vizColors.white : vizColors.gray10, |
| 110 | + padding: 3, |
| 111 | + color: vizColors.blueGray50, |
| 112 | + '&:hover': { cursor: 'pointer', borderColor: vizColors.gray30 }, |
| 113 | + }} |
| 114 | + > |
| 115 | + <Text sx={{ fontSize: 2 }}>{t('Device Issues ({{count}})', { count: patients.length })}</Text> |
| 116 | + |
| 117 | + <Icon |
| 118 | + variant="static" |
| 119 | + icon={isAccordionOpen ? KeyboardArrowDownIcon : KeyboardArrowLeftIcon} |
| 120 | + label={isAccordionOpen ? t('Data Issues Close Icon') : t('Data Issues Open Icon')} |
| 121 | + title={isAccordionOpen ? t('Data Issues Close Icon') : t('Data Issues Open Icon')} |
| 122 | + sx={{ fontSize: 4 }} |
| 123 | + /> |
| 124 | + </Flex> |
| 125 | + |
| 126 | + <Box sx={{ |
| 127 | + display: 'grid', |
| 128 | + gridTemplateRows: isAccordionOpen ? 'minmax(0, 1fr)' : 'minmax(0, 0fr)', |
| 129 | + transition: 'grid-template-rows 0.5s ease', |
| 130 | + }}> |
| 131 | + <Box sx={{ minHeight: 0, overflow: 'hidden' }}> |
| 132 | + <Table |
| 133 | + id="tideDashboardDataIssuesTable" |
| 134 | + variant="condensed" |
| 135 | + label="tideDashboardDataIssuesTable" |
| 136 | + columns={columns} |
| 137 | + data={patients} |
| 138 | + emptyContentNode={<EmptyContentNode />} |
| 139 | + containerProps={{ sx: { containerType: 'inline-size' } }} |
| 140 | + /> |
| 141 | + </Box> |
| 142 | + </Box> |
| 143 | + |
| 144 | + <EditPatientDialogController |
| 145 | + api={api} |
| 146 | + isOpen={isEditPatientDialogOpen} |
| 147 | + patient={activePatient} |
| 148 | + onClose={handleCloseModals} |
| 149 | + /> |
| 150 | + |
| 151 | + <DataConnectionsModalController |
| 152 | + isOpen={isDataConnectionsModalOpen} |
| 153 | + patient={activePatient} |
| 154 | + onClose={handleCloseModals} |
| 155 | + /> |
| 156 | + </Box> |
| 157 | + ); |
| 158 | +}; |
| 159 | + |
| 160 | +export default DataIssues; |
0 commit comments