-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart-block-hooks.ts
62 lines (57 loc) · 1.84 KB
/
chart-block-hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {
UpdateTwoDimensionChartFormatMutationHookResult,
useUpdateTwoDimensionChartFormatMutation,
} from "~/graphql/Mutations/Chart/UpdateTwoDimensionChartFormat.mutation.generated";
import { useErrorToast } from "~/layout/hooks/hooks";
import { ToastService } from "~/layout/services/toast-service";
import {
TwoDimensionChartFormat,
TwoDimensionChartFormatUpdate,
} from "../../../../../types";
export interface UseUpdateChartFormatResult {
updateChartFormat: (
chartFormat: TwoDimensionChartFormat,
update: TwoDimensionChartFormatUpdate,
errorDescription: string
) => void;
chartFormatIsLoading: boolean;
}
/**
* Chart format update hook
*/
export function useUpdateChartFormat(): UseUpdateChartFormatResult {
const [
updateChartFormat,
{ loading: chartFormatIsLoading, error: chartFormatError },
]: UpdateTwoDimensionChartFormatMutationHookResult =
useUpdateTwoDimensionChartFormatMutation();
useErrorToast(chartFormatError);
// Pass back properties that would be nullified if omitted
const safeFormatUpdate = async (
chartFormat: TwoDimensionChartFormat,
update: TwoDimensionChartFormatUpdate,
errorDescription: string
): Promise<void> => {
try {
await updateChartFormat({
variables: {
update: {
leftAxisScale: chartFormat.leftAxisScale,
rightAxisScale: chartFormat.rightAxisScale,
leftAxisMinimum: chartFormat.leftAxisMinimum,
leftAxisMaximum: chartFormat.leftAxisMaximum,
rightAxisMinimum: chartFormat.rightAxisMinimum,
rightAxisMaximum: chartFormat.rightAxisMaximum,
...update,
},
},
});
} catch (error) {
ToastService.error(errorDescription, error.message);
}
};
return {
updateChartFormat: safeFormatUpdate,
chartFormatIsLoading,
};
}