Skip to content

Commit

Permalink
Update useUpdateThermostat
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Jul 10, 2024
1 parent 65d85e8 commit cd45c68
Showing 1 changed file with 38 additions and 44 deletions.
82 changes: 38 additions & 44 deletions src/lib/seam/thermostats/use-update-thermostat.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,61 @@
import type { ThermostatsUpdateBody, SeamHttpApiError } from '@seamapi/http/connect'
import type { Device } from '@seamapi/types/connect'
import {
useMutation,
type UseMutationResult,
useQueryClient,
} from '@tanstack/react-query'
import type {
SeamError,
ThermostatDevice,
ThermostatsListResponse,
ThermostatUpdateRequest,
} from 'seamapi'
import { shake } from 'radash'

import { NullSeamClientError, useSeamClient } from 'lib/seam/use-seam-client.js'

// UPSTREAM: Missing ThermostatUpdateResponse in seamapi.
export type UseUpdateThermostatData = Record<string, unknown>
export type UseUpdateThermostatParams = never

export type UseUpdateThermostatMutationVariables = ThermostatUpdateRequest
export type UseUpdateThermostatData = undefined

export type UseUpdateThermostatMutationVariables = ThermostatsUpdateBody

export function useUpdateThermostat(): UseMutationResult<
UseUpdateThermostatData,
SeamError,
SeamHttpApiError,
UseUpdateThermostatMutationVariables
> {
const { client } = useSeamClient()
const queryClient = useQueryClient()

return useMutation<
UseUpdateThermostatData,
SeamError,
SeamHttpApiError,
UseUpdateThermostatMutationVariables
>({
mutationFn: async (variables: UseUpdateThermostatMutationVariables) => {
mutationFn: async (variables) => {
if (client === null) throw new NullSeamClientError()

await client.thermostats.update(variables)
},
onSuccess: (_data, variables) => {
queryClient.setQueryData<ThermostatDevice | null>(
queryClient.setQueryData<Device | null>(
['devices', 'get', { device_id: variables.device_id }],
(thermostat) => {
if (thermostat == null) {
(device) => {
if (device == null) {
return
}

return getUpdatedDevice(thermostat, variables)
return getUpdatedDevice(device, variables)
}
)

queryClient.setQueryData<ThermostatsListResponse['thermostats']>(
queryClient.setQueryData<Device[]>(
['devices', 'list', { device_id: variables.device_id }],
(thermostats): ThermostatDevice[] => {
if (thermostats == null) {
(devices): Device[] => {
if (devices == null) {
return []
}

return thermostats.map((thermostat) => {
if (thermostat.device_id === variables.device_id) {
return getUpdatedDevice(thermostat, variables)
return devices.map((device) => {
if (device.device_id === variables.device_id) {
return getUpdatedDevice(device, variables)
}

return thermostat
return device
})
}
)
Expand All @@ -68,26 +64,24 @@ export function useUpdateThermostat(): UseMutationResult<
}

function getUpdatedDevice(
thermostat: ThermostatDevice,
device: Device,
variables: UseUpdateThermostatMutationVariables
): ThermostatDevice {
return {
...thermostat,

properties: {
...thermostat.properties,

default_climate_setting: {
...thermostat.properties.default_climate_setting,
manual_override_allowed:
thermostat.properties?.default_climate_setting
?.manual_override_allowed != null
? thermostat.properties.default_climate_setting
.manual_override_allowed
: true,

...variables.default_climate_setting,
): Device {
const { properties } = device
if (
'default_climate_setting' in properties &&
properties.default_climate_setting != null
) {
return {
...device,
properties: {
...properties,
default_climate_setting: {
...properties.default_climate_setting,
...shake(variables.default_climate_setting),
},
},
},
}
}
return device
}

0 comments on commit cd45c68

Please sign in to comment.