Skip to content

Commit 49a93b6

Browse files
committed
Use mutation
1 parent c3bcf26 commit 49a93b6

File tree

2 files changed

+93
-13
lines changed

2 files changed

+93
-13
lines changed

src/lib/seam/components/DeviceDetails/DeviceDetails.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import { LockDeviceDetails } from 'lib/seam/components/DeviceDetails/LockDeviceD
66
import { NoiseSensorDeviceDetails } from 'lib/seam/components/DeviceDetails/NoiseSensorDeviceDetails.js'
77
import { ThermostatDeviceDetails } from 'lib/seam/components/DeviceDetails/ThermostatDeviceDetails.js'
88
import { useDevice } from 'lib/seam/devices/use-device.js'
9+
import { useSetDeviceName } from 'lib/seam/devices/use-set-device-name.js'
910
import { isLockDevice } from 'lib/seam/locks/lock-device.js'
1011
import { isNoiseSensorDevice } from 'lib/seam/noise-sensors/noise-sensor-device.js'
1112
import { isThermostatDevice } from 'lib/seam/thermostats/thermostat-device.js'
12-
import { useSeamClient } from 'lib/seam/use-seam-client.js'
1313
import { useComponentTelemetry } from 'lib/telemetry/index.js'
1414

1515
export interface DeviceDetailsProps extends CommonProps {
@@ -38,24 +38,21 @@ export function DeviceDetails({
3838
}: DeviceDetailsProps): JSX.Element | null {
3939
useComponentTelemetry('DeviceDetails')
4040

41-
const { client } = useSeamClient()
42-
const { device, refetch: refetchDevice } = useDevice({
41+
const { device } = useDevice({
4342
device_id: deviceId,
4443
})
4544

46-
const updateDeviceName = async (newName: string): Promise<void> => {
47-
if (client == null) return
45+
const { mutate: setDeviceName } = useSetDeviceName({
46+
device_id: deviceId,
47+
})
4848

49-
client.devices
50-
.update({
51-
device_id: deviceId,
49+
const updateDeviceName = (newName: string): void => {
50+
if (device != null) {
51+
setDeviceName({
52+
device_id: device.device_id,
5253
name: newName,
5354
})
54-
.then(async () => await refetchDevice())
55-
.catch((error) => {
56-
// eslint-disable-next-line no-console
57-
console.error(error)
58-
})
55+
}
5956
}
6057

6158
if (device == null) {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import type {
2+
DevicesGetParams,
3+
DevicesUpdateBody,
4+
SeamHttpApiError,
5+
} from '@seamapi/http/connect'
6+
import type { Device } from '@seamapi/types/connect'
7+
import {
8+
useMutation,
9+
type UseMutationResult,
10+
useQueryClient,
11+
} from '@tanstack/react-query'
12+
13+
import { NullSeamClientError, useSeamClient } from 'lib/seam/use-seam-client.js'
14+
15+
export type UseSetDeviceNameParams = never
16+
17+
export type UseSetDeviceNameData = undefined
18+
19+
export type UseSetDeviceNameMutationVariables = Pick<DevicesUpdateBody, 'device_id' | 'name'>
20+
21+
type MutationError = SeamHttpApiError
22+
23+
export function useSetDeviceName(params: DevicesGetParams): UseMutationResult<
24+
UseSetDeviceNameData,
25+
MutationError,
26+
UseSetDeviceNameMutationVariables
27+
> {
28+
const { client } = useSeamClient()
29+
const queryClient = useQueryClient()
30+
31+
return useMutation<
32+
UseSetDeviceNameData,
33+
MutationError,
34+
UseSetDeviceNameMutationVariables
35+
>({
36+
mutationFn: async (variables) => {
37+
if (client === null) throw new NullSeamClientError()
38+
await client.devices.update(variables)
39+
},
40+
onSuccess: (_data, variables) => {
41+
42+
queryClient.setQueryData<Device | null>(
43+
['devices', 'get', params],
44+
(device) => {
45+
if (device == null) {
46+
return
47+
}
48+
49+
return getUpdatedDevice(device, variables.name ?? device.properties.name)
50+
}
51+
)
52+
53+
queryClient.setQueryData<Device[]>(
54+
['devices', 'list', { device_id: variables.device_id }],
55+
(devices): Device[] => {
56+
if (devices == null) {
57+
return []
58+
}
59+
60+
return devices.map((device) => {
61+
if (device.device_id === variables.device_id) {
62+
return getUpdatedDevice(device, variables.name ?? device.properties.name)
63+
}
64+
65+
return device
66+
})
67+
}
68+
)
69+
},
70+
})
71+
}
72+
73+
const getUpdatedDevice = (device: Device, name: string): Device => {
74+
const { properties } = device
75+
76+
return {
77+
...device,
78+
properties: {
79+
...properties,
80+
name,
81+
},
82+
}
83+
}

0 commit comments

Comments
 (0)