-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathworkspace-launcher.component.tsx
130 lines (119 loc) · 4.21 KB
/
workspace-launcher.component.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, { useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { formatDatetime, parseDate, showSnackbar } from '@openmrs/esm-framework';
import { useLaunchWorkspaceRequiringVisit } from '@openmrs/esm-patient-common-lib';
import { Button } from '@carbon/react';
import { type Appointment, type FormFieldInputProps } from '../../../types';
import { isTrue } from '../../../utils/boolean-utils';
import styles from './workspace-launcher.scss';
import { useFormFactory } from '../../../provider/form-factory-provider';
import { DataTable, Table, TableHead, TableRow, TableHeader, TableBody, TableCell } from '@carbon/react';
import { InlineNotification } from '@carbon/react';
const WorkspaceLauncher: React.FC<FormFieldInputProps> = ({ field }) => {
const { t } = useTranslation();
const {
patientAppointments: { addAppointmentForCurrentEncounter },
} = useFormFactory();
const launchWorkspace = useLaunchWorkspaceRequiringVisit(field.questionOptions?.workspaceName);
const handleAfterCreateAppointment = async (appointmentUuid: string) => {
addAppointmentForCurrentEncounter(appointmentUuid);
};
const handleLaunchWorkspace = () => {
if (!launchWorkspace) {
showSnackbar({
title: t('invalidWorkspaceName', 'Invalid workspace name.'),
subtitle: t('invalidWorkspaceNameSubtitle', 'Please provide a valid workspace name.'),
kind: 'error',
isLowContrast: true,
});
}
if (field.meta?.handleAppointmentCreation) {
launchWorkspace({ handleAfterCreateAppointment });
} else {
launchWorkspace();
}
};
return (
!field.isHidden && (
<div>
<div className={styles.label}>{t(field.label)}</div>
<div className={styles.workspaceButton}>
<Button disabled={isTrue(field.readonly)} onClick={handleLaunchWorkspace}>
{t(field.questionOptions?.buttonLabel) ?? t('launchWorkspace', 'Launch Workspace')}
</Button>
</div>
{field.meta?.handleAppointmentCreation && <AppointmentsTable />}
</div>
)
);
};
const AppointmentsTable: React.FC = () => {
const { t } = useTranslation();
const {
patientAppointments: { appointments, errorFetchingAppointments },
} = useFormFactory();
const headers = useMemo(
() => [
{ key: 'startDateTime', header: t('appointmentDatetime', 'Date & time') },
{ key: 'location', header: t('location', 'Location') },
{ key: 'service', header: t('service', 'Service') },
{ key: 'status', header: t('status', 'Status') },
],
[t],
);
const rows = useMemo(
() =>
appointments.map((appointment) => ({
id: appointment.uuid,
startDateTime: formatDatetime(parseDate(appointment.startDateTime), {
mode: 'standard',
}),
location: appointment?.location?.name ? appointment?.location?.name : '——',
service: appointment.service.name,
status: appointment.status,
})),
[appointments],
);
if (errorFetchingAppointments) {
return (
<InlineNotification
kind="error"
title={t('errorFetchingAppointments', 'Error fetching appointments')}
subtitle={errorFetchingAppointments?.message}
lowContrast={false}
/>
);
}
if (rows.length === 0) {
return null;
}
return (
<DataTable rows={rows} headers={headers}>
{({ rows, headers, getTableProps, getHeaderProps, getRowProps, getCellProps }) => (
<Table {...getTableProps()}>
<TableHead>
<TableRow>
{headers.map((header) => (
<TableHeader key={header.key} {...getHeaderProps({ header })}>
{header.header}
</TableHeader>
))}
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow key={row.id} {...getRowProps({ row })}>
{row.cells.map((cell) => (
<TableCell key={cell.id} {...getCellProps({ cell })}>
{cell.value}
</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
)}
</DataTable>
);
};
export default WorkspaceLauncher;