Skip to content

Commit

Permalink
Rename useUpdateFanMode to useSetThermostatFanMode
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Jul 10, 2024
1 parent 95eba69 commit 65d85e8
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { getSupportedThermostatModes } from 'lib/seam/thermostats/temperature-bo
import { useCoolThermostat } from 'lib/seam/thermostats/use-cool-thermostat.js'
import { useHeatCoolThermostat } from 'lib/seam/thermostats/use-heat-cool-thermostat.js'
import { useHeatThermostat } from 'lib/seam/thermostats/use-heat-thermostat.js'
import { useSetThermostatFanMode } from 'lib/seam/thermostats/use-set-thermostat-fan-mode.js'
import { useSetThermostatOff } from 'lib/seam/thermostats/use-set-thermostat-off.js'
import { useUpdateFanMode } from 'lib/seam/thermostats/use-update-fan-mode.js'
import { useUpdateThermostat } from 'lib/seam/thermostats/use-update-thermostat.js'
import { AccordionRow } from 'lib/ui/layout/AccordionRow.js'
import { ContentHeader } from 'lib/ui/layout/ContentHeader.js'
Expand Down Expand Up @@ -203,7 +203,7 @@ function ManualOverrideRow({
}

function FanModeRow({ device }: { device: ThermostatDevice }): JSX.Element {
const { mutate, isSuccess, isError } = useUpdateFanMode()
const { mutate, isSuccess, isError } = useSetThermostatFanMode()

return (
<>
Expand Down
95 changes: 95 additions & 0 deletions src/lib/seam/thermostats/use-set-thermostat-fan-mode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import type {
SeamActionAttemptFailedError,
SeamActionAttemptTimeoutError,
SeamHttpApiError,
ThermostatsSetFanModeBody,
} from '@seamapi/http/connect'
import type { ActionAttempt, Device } from '@seamapi/types/connect'
import {
useMutation,
type UseMutationResult,
useQueryClient,
} from '@tanstack/react-query'

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

export type UseSetThermostatFanModeParams = never

export type UseSetThermostatFanModeData = undefined

export type UseSetThermostatFanModeMutationVariables = ThermostatsSetFanModeBody

type SetThermostatFanModeActionAttempt = Extract<
ActionAttempt,
{ action_type: 'SET_FAN_MODE' }
>

type MutationError =
| SeamHttpApiError
| SeamActionAttemptFailedError<SetThermostatFanModeActionAttempt>
| SeamActionAttemptTimeoutError<SetThermostatFanModeActionAttempt>

export function useSetThermostatFanMode(): UseMutationResult<
UseSetThermostatFanModeData,
MutationError,
UseSetThermostatFanModeMutationVariables
> {
const { client } = useSeamClient()
const queryClient = useQueryClient()

return useMutation<
UseSetThermostatFanModeData,
MutationError,
UseSetThermostatFanModeMutationVariables
>({
mutationFn: async (variables) => {
if (client === null) throw new NullSeamClientError()
await client.thermostats.setFanMode(variables)
},
onSuccess: (_data, variables) => {
queryClient.setQueryData<Device | null>(
['devices', 'get', { device_id: variables.device_id }],
(device) => {
if (device == null) {
return
}
return getUpdatedDevice(device, variables)
}
)

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

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

return device
})
}
)
},
})
}

function getUpdatedDevice(
device: Device,
variables: UseSetThermostatFanModeMutationVariables
): Device {
const { properties } = device
if ('fan_mode_setting' in properties && properties.fan_mode_setting != null) {
return {
...device,
properties: {
...properties,
fan_mode_setting: variables.fan_mode_setting,
},
}
}
return device
}
82 changes: 0 additions & 82 deletions src/lib/seam/thermostats/use-update-fan-mode.ts

This file was deleted.

0 comments on commit 65d85e8

Please sign in to comment.