-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbed_mutations.ts
129 lines (110 loc) · 3.71 KB
/
bed_mutations.ts
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
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import {
CreateBedRequest,
DeleteBedRequest,
GetBedRequest, GetBedsRequest,
UpdateBedRequest
} from '@helpwave/proto-ts/services/tasks_svc/v1/bed_svc_pb'
import { QueryKeys } from '../query_keys'
import { APIServices } from '../../services'
import { getAuthenticatedGrpcMetadata } from '../../authentication/grpc_metadata'
import type { BedWithRoomId } from '../../types/tasks/bed'
import { roomOverviewsQueryKey } from './room_mutations'
export const useBedQuery = (bedId: string | undefined) => {
return useQuery({
queryKey: [QueryKeys.beds],
enabled: !!bedId,
queryFn: async () => {
const req = new GetBedRequest()
if (bedId) {
req.setId(bedId)
}
const res = await APIServices.bed.getBed(req, getAuthenticatedGrpcMetadata())
const bed: BedWithRoomId = {
id: res.getId(),
name: res.getName(),
roomId: res.getRoomId()
}
return bed
},
})
}
export const useBedsQuery = (roomId?: string) => {
return useQuery({
queryKey: [QueryKeys.beds, roomId ?? 'all'],
queryFn: async () => {
const req = new GetBedsRequest()
if (roomId) {
req.setRoomId(roomId)
}
const res = await APIServices.bed.getBeds(req, getAuthenticatedGrpcMetadata())
const beds: BedWithRoomId[] = res.getBedsList().map(bed => ({
id: bed.getId(),
name: bed.getName(),
roomId: bed.getRoomId()
}))
return beds
},
})
}
export const useBedCreateMutation = () => {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async (bed: BedWithRoomId) => {
const req = new CreateBedRequest()
req.setRoomId(bed.roomId)
req.setName(bed.name)
const res = await APIServices.bed.createBed(req, getAuthenticatedGrpcMetadata())
if (!res.toObject()) {
console.log('error in BedCreate')
}
return { id: res.getId(), name: bed.name }
},
onSuccess: () => {
queryClient.refetchQueries([QueryKeys.beds]).catch(console.error)
queryClient.refetchQueries([QueryKeys.rooms, roomOverviewsQueryKey]).catch(console.error)
queryClient.refetchQueries([QueryKeys.wards]).catch(console.error)
},
})
}
export const useBedUpdateMutation = () => {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async (bed: BedWithRoomId) => {
const req = new UpdateBedRequest()
req.setId(bed.id)
req.setName(bed.name)
req.setRoomId(bed.roomId)
const res = await APIServices.bed.updateBed(req, getAuthenticatedGrpcMetadata())
const obj = res.toObject() // TODO: what is the type of this?
if (!obj) {
throw new Error('error in BedUpdate')
}
return obj
},
onSuccess: () => {
queryClient.refetchQueries([APIServices.bed]).catch(console.error)
queryClient.refetchQueries([QueryKeys.rooms, roomOverviewsQueryKey]).catch(console.error)
},
})
}
export const useBedDeleteMutation = () => {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async (bedId: string) => {
const req = new DeleteBedRequest()
req.setId(bedId)
const res = await APIServices.bed.deleteBed(req, getAuthenticatedGrpcMetadata())
const obj = res.toObject() // TODO: what is the type of this?
if (!obj) {
throw new Error('error in BedDelete')
}
return obj
},
onSuccess: () => {
queryClient.refetchQueries([APIServices.bed]).catch(console.error)
queryClient.refetchQueries([QueryKeys.rooms, roomOverviewsQueryKey]).catch(console.error)
queryClient.refetchQueries([QueryKeys.wards]).catch(console.error)
},
})
}