1+ import type { SeamHttpApiError } from '@seamapi/http/connect'
12import classNames from 'classnames'
23import {
34 type HTMLAttributes ,
@@ -70,6 +71,18 @@ interface PresetFormProps {
7071 withKeyField ?: boolean
7172}
7273
74+ function useErrorMessage ( isError : boolean , error : SeamHttpApiError | null ) : string {
75+ return useMemo ( ( ) => {
76+ if ( ! isError ) return ''
77+
78+ if ( error ?. message != null ) {
79+ return error . message
80+ }
81+
82+ return t . unknownErrorOccured
83+ } , [ error , isError ] )
84+ }
85+
7386function PresetForm ( props : PresetFormProps ) : JSX . Element {
7487 const {
7588 defaultValues,
@@ -266,28 +279,11 @@ interface CreateFormProps {
266279 onComplete : ( ) => void
267280}
268281
269- const CreateClimatePresetErrorCodes = {
270- DeviceNotFound : 'device_not_found' ,
271- ClimatePresetExists : 'climate_preset_exists' ,
272- }
273-
274282function CreateForm ( { device, onComplete } : CreateFormProps ) : JSX . Element {
275283 const { mutate, isError, error, isPending } =
276284 useCreateThermostatClimatePreset ( )
277285
278- const errorMessage = useMemo ( ( ) => {
279- if ( ! isError ) return ''
280-
281- if ( error ?. code === CreateClimatePresetErrorCodes . ClimatePresetExists ) {
282- return t . keyAlreadyExists
283- }
284-
285- if ( error ?. code === CreateClimatePresetErrorCodes . DeviceNotFound ) {
286- return t . deviceNotFound
287- }
288-
289- return t . unknownErrorOccured
290- } , [ error , isError ] )
286+ const errorMessage = useErrorMessage ( isError , error )
291287
292288 const onSubmit = useCallback (
293289 ( values : PresetFormProps [ 'defaultValues' ] ) => {
@@ -341,10 +337,6 @@ interface UpdateFormProps {
341337 onComplete : ( ) => void
342338 preset : ThermostatClimatePreset
343339}
344- const UpdateClimatePresetErrorCodes = {
345- DeviceNotFound : 'device_not_found' ,
346- ClimatePresetNotFound : 'climate_preset_not_found' ,
347- }
348340
349341function UpdateForm ( {
350342 device,
@@ -386,20 +378,8 @@ function UpdateForm({
386378 [ device , mutate , onComplete ]
387379 )
388380
389- const errorMessage = useMemo ( ( ) => {
390- if ( ! isError ) return ''
391-
392- if ( error ?. code === UpdateClimatePresetErrorCodes . ClimatePresetNotFound ) {
393- return t . climatePresetNotFound
394- }
395-
396- if ( error ?. code === UpdateClimatePresetErrorCodes . DeviceNotFound ) {
397- return t . deviceNotFound
398- }
399-
400- return t . unknownErrorOccured
401- } , [ error , isError ] )
402-
381+ const errorMessage = useErrorMessage ( isError , error ) ;
382+
403383 return (
404384 < >
405385 < Snackbar
@@ -422,8 +402,6 @@ function UpdateForm({
422402const t = {
423403 keyAlreadyExists : 'Climate Preset with this key already exists.' ,
424404 keyCannotContainSpaces : 'Climate Preset key cannot contain spaces.' ,
425- climatePresetNotFound : 'Climate Preset not found.' ,
426- deviceNotFound : 'Device not found.' ,
427405 nameField : 'Display Name (Optional)' ,
428406 fanModeField : 'Fan Mode' ,
429407 hvacModeField : 'HVAC Mode' ,
0 commit comments