@seamapi/react 4.13.0 → 4.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,104 @@
1
+ import type {
2
+ SeamActionAttemptFailedError,
3
+ SeamActionAttemptTimeoutError,
4
+ SeamHttpApiError,
5
+ } from '@seamapi/http/connect'
6
+ import { NullSeamClientError, useSeamClient } from '@seamapi/react-query'
7
+ import type { ActionAttempt, Device } from '@seamapi/types/connect'
8
+ import {
9
+ useMutation,
10
+ type UseMutationResult,
11
+ useQueryClient,
12
+ } from '@tanstack/react-query'
13
+
14
+ export type UseUnlockData = undefined
15
+
16
+ export type UseUnlockMutationVariables = Pick<Device, 'device_id'> & {
17
+ properties: Required<Pick<Device['properties'], 'locked'>>
18
+ }
19
+
20
+ type UnlockActionAttempt = Extract<
21
+ ActionAttempt,
22
+ { action_type: 'UNLOCK_DOOR' }
23
+ >
24
+
25
+ type MutationError =
26
+ | SeamHttpApiError
27
+ | SeamActionAttemptFailedError<UnlockActionAttempt>
28
+ | SeamActionAttemptTimeoutError<UnlockActionAttempt>
29
+
30
+ interface UseUnlockParams {
31
+ onError?: () => void
32
+ onSuccess?: () => void
33
+ }
34
+
35
+ export function useUnlock(
36
+ params: UseUnlockParams = {}
37
+ ): UseMutationResult<UseUnlockData, MutationError, UseUnlockMutationVariables> {
38
+ const { client } = useSeamClient()
39
+ const queryClient = useQueryClient()
40
+
41
+ return useMutation<UseUnlockData, MutationError, UseUnlockMutationVariables>({
42
+ mutationFn: async (variables) => {
43
+ const {
44
+ device_id: deviceId,
45
+ properties: { locked },
46
+ } = variables
47
+ if (client === null) throw new NullSeamClientError()
48
+ if (locked == null) return
49
+ await client.locks.unlockDoor({ device_id: deviceId })
50
+ },
51
+ onMutate: (variables) => {
52
+ queryClient.setQueryData<Device[]>(['devices', 'list', {}], (devices) => {
53
+ if (devices == null) {
54
+ return devices
55
+ }
56
+
57
+ return devices.map((device) => {
58
+ if (
59
+ device.device_id !== variables.device_id ||
60
+ device.properties.locked == null
61
+ ) {
62
+ return device
63
+ }
64
+
65
+ return {
66
+ ...device,
67
+ properties: {
68
+ ...device.properties,
69
+ locked: !variables.properties.locked,
70
+ },
71
+ }
72
+ })
73
+ })
74
+
75
+ queryClient.setQueryData<Device>(
76
+ ['devices', 'get', { device_id: variables.device_id }],
77
+ (device) => {
78
+ if (device?.properties.locked == null) return device
79
+
80
+ return {
81
+ ...device,
82
+ properties: {
83
+ ...device.properties,
84
+ locked: !variables.properties.locked,
85
+ },
86
+ }
87
+ }
88
+ )
89
+ },
90
+ onError: async (_error, variables) => {
91
+ params.onError?.()
92
+
93
+ await queryClient.invalidateQueries({
94
+ queryKey: ['devices', 'list'],
95
+ })
96
+ await queryClient.invalidateQueries({
97
+ queryKey: ['devices', 'get', { device_id: variables.device_id }],
98
+ })
99
+ },
100
+ onSuccess() {
101
+ params.onSuccess?.()
102
+ },
103
+ })
104
+ }
@@ -1,3 +1,3 @@
1
- const seamapiReactVersion = '4.13.0'
1
+ const seamapiReactVersion = '4.13.1'
2
2
 
3
3
  export default seamapiReactVersion
@@ -43,6 +43,7 @@
43
43
  > .seam-right {
44
44
  display: flex;
45
45
  align-items: center;
46
+ gap: 8px;
46
47
  }
47
48
  }
48
49