Skip to content

Commit

Permalink
Update useCreateClimateSettingSchedule
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Jul 2, 2024
1 parent 75cc92b commit 24b441e
Showing 1 changed file with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
import type {
SeamHttpApiError,
ThermostatsClimateSettingSchedulesCreateBody,
} from '@seamapi/http/connect'
import type { ClimateSettingSchedule } from '@seamapi/types/connect'
import {
useMutation,
type UseMutationResult,
useQueryClient,
} from '@tanstack/react-query'
import type {
ClimateSettingSchedule,
ClimateSettingScheduleCreateRequest,
ClimateSettingScheduleCreateResponse,
SeamError,
} from 'seamapi'

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

export type UseCreateClimateSettingScheduleParams = never

export type UseCreateClimateSettingScheduleData = ClimateSettingSchedule

export type UseCreateClimateSettingScheduleMutationVariables =
ClimateSettingScheduleCreateRequest
ThermostatsClimateSettingSchedulesCreateBody

export function useCreateClimateSettingSchedule(): UseMutationResult<
UseCreateClimateSettingScheduleData,
SeamError,
SeamHttpApiError,
UseCreateClimateSettingScheduleMutationVariables
> {
const { client } = useSeamClient()
const queryClient = useQueryClient()

return useMutation<
ClimateSettingScheduleCreateResponse['climate_setting_schedule'],
SeamError,
ClimateSettingScheduleCreateRequest
UseCreateClimateSettingScheduleData,
SeamHttpApiError,
UseCreateClimateSettingScheduleMutationVariables
>({
mutationFn: async (
variables: UseCreateClimateSettingScheduleMutationVariables
) => {
mutationFn: async (variables) => {
if (client === null) throw new NullSeamClientError()
const result =
await client.thermostats.climateSettingSchedules.create(variables)
return result
return await client.thermostats.climateSettingSchedules.create(variables)
},
onSuccess: () => {
onSuccess: (data) => {
queryClient.setQueryData(
[
'thermostats',
'climate_setting_schedules',
'get',
{ climate_setting_schedules: data.climate_setting_schedule_id },
],
data
)
void queryClient.invalidateQueries({
queryKey: ['thermostats', 'climate_setting_schedules', 'list'],
})
Expand Down

0 comments on commit 24b441e

Please sign in to comment.