Skip to content

Commit b0e87a2

Browse files
committed
Refactor climate preset form
1 parent 3329471 commit b0e87a2

File tree

8 files changed

+360
-288
lines changed

8 files changed

+360
-288
lines changed

src/lib/seam/thermostats/thermostat-device.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ export interface ClimateSetting {
3737
export const isThermostatDevice = (
3838
device: Device
3939
): device is ThermostatDevice => 'is_fan_running' in device.properties
40+
41+
export type ThermostatClimatePreset = ThermostatDevice['properties']['available_climate_presets'][number]

src/lib/seam/thermostats/unit-conversion.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { Device } from '@seamapi/types/connect'
22

3-
export const celsiusToFahrenheit = (t: number): number => (t * 9) / 5 + 32
3+
type ConversionReturn<T> = T extends NonNullable<number> ? number : T
44

5-
export const fahrenheitToCelsius = (t: number): number => (t - 32) * (5 / 9)
5+
export const celsiusToFahrenheit = <T>(t: T): ConversionReturn<T> => (typeof t === 'number' ? ((t * 9) / 5 + 32) : t) as ConversionReturn<T>
6+
7+
export const fahrenheitToCelsius = <T>(t: T): ConversionReturn<T> => (typeof t === 'number' ? ((t - 32) * (5 / 9)) : t) as ConversionReturn<T>
68

79
export const getCoolingSetPointCelsius = (
810
variables: {
@@ -87,3 +89,7 @@ export const getHeatingSetPointFahrenheit = (
8789
undefined
8890
)
8991
}
92+
93+
export function getTemperatureUnitSymbol(type: "fahrenheit" | "celsius"): string {
94+
return type === "fahrenheit" ? "°F" : "°C"
95+
}

src/lib/seam/thermostats/use-create-thermostat-climate-preset.ts

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
useQueryClient,
99
} from '@tanstack/react-query'
1010

11-
import type { ThermostatDevice } from 'lib/seam/thermostats/thermostat-device.js'
11+
import type { ThermostatClimatePreset, ThermostatDevice } from 'lib/seam/thermostats/thermostat-device.js'
12+
import { fahrenheitToCelsius } from 'lib/seam/thermostats/unit-conversion.js'
1213
import { NullSeamClientError, useSeamClient } from 'lib/seam/use-seam-client.js'
1314

1415
export type UseCreateThermostatClimatePresetParams = never
@@ -17,12 +18,6 @@ export type UseCreateThermostatClimatePresetData = undefined
1718
export type UseCreateThermostatClimatePresetVariables =
1819
ThermostatsCreateClimatePresetBody
1920

20-
const fhToCelsius = (t?: number): number | undefined =>
21-
t == null ? undefined : (t - 32) * (5 / 9)
22-
23-
type ClimatePreset =
24-
ThermostatDevice['properties']['available_climate_presets'][number]
25-
2621
export function useCreateThermostatClimatePreset(): UseMutationResult<
2722
UseCreateThermostatClimatePresetData,
2823
SeamHttpApiError,
@@ -41,28 +36,14 @@ export function useCreateThermostatClimatePreset(): UseMutationResult<
4136
await client.thermostats.createClimatePreset(variables)
4237
},
4338
onSuccess: (_data, variables) => {
44-
const preset: ClimatePreset = {
45-
...variables,
46-
cooling_set_point_celsius: fhToCelsius(
47-
variables.cooling_set_point_fahrenheit
48-
),
49-
heating_set_point_celsius: fhToCelsius(
50-
variables.heating_set_point_fahrenheit
51-
),
52-
display_name: variables.name ?? variables.climate_preset_key,
53-
can_delete: true,
54-
can_edit: true,
55-
manual_override_allowed: true,
56-
}
57-
5839
queryClient.setQueryData<ThermostatDevice | null>(
5940
['devices', 'get', { device_id: variables.device_id }],
6041
(device) => {
6142
if (device == null) {
6243
return
6344
}
6445

65-
return getUpdatedDevice(device, preset)
46+
return getUpdatedDevice(device, variables)
6647
}
6748
)
6849

@@ -75,7 +56,7 @@ export function useCreateThermostatClimatePreset(): UseMutationResult<
7556

7657
return devices.map((device) => {
7758
if (device.device_id === variables.device_id) {
78-
return getUpdatedDevice(device, preset)
59+
return getUpdatedDevice(device, variables)
7960
}
8061

8162
return device
@@ -86,12 +67,22 @@ export function useCreateThermostatClimatePreset(): UseMutationResult<
8667
})
8768
}
8869

