-
Notifications
You must be signed in to change notification settings - Fork 0
/
Patient.js
294 lines (266 loc) · 10.1 KB
/
Patient.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import React from 'react';
import './Patient.less';
import NavFrontendSpinner from 'nav-frontend-spinner';
import { Textarea, Input, Label } from 'nav-frontend-skjema';
import { Hovedknapp } from 'nav-frontend-knapper';
import { MuiPickersUtilsProvider, KeyboardDatePicker } from '@material-ui/pickers';
import moment from 'moment';
import 'moment/locale/nb';
import MomentUtils from '@date-io/moment';
import AlertStripe from 'nav-frontend-alertstriper';
import QuestionnaireResponseTemplate from '../QuestionnaireResponseTemplate.json';
import FhirClientContext from '../FhirClientContext';
moment.locale('nb'); // Set calendar to be norwegian (bokmaal)
const axios = require('axios');
const QUESTIONNAIRE_ID = 235192;
function PatientName({ name = [] }) {
const entry = name.find((nameRecord) => nameRecord.use === 'official') || name[0];
if (!entry) {
return <h3>Navn: Navn ikke funnet</h3>;
}
return (
<div className="name-wrapper">
<Label htmlFor="name">Navn:</Label>
<Input id="name" disabled value={`${entry.given.join(' ')} ${entry.family}`} />
</div>
);
}
function PatientSocialSecurityNumber({ identifier = [] }) {
const socialSecurityNumber = identifier.find((sb) => sb.system === 'http:https://hl7.org/fhir/sid/us-ssn').value;
if (!socialSecurityNumber) {
return <p>Fødelsnummer ikke funnet</p>;
}
return (
<div className="birthnr-wrapper">
<Label htmlFor="birthnr-input">Fødelsnummer:</Label>
<Input id="birthnr-input" disabled value={socialSecurityNumber} />
</div>
);
}
export default class Patient extends React.Component {
static contextType = FhirClientContext;
constructor(props) {
super(props);
this.state = {
loading: true,
patient: null,
value: '',
startDate: null,
endDate: null,
responseID: null,
error: null,
sucessfullSave: false,
sucessfullSend: false,
};
}
async componentDidMount() {
const { client } = this.context;
this._loader = await client.patient
.read()
.then((patient) => {
this.setState({ patient, loading: false, error: false });
this.formData();
})
.catch((error) => {
this.setState({ error, loading: false });
});
}
formData = () => {
const fhirclient = this.context.client;
fhirclient.request(`http:https://launch.smarthealthit.org/v/r3/fhir/QuestionnaireResponse/_search?questionnaire=${QUESTIONNAIRE_ID}&patient=${fhirclient.patient.id}&status=in-progress`)
.then((result) => {
if (result.total === 0) { return; }
this.setState({ responseID: result.entry[0].resource.id });
if (typeof (result.entry[0].resource.item[4].answer) !== 'undefined') {
this.setState({ value: result.entry[0].resource.item[4].answer[0].valueString });
}
if (typeof (result.entry[0].resource.item[2].answer) !== 'undefined') {
this.setState({ startDate: result.entry[0].resource.item[2].answer[0].valueString });
}
if (typeof (result.entry[0].resource.item[3].answer) !== 'undefined') {
this.setState({ endDate: result.entry[0].resource.item[3].answer[0].valueString });
}
}).catch((e) => {
console.log('Error loading formData: ', e);
});
}
// Gets QuestionnaireResponseTemplate.json and fills out with current patients information,
// before returning the form.
convertToQuestionnaire = (status) => {
const fullPatientName = `${this.state.patient.name[0].given
.join(' ')} ${this.state.patient.name[0].family}`;
const socialSecurityNumber = this.state.patient.identifier
.find((sb) => sb.system === 'http:https://hl7.org/fhir/sid/us-ssn').value;
const responseForm = QuestionnaireResponseTemplate;
responseForm.subject.reference = `Patient/${this.state.patient.id}`;
responseForm.subject.display = fullPatientName;
responseForm.item[0].answer[0].valueString = fullPatientName;
responseForm.item[1].answer[0].valueString = socialSecurityNumber;
responseForm.item[2].answer[0].valueString = this.state.startDate ? this.state.startDate._d : '';
responseForm.item[3].answer[0].valueString = this.state.endDate ? this.state.endDate._d : '';
responseForm.item[4].answer[0].valueString = this.state.value;
// Gets the fhirUser-ID of the practitioner and fills it in the form
responseForm.author.reference = this.context.client.user.fhirUser;
// Sets the status of the QuestionnaireResponse-form to the functions argument
responseForm.status = status;
return responseForm;
}
// Function for saving the information in our form to FHIR
saveAndSendToFHIR = (status) => {
const filledResponse = this.convertToQuestionnaire(status);
const fhirclient = this.context.client;
const headers = {
'Content-Type': 'application/fhir+json',
Accept: '*/*',
};
let options;
// Patient has no existing QuestionnairyResponse and a new one is created
if (this.state.responseID === null) {
options = {
url: 'http:https://launch.smarthealthit.org/v/r3/fhir/QuestionnaireResponse',
body: JSON.stringify(filledResponse),
headers,
method: 'POST',
};
} else {
// Patient has previously excisting QuestionnairyResponse
filledResponse.id = this.state.responseID;
options = {
url: `http:https://launch.smarthealthit.org/v/r3/fhir/QuestionnaireResponse/${this.state.responseID}`,
body: JSON.stringify(filledResponse),
headers,
method: 'PUT',
};
}
return fhirclient.request(options);
}
// Function for saving patient information form to FHIR with status in progress
handleSave = (event, status) => {
event.preventDefault();
this.saveAndSendToFHIR(status)
.then((response) => {
this.setState({ responseID: response.id });
if (status === 'in-progress') {
this.setState({ sucessfullSave: true });
}
}).catch((e) => {
console.log('Error loading formData: ', e);
});
}
// Function for saving patient information form to FHIR with status completed
// and sending it to Kafka-stream
handleSubmit = (event, status) => {
this.handleSave(event, status);
const token = this.context.client.state.tokenResponse.access_token;
const ID = this.state.responseID;
const config = {
headers: {
Authorization: `Bearer ${token}`,
},
};
axios.post('http:https://localhost:8081/send-application', ID, config)
.then((res) => {
if (res.status === 200) {
this.setState({
sucessfullSend: true,
sucessfullSave: false,
});
} else {
console.log('Error sending information to backend, status code:', res.status);
}
});
}
handleChange = (event) => {
this.setState({ value: event.target.value });
// Removes popup
this.setState({
sucessfullSave: false,
sucessfullSend: false,
});
}
/* eslint-disable react/jsx-props-no-spreading */
render() {
const { error, loading, patient } = this.state;
if (loading) {
return <NavFrontendSpinner />;
}
if (error) {
return <p>{error.message}</p>;
}
let A;
if (this.state.sucessfullSave) {
A = (
<AlertStripe type="suksess">
Skjemaet ble lagret!
</AlertStripe>
);
}
if (this.state.sucessfullSend) {
A = (
<AlertStripe type="suksess">
Skjemaet ble lagret og sendt!
</AlertStripe>
);
}
return (
<div className="form-wrapper">
<h1> Erklæring om pleiepenger</h1>
<div className="banner-wrapper">
<PatientName name={patient.name} />
<PatientSocialSecurityNumber identifier={patient.identifier} />
</div>
<form className="patientform" onSubmit={(e) => this.handleSubmit(e, 'completed')}>
<Textarea className="tekstfelt" value={this.state.value} onChange={this.handleChange} maxLength={0} disabled={this.state.sucessfullSend} />
<div className="datepicker-wrapper">
<MuiPickersUtilsProvider libInstance={moment} utils={MomentUtils} locale="nb">
<KeyboardDatePicker
className="datepicker"
disableToolbar
variant="inline"
format="DD. MMMM yyyy"
id="startdate-picker"
label="Fra dato"
maxDate={this.state.endDate ? this.state.endDate : undefined}
maxDateMessage="Starten av perioden kan ikke være senere enn slutten av perioden"
invalidDateMessage="Ugyldig datoformat"
value={this.state.startDate}
onChange={(d) => this.setState({
startDate: d,
sucessfullSave: false,
sucessfullSend: false,
})}
disabled={this.state.sucessfullSend}
/>
<KeyboardDatePicker
className="datepicker"
disableToolbar
variant="inline"
format="DD. MMMM yyyy"
id="enddate-picker"
label="Til dato"
minDate={this.state.startDate ? this.state.startDate : undefined}
minDateMessage="Slutten av perioden kan ikke være tidligere enn starten av perioden"
invalidDateMessage="Ugyldig datoformat"
value={this.state.endDate}
onChange={(d) => this.setState({
endDate: d,
sucessfullSave: false,
sucessfullSend: false,
})}
disabled={this.state.sucessfullSend}
/>
</MuiPickersUtilsProvider>
</div>
<div id="popup" aria-live="polite">
{A}
</div>
<br />
<div className="button-wrapper">
<Hovedknapp className="button" onClick={(e) => this.handleSave(e, 'in-progress')}>Lagre</Hovedknapp>
<Hovedknapp className="button" htmlType="submit" disabled={this.state.sucessfullSend}>Send</Hovedknapp>
</div>
</form>
</div>
);
}
}