Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Jul 10, 2024
1 parent 980c4f4 commit b23f8d8
Showing 1 changed file with 56 additions and 36 deletions.
92 changes: 56 additions & 36 deletions src/lib/seam/thermostats/use-cool-thermostat.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,66 @@
import type {
SeamActionAttemptFailedError,
SeamActionAttemptTimeoutError,
SeamHttpApiError,
ThermostatsCoolBody,
} from '@seamapi/http/connect'
import type { ActionAttempt, Device } from '@seamapi/types/connect'
import {
useMutation,
type UseMutationResult,
useQueryClient,
} from '@tanstack/react-query'
import type {
SeamError,
ThermostatCoolRequest,
ThermostatDevice,
ThermostatsListResponse,
} from 'seamapi'

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

// UPSTREAM: Missing ThermostatCoolResponse in seamapi.
export type UseCoolThermostatData = Record<string, unknown>
export type UseCoolThermostatParams = never

export type UseCoolThermostatData = undefined

export type UseCoolThermostatMutationVariables = Pick<
ThermostatsCoolBody,
'cooling_set_point_celsius' | 'cooling_set_point_fahrenheit'
>

export type UseCoolThermostatMutationVariables = ThermostatCoolRequest
type CoolThermostatActionAttempt = Extract<
ActionAttempt,
{ action_type: 'COOL_THERMOSTAT' }
>

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

export function useCoolThermostat(): UseMutationResult<
UseCoolThermostatData,
SeamError,
MutationError,
UseCoolThermostatMutationVariables
> {
const { client } = useSeamClient()
const queryClient = useQueryClient()

return useMutation<
UseCoolThermostatData,
SeamError,
MutationError,
UseCoolThermostatMutationVariables
>({
mutationFn: async (variables: UseCoolThermostatMutationVariables) => {
mutationFn: async (variables) => {
if (client === null) throw new NullSeamClientError()

return await client.thermostats.cool(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 getUpdatedThermostat(thermostat, variables)
return getUpdatedThermostat(device, variables)
}
)

queryClient.setQueryData<ThermostatsListResponse['thermostats']>(
queryClient.setQueryData<Device[]>(
['devices', 'list', { device_id: variables.device_id }],
(thermostats): ThermostatDevice[] => {
if (thermostats == null) {
Expand All @@ -68,24 +81,31 @@ export function useCoolThermostat(): UseMutationResult<
}

function getUpdatedThermostat(
thermostat: ThermostatDevice,
device: Device,
variables: UseCoolThermostatMutationVariables
): ThermostatDevice {
return {
...thermostat,
properties: {
...thermostat.properties,
current_climate_setting: {
...thermostat.properties.current_climate_setting,
cooling_set_point_fahrenheit:
variables.cooling_set_point_fahrenheit ??
thermostat.properties.current_climate_setting
.cooling_set_point_fahrenheit,
cooling_set_point_celsius:
variables.cooling_set_point_celsius ??
thermostat.properties.current_climate_setting
.cooling_set_point_celsius,
): Device {
const { properties } = device
if (
'current_climate_setting' in properties &&
'cooling_set_point_fahrenheit' in properties &&
'cooling_set_point_celsius' in properties
) {
return {
...device,
properties: {
...device.properties,
current_climate_setting: {
...device.properties.current_climate_setting,
cooling_set_point_fahrenheit:
variables.cooling_set_point_fahrenheit ??
device.properties.current_climate_setting
.cooling_set_point_fahrenheit,
cooling_set_point_celsius:
variables.cooling_set_point_celsius ??
device.properties.current_climate_setting.cooling_set_point_celsius,
},
},
},
}
}
return device
}

0 comments on commit b23f8d8

Please sign in to comment.