@@ -21,6 +21,7 @@ import { Button } from 'lib/ui/Button.js'
2121import { FormField } from 'lib/ui/FormField.js'
2222import { InputLabel } from 'lib/ui/InputLabel.js'
2323import { ContentHeader } from 'lib/ui/layout/ContentHeader.js'
24+ import { Snackbar } from 'lib/ui/Snackbar/Snackbar.js'
2425import { TextField } from 'lib/ui/TextField/TextField.js'
2526import { ClimateModeMenu } from 'lib/ui/thermostat/ClimateModeMenu.js'
2627import { FanModeMenu } from 'lib/ui/thermostat/FanModeMenu.js'
@@ -265,12 +266,35 @@ interface CreateFormProps {
265266 onComplete : ( ) => void
266267}
267268
269+ /**
270+ * @see https://github.com/seamapi/seam-connect/blob/a0b081e086e6f031ad3bcac6dd3f27c46e5b54e5/pages/api/public/thermostats/create_climate_preset.ts#L78-L81
271+ **/
272+ const CreateClimatePresetErrorCodes = {
273+ DeviceNotFound : 'device_not_found' ,
274+ ClimatePresetExists : 'climate_preset_exists' ,
275+ }
276+
268277function CreateForm ( { device, onComplete } : CreateFormProps ) : JSX . Element {
269- const mutation = useCreateThermostatClimatePreset ( )
278+ const { mutate, isError, error, isPending } =
279+ useCreateThermostatClimatePreset ( )
280+
281+ const errorMessage = useMemo ( ( ) => {
282+ if ( ! isError ) return ''
283+
284+ if ( error ?. code === CreateClimatePresetErrorCodes . ClimatePresetExists ) {
285+ return t . keyAlreadyExists
286+ }
287+
288+ if ( error ?. code === CreateClimatePresetErrorCodes . DeviceNotFound ) {
289+ return t . deviceNotFound
290+ }
291+
292+ return t . unknownErrorOccured
293+ } , [ error , isError ] )
270294
271295 const onSubmit = useCallback (
272296 ( values : PresetFormProps [ 'defaultValues' ] ) => {
273- mutation . mutate (
297+ mutate (
274298 {
275299 climate_preset_key : values . key ,
276300 device_id : device . device_id ,
@@ -285,24 +309,33 @@ function CreateForm({ device, onComplete }: CreateFormProps): JSX.Element {
285309 { onSuccess : onComplete }
286310 )
287311 } ,
288- [ device , mutation , onComplete ]
312+ [ device , mutate , onComplete ]
289313 )
290314
291315 return (
292- < PresetForm
293- defaultValues = { {
294- key : '' ,
295- coolPoint : 60 ,
296- heatPoint : 80 ,
297- name : '' ,
298- hvacMode : 'off' ,
299- fanMode : 'auto' ,
300- } }
301- device = { device }
302- loading = { mutation . isPending }
303- onSubmit = { onSubmit }
304- withKeyField
305- />
316+ < >
317+ < Snackbar
318+ message = { errorMessage }
319+ variant = 'error'
320+ visible = { isError }
321+ automaticVisibility
322+ />
323+
324+ < PresetForm
325+ defaultValues = { {
326+ key : '' ,
327+ coolPoint : 60 ,
328+ heatPoint : 80 ,
329+ name : '' ,
330+ hvacMode : 'off' ,
331+ fanMode : 'auto' ,
332+ } }
333+ device = { device }
334+ loading = { isPending }
335+ onSubmit = { onSubmit }
336+ withKeyField
337+ />
338+ </ >
306339 )
307340}
308341
@@ -312,16 +345,26 @@ interface UpdateFormProps {
312345 preset : ThermostatClimatePreset
313346}
314347
348+ /**
349+ * @see https://github.com/seamapi/seam-connect/blob/a0b081e086e6f031ad3bcac6dd3f27c46e5b54e5/pages/api/public/thermostats/update_climate_preset.ts
350+ **/
351+ const UpdateClimatePresetErrorCodes = {
352+ DeviceNotFound : 'device_not_found' ,
353+ ClimatePresetNotFound : 'climate_preset_not_found' ,
354+ }
355+
315356function UpdateForm ( {
316357 device,
317358 onComplete,
318359 preset,
319360} : UpdateFormProps ) : JSX . Element {
320- const mutation = useUpdateThermostatClimatePreset ( )
361+ const { mutate, isError, error, isPending } =
362+ useUpdateThermostatClimatePreset ( )
363+
321364 const defaultValues = useMemo < PresetFormProps [ 'defaultValues' ] > (
322365 ( ) => ( {
323- coolPoint : preset . cooling_set_point_fahrenheit ?? 60 ,
324- heatPoint : preset . heating_set_point_fahrenheit ?? 80 ,
366+ coolPoint : preset . cooling_set_point_fahrenheit ,
367+ heatPoint : preset . heating_set_point_fahrenheit ,
325368 name : preset . display_name ,
326369 hvacMode : preset . hvac_mode_setting ,
327370 fanMode : preset . fan_mode_setting ,
@@ -332,7 +375,7 @@ function UpdateForm({
332375
333376 const onSubmit = useCallback (
334377 ( values : PresetFormProps [ 'defaultValues' ] ) => {
335- mutation . mutate (
378+ mutate (
336379 {
337380 climate_preset_key : values . key ,
338381 device_id : device . device_id ,
@@ -347,27 +390,53 @@ function UpdateForm({
347390 { onSuccess : onComplete }
348391 )
349392 } ,
350- [ device , mutation , onComplete ]
393+ [ device , mutate , onComplete ]
351394 )
352395
396+ const errorMessage = useMemo ( ( ) => {
397+ if ( ! isError ) return ''
398+
399+ if ( error ?. code === UpdateClimatePresetErrorCodes . ClimatePresetNotFound ) {
400+ return t . climatePresetNotFound
401+ }
402+
403+ if ( error ?. code === UpdateClimatePresetErrorCodes . DeviceNotFound ) {
404+ return t . deviceNotFound
405+ }
406+
407+ return t . unknownErrorOccured
408+ } , [ error , isError ] )
409+
353410 return (
354- < PresetForm
355- defaultValues = { defaultValues }
356- device = { device }
357- loading = { mutation . isPending }
358- onSubmit = { onSubmit }
359- />
411+ < >
412+ < Snackbar
413+ message = { errorMessage }
414+ variant = 'error'
415+ visible = { isError }
416+ automaticVisibility
417+ />
418+
419+ < PresetForm
420+ defaultValues = { defaultValues }
421+ device = { device }
422+ loading = { isPending }
423+ onSubmit = { onSubmit }
424+ />
425+ </ >
360426 )
361427}
362428
363429const t = {
364430 keyAlreadyExists : 'Climate Preset with this key already exists.' ,
365431 keyCannotContainSpaces : 'Climate Preset key cannot contain spaces.' ,
432+ climatePresetNotFound : 'Climate Preset not found.' ,
433+ deviceNotFound : 'Device not found.' ,
366434 nameField : 'Display Name (Optional)' ,
367435 fanModeField : 'Fan Mode' ,
368436 hvacModeField : 'HVAC Mode' ,
369437 heatCoolField : 'Heat / Cool' ,
370438 delete : 'Delete' ,
371439 save : 'Save' ,
372440 crateNewPreset : 'Create New Climate Preset' ,
441+ unknownErrorOccured : 'An unknown error occurred.' ,
373442}
0 commit comments