-
Notifications
You must be signed in to change notification settings - Fork 194
/
appointment-services.component.tsx
183 lines (174 loc) · 7.02 KB
/
appointment-services.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React from 'react';
import { Button, ButtonSet, Dropdown, Layer, SelectItem, TextInput, TimePicker, TimePickerSelect } from '@carbon/react';
import { useTranslation } from 'react-i18next';
import { Form, Formik, type FormikHelpers } from 'formik';
import { validationSchema } from './appointment-services-validation';
import { useAppointmentServices } from './appointment-services-hook';
import { showNotification, showToast, useLocations } from '@openmrs/esm-framework';
import type { AppointmentService } from '../../types';
import { closeOverlay } from '../../hooks/useOverlay';
import styles from './appointment-services.scss';
import { appointmentLocationTagName } from '../../constants';
interface AppointmentServicesProps {}
const AppointmentServices: React.FC<AppointmentServicesProps> = () => {
const { t } = useTranslation();
const { appointmentServiceInitialValue, addNewAppointmentService } = useAppointmentServices();
const locations = useLocations(appointmentLocationTagName);
const handleSubmit = async (values: AppointmentService, helpers: FormikHelpers<AppointmentService>) => {
const payload = {
name: values.name,
startTime: values.startTime.concat(':00'),
endTime: values.endTime.concat(':00'),
durationMins: values.durationMins,
color: values.color,
locationUuid: values.location.uuid,
};
addNewAppointmentService(payload).then(
({ status }) => {
if (status === 200) {
showToast({
critical: true,
kind: 'success',
description: t('appointmentServiceCreate', 'Appointment service created successfully'),
title: t('appointmentService', 'Appointment service'),
});
closeOverlay();
}
},
(error) => {
showNotification({
title: t('errorCreatingAppointmentService', 'Error creating appointment service'),
kind: 'error',
critical: true,
description: error?.message,
});
},
);
};
return (
<Formik
onSubmit={handleSubmit}
isInitialValid={false}
validationSchema={validationSchema}
initialValues={appointmentServiceInitialValue}>
{(props) => {
return (
<Form onSubmit={props.handleSubmit} className={styles.appointmentServiceContainer}>
<p className={styles.formTitle}>{t('createAppointmentService', 'Create appointment service')}</p>
<Layer>
<TextInput
id="name"
invalidText={t(props.errors.name)}
labelText={t('appointmentServiceName', 'Appointment service name')}
placeholder={t('appointmentServiceName', 'Appointment service name')}
invalid={!!(props.touched && props.errors.name)}
onChange={props.handleChange}
value={props.values.name}
name="name"
onBlue={props.handleBlur}
/>
</Layer>
<Layer>
<TimePicker
className={styles.timePickerInput}
invalid={!!(props.touched && props.errors.startTime)}
pattern="([\d]+:[\d]{2})"
name="startTime"
value={props.values.startTime}
onChange={props.handleChange}
labelText={t('startTime', 'Start Time')}
id="start-time-picker">
<TimePickerSelect
name="startTimeTimeFormat"
onChange={props.handleChange}
value={props.values.startTimeTimeFormat}
invalid={!!(props.touched && props.errors.startTimeTimeFormat)}
id="start-time-picker"
labelText={t('time', 'Time')}
aria-label={t('time', 'Time')}>
<SelectItem value="AM" text="AM" />
<SelectItem value="PM" text="PM" />
</TimePickerSelect>
</TimePicker>
</Layer>
<Layer>
<TimePicker
invalid={!!(props.touched && props.errors.endTime)}
className={styles.timePickerInput}
pattern="([\d]+:[\d]{2})"
value={props.values.endTime}
name="endTime"
onChange={props.handleChange}
labelText={t('endTime', 'End Time')}
id="end-time-picker">
<TimePickerSelect
name="endTimeTimeFormat"
onChange={props.handleChange}
id="end-time-picker"
value={props.values.endTimeTimeFormat}
labelText={t('time', 'Time')}
aria-label={t('time', 'Time')}>
<SelectItem value="AM" text="AM" />
<SelectItem value="PM" text="PM" />
</TimePickerSelect>
</TimePicker>
</Layer>
<Layer>
<TextInput
id="durationMins"
invalidText={props.errors.durationMins}
labelText={t('durationMins', 'Duration min')}
placeholder={t('durationMins', 'Duration min')}
invalid={!!(props.touched && props.errors.durationMins)}
onChange={props.handleChange}
value={props.values.durationMins}
name="durationMins"
/>
</Layer>
<Layer>
<Dropdown
id="default"
titleText={t('selectLocation', 'Select location')}
label={t('selectLocation', 'Select location')}
items={locations}
itemToString={(item) => (item ? item.display : '')}
selectedItem={props.values.location}
invalid={!!(props.touched && props.errors.location?.uuid)}
name="location"
onChange={({ selectedItem }) => props.setValues({ ...props.values, location: selectedItem })}
/>
</Layer>
<Layer>
<TextInput
invalid={!!(props.touched && props.errors.color)}
onChange={props.handleChange}
invalidText={props.errors.color}
labelText={t('appointmentColor', 'Appointment color')}
type="color"
name="color"
/>
</Layer>
<ButtonSet className={styles.buttonSet}>
<Button
onClick={closeOverlay}
style={{ maxWidth: 'none', width: '50%' }}
className={styles.button}
kind="secondary">
{t('discard', 'Discard')}
</Button>
<Button
disabled={!props.isValid}
style={{ maxWidth: 'none', width: '50%' }}
className={styles.button}
kind="primary"
type="submit">
{t('save', 'Save')}
</Button>
</ButtonSet>
</Form>
);
}}
</Formik>
);
};
export default AppointmentServices;