Skip to content

Commit 18f3d54

Browse files
committed
Pass message from API directly
1 parent e860d81 commit 18f3d54

File tree

2 files changed

+18
-59
lines changed

2 files changed

+18
-59
lines changed

src/lib/ui/thermostat/ClimatePreset.tsx

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { SeamHttpApiError } from '@seamapi/http/connect'
12
import classNames from 'classnames'
23
import {
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+
7386
function 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-
274282
function 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

349341
function 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({
422402
const 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',

src/lib/ui/thermostat/ClimatePresets.tsx

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ interface ClimatePresetsManagement {
3030

3131
const CreateNewPresetSymbol = Symbol('CreateNewPreset')
3232

33-
const DeleteClimatePresetErrorCodes = {
34-
ClimatePresetNotFound: 'climate_preset_not_found',
35-
DeviceNotFound: 'device_not_found',
36-
ClimatePresetIsScheduled: 'climate_preset_is_scheduled',
37-
}
38-
3933
export function ClimatePresets(props: ClimatePresetsManagement): JSX.Element {
4034
const { device, onBack } = props
4135

@@ -54,18 +48,8 @@ export function ClimatePresets(props: ClimatePresetsManagement): JSX.Element {
5448
const errorMessage = useMemo(() => {
5549
if (!isError) return ''
5650

57-
if (error?.code === DeleteClimatePresetErrorCodes.ClimatePresetNotFound) {
58-
return t.climatePresetNotFound
59-
}
60-
61-
if (error?.code === DeleteClimatePresetErrorCodes.DeviceNotFound) {
62-
return t.deviceNotFound
63-
}
64-
65-
if (
66-
error?.code === DeleteClimatePresetErrorCodes.ClimatePresetIsScheduled
67-
) {
68-
return t.climatePresetIsScheduled
51+
if(error?.message != null) {
52+
return error.message
6953
}
7054

7155
return t.unknownErrorOccured
@@ -272,7 +256,4 @@ const t = {
272256
edit: 'Edit',
273257
unknownErrorOccured: 'An unknown error occurred.',
274258
climatePresetNotFound: 'Climate Preset not found.',
275-
deviceNotFound: 'Device not found.',
276-
climatePresetIsScheduled:
277-
'The climate preset has upcoming schedules and cannot be deleted.',
278259
}

0 commit comments

Comments
 (0)