forked from HospitalRun/hospitalrun-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useReportIncident.tsx
35 lines (28 loc) · 963 Bytes
/
useReportIncident.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
import isEmpty from 'lodash/isEmpty'
import { queryCache, useMutation } from 'react-query'
import shortid from 'shortid'
import IncidentRepository from '../../shared/db/IncidentRepository'
import Incident from '../../shared/model/Incident'
import validateIncident from '../util/validate-incident'
const getIncidentCode = (): string => `I-${shortid.generate()}`
export function reportIncident(incident: Incident): Promise<Incident> {
const error = validateIncident(incident)
if (isEmpty(error)) {
const updatedIncident: Incident = {
...incident,
code: getIncidentCode(),
status: 'reported',
reportedOn: new Date(Date.now()).toISOString(),
}
return IncidentRepository.save(updatedIncident)
}
throw error
}
export default function useReportIncident() {
return useMutation(reportIncident, {
onSuccess: async () => {
await queryCache.invalidateQueries('incidents')
},
throwOnError: true,
})
}