added api setup
This commit is contained in:
1 parent
787716ccba
commit
4a08956e1a
21 files changed
+12292
-12527
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { useState } from "react";
|
||||
import { ApiState } from "./types";
|
||||
import { AutomationAPI } from './automations';
|
||||
|
||||
|
||||
|
||||
export const useDefaultApiState = () => useState<ApiState>({
|
||||
automations: {ready: false},
|
||||
});
|
||||
|
||||
export const makeReloadAutomations = (
|
||||
automationAPI: AutomationAPI,
|
||||
state: ApiState,
|
||||
setState: (s: ApiState) => void,
|
||||
) => {
|
||||
const reload = async () => {
|
||||
let params = { offset: 0, limit: 100 };
|
||||
if (state.automations.ready && state.automations.ok) {
|
||||
params = state.automations.data.params
|
||||
}
|
||||
const resp = await automationAPI.list(params);
|
||||
setState({
|
||||
...state, automations: {
|
||||
ready: true,
|
||||
...resp,
|
||||
}
|
||||
});
|
||||
}
|
||||
const wrapCall = <
|
||||
Call extends (a: any) => Promise<any>
|
||||
>(fcn: Call) => {
|
||||
return async (args: Parameters<Call>[0]): Promise<ReturnType<Call>> => {
|
||||
const resp = await fcn(args);
|
||||
await reload();
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
return {
|
||||
reload,
|
||||
wrapCall,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user