89-
function getUpdatedDevice(
70+
const getUpdatedDevice =(
9071
device: ThermostatDevice,
91-
preset: ClimatePreset
92-
): ThermostatDevice {
93-
if (device == null) {
94-
return device
72+
variables: UseCreateThermostatClimatePresetVariables
73+
): ThermostatDevice => {
74+
const preset: ThermostatClimatePreset = {
75+
...variables,
76+
cooling_set_point_celsius: fahrenheitToCelsius(
77+
variables.cooling_set_point_fahrenheit
78+
),
79+
heating_set_point_celsius: fahrenheitToCelsius(
80+
variables.heating_set_point_fahrenheit
81+
),
82+
display_name: variables.name ?? variables.climate_preset_key,
83+
can_delete: true,
84+
can_edit: true,
85+
manual_override_allowed: false,
9586
}
9687

9788
return {

src/lib/seam/thermostats/use-delete-thermostat-climate-preset.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function useDeleteThermostatClimatePreset(): UseMutationResult<
4343
return
4444
}
4545

46-
return getUpdatedDevice(device, variables.climate_preset_key)
46+
return getUpdatedDevice(device, variables)
4747
}
4848
)
4949

@@ -56,7 +56,7 @@ export function useDeleteThermostatClimatePreset(): UseMutationResult<
5656

5757
return devices.map((device) => {
5858
if (device.device_id === variables.device_id) {
59-
return getUpdatedDevice(device, variables.climate_preset_key)
59+
return getUpdatedDevice(device, variables)
6060
}
6161

6262
return device
@@ -69,16 +69,16 @@ export function useDeleteThermostatClimatePreset(): UseMutationResult<
6969

7070
function getUpdatedDevice(
7171
device: ThermostatDevice,
72-
climatePresetKey: string
72+
variables: UseDeleteThermostatClimatePresetVariables
7373
): ThermostatDevice {
7474
return {
7575
...device,
7676
properties: {
7777
...device.properties,
7878
available_climate_presets:
79-
device.properties.available_climate_presets.filter(
80-
(preset) => preset.climate_preset_key !== climatePresetKey
81-
),
79+
device.properties.available_climate_presets.filter((preset) => {
80+
return preset.climate_preset_key !== variables.climate_preset_key
81+
}),
8282
},
8383
}
8484
}

src/lib/seam/thermostats/use-update-thermostat-climate-preset.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
useQueryClient,
99
} from '@tanstack/react-query'
1010

11-
import type { ThermostatDevice } from 'lib/seam/thermostats/thermostat-device.js'
11+
import type { ThermostatClimatePreset, ThermostatDevice } from 'lib/seam/thermostats/thermostat-device.js'
12+
import { fahrenheitToCelsius } from 'lib/seam/thermostats/unit-conversion.js'
1213
import { NullSeamClientError, useSeamClient } from 'lib/seam/use-seam-client.js'
1314

1415
export type UseUpdateThermostatClimatePresetParams = never
@@ -17,12 +18,6 @@ export type UseUpdateThermostatClimatePresetData = undefined
1718
export type UseUpdateThermostatClimatePresetVariables =
1819
ThermostatsUpdateClimatePresetBody
1920

20-
const fhToCelsius = (t?: number): number | undefined =>
21-
t == null ? undefined : (t - 32) * (5 / 9)
22-
23-
type ClimatePreset =
24-
ThermostatDevice['properties']['available_climate_presets'][number]
25-
2621
export function useUpdateThermostatClimatePreset(): UseMutationResult<
2722
UseUpdateThermostatClimatePresetData,
2823
SeamHttpApiError,
@@ -41,28 +36,14 @@ export function useUpdateThermostatClimatePreset(): UseMutationResult<
4136
await client.thermostats.createClimatePreset(variables)
4237
},
4338
onSuccess: (_data, variables) => {
44-
const preset: ClimatePreset = {
45-
...variables,
46-
cooling_set_point_celsius: fhToCelsius(
47-
variables.cooling_set_point_fahrenheit
48-
),
49-
heating_set_point_celsius: fhToCelsius(
50-
variables.heating_set_point_fahrenheit
51-
),
52-
display_name: variables.name ?? variables.climate_preset_key,
53-
can_delete: true,
54-
can_edit: true,
55-
manual_override_allowed: true,
56-
}
57-
5839
queryClient.setQueryData<ThermostatDevice | null>(
5940
['devices', 'get', { device_id: variables.device_id }],
6041
(device) => {
6142
if (device == null) {
6243
return
6344
}
6445

65-
return getUpdatedDevice(device, preset)
46+
return getUpdatedDevice(device, variables)
6647
}
6748
)
6849

@@ -75,7 +56,7 @@ export function useUpdateThermostatClimatePreset(): UseMutationResult<
7556

7657
return devices.map((device) => {
7758
if (device.device_id === variables.device_id) {
78-
return getUpdatedDevice(device, preset)
59+
return getUpdatedDevice(device, variables)
7960
}
8061

8162
return device
@@ -88,10 +69,20 @@ export function useUpdateThermostatClimatePreset(): UseMutationResult<
8869

8970
function getUpdatedDevice(
9071
device: ThermostatDevice,
91-
preset: ClimatePreset
72+
variables: UseUpdateThermostatClimatePresetVariables
9273
): ThermostatDevice {
93-
if (device == null) {
94-
return device
74+
const preset: ThermostatClimatePreset = {
75+
...variables,
76+
cooling_set_point_celsius: fahrenheitToCelsius(
77+
variables.cooling_set_point_fahrenheit
78+
),
79+
heating_set_point_celsius: fahrenheitToCelsius(
80+
variables.heating_set_point_fahrenheit
81+
),
82+
display_name: variables.name ?? variables.climate_preset_key,
83+
can_delete: true,
84+
can_edit: true,
85+
manual_override_allowed: true,
9586
}
9687

9788
return {

src/lib/ui/thermostat/ClimateModeMenu.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import classNames from 'classnames'
2+
import type { CSSProperties } from 'react'
23

34
import { ChevronDownIcon } from 'lib/icons/ChevronDown.js'
45
import { OffIcon } from 'lib/icons/Off.js'
@@ -15,7 +16,7 @@ interface ClimateModeMenuProps {
1516
supportedModes?: HvacModeSetting[]
1617
buttonTextVisible?: boolean
1718
className?: string
18-
style?: React.CSSProperties
19+
style?: CSSProperties
1920
block?: boolean
2021
size?: 'regular' | 'large'
2122
}

0 commit comments

Comments
 (0)