sync'd up
This commit is contained in:
1 parent
7cb1a5d907
commit
a391eae7c5
19 files changed
+320
-170
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { AutomationData } from "types/automations";
|
||||
import { AutomationData, BareAutomationData } from "types/automations";
|
||||
import { API } from "./base";
|
||||
import { ListData, ListParams } from "./types";
|
||||
import {
|
||||
@@ -20,6 +20,14 @@ export const makeAutomationAPI = (api: API) => ({
|
||||
},
|
||||
async update(auto: AutomationData) {
|
||||
return await api.makeCall({
|
||||
method: "PUT",
|
||||
path: AUTOMTAION_ITEM,
|
||||
data: auto,
|
||||
});
|
||||
},
|
||||
async create(auto: BareAutomationData) {
|
||||
return await api.makeCall({
|
||||
method: "POST",
|
||||
path: AUTOMTAION_ITEM,
|
||||
data: auto,
|
||||
});
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
import { APIRequest, APIResponse } from './types';
|
||||
import { APIRequest, APIResponse } from "./types";
|
||||
|
||||
export interface API {
|
||||
makeCall: <Res = any, Req = any>(req: APIRequest<Req>) => Promise<APIResponse<Res>>;
|
||||
makeCall: <Res = any, Req = any>(
|
||||
req: APIRequest<Req>
|
||||
) => Promise<APIResponse<Res>>;
|
||||
}
|
||||
export const makeRemoteAPI = (baseURL: string): API => ({
|
||||
async makeCall({
|
||||
path,
|
||||
method = "POST",
|
||||
data = {}
|
||||
}) {
|
||||
async makeCall({ path, method = "POST", data = {} }) {
|
||||
try {
|
||||
const payload: RequestInit = {
|
||||
method,
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
if (method !== 'GET') {
|
||||
if (method !== "GET") {
|
||||
payload["body"] = JSON.stringify(data);
|
||||
}
|
||||
const reply = await fetch(baseURL + path, payload);
|
||||
@@ -25,27 +23,27 @@ export const makeRemoteAPI = (baseURL: string): API => ({
|
||||
response = {
|
||||
ok: true,
|
||||
data: await reply.json(),
|
||||
}
|
||||
};
|
||||
} else {
|
||||
response = {
|
||||
ok: false,
|
||||
error: await reply.text(),
|
||||
}
|
||||
console.error(response)
|
||||
};
|
||||
console.error(response);
|
||||
}
|
||||
return response
|
||||
return response;
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
let error = String(err)
|
||||
console.error(err);
|
||||
let error = String(err);
|
||||
|
||||
if (err instanceof Error) {
|
||||
error += ` ${err.stack}`
|
||||
error += ` ${err.stack}`;
|
||||
}
|
||||
|
||||
return {
|
||||
ok: false,
|
||||
error,
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
});
|
||||
@@ -22,6 +22,7 @@ export const useAPIService = (automationAPI: AutomationAPI) => {
|
||||
state,
|
||||
removeAutomation: wrapCall(automationAPI.remove),
|
||||
updateAutomation: wrapCall(automationAPI.update),
|
||||
createAutomation: wrapCall(automationAPI.create),
|
||||
updateTags: wrapCall(automationAPI.updateTags),
|
||||
};
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useRef } from "react";
|
||||
import { AutomationData } from "types/automations";
|
||||
import { AutomationData, BareAutomationData } from "types/automations";
|
||||
import { API } from "./base";
|
||||
import {
|
||||
AUTOMTAION_LIST,
|
||||
@@ -35,13 +35,16 @@ export const useMockAPI = (
|
||||
} as any;
|
||||
}
|
||||
if (path === AUTOMTAION_ITEM) {
|
||||
if (method === "POST") {
|
||||
if (method === "PUT") {
|
||||
const auto = data as AutomationData;
|
||||
const index = automationsRef.current.findIndex(
|
||||
({ id }) => auto.id === id
|
||||
);
|
||||
if (index < 0) {
|
||||
automationsRef.current = [...automationsRef.current, auto];
|
||||
return {
|
||||
ok: false,
|
||||
error: "Not Found",
|
||||
};
|
||||
} else {
|
||||
automationsRef.current = [
|
||||
...automationsRef.current.slice(0, index),
|
||||
@@ -62,6 +65,21 @@ export const useMockAPI = (
|
||||
ok: true,
|
||||
data: {},
|
||||
};
|
||||
} else if (method === "POST") {
|
||||
const auto = data as BareAutomationData;
|
||||
automationsRef.current = [
|
||||
...automationsRef.current,
|
||||
{
|
||||
...auto,
|
||||
source_file: "automation.yaml",
|
||||
source_file_type: "list",
|
||||
configuration_key: "automation",
|
||||
},
|
||||
];
|
||||
return {
|
||||
ok: true,
|
||||
data: {},
|
||||
};
|
||||
}
|
||||
}
|
||||
if (path === AUTOMTAION_ITEM_TAGS) {
|
||||
|
||||
@@ -1,26 +1,29 @@
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
|
||||
export interface ListParams {
|
||||
offset: number;
|
||||
limit: number;
|
||||
}
|
||||
export type APIRequest<D> = {
|
||||
path: string;
|
||||
method?: "POST" | "DELETE" | "GET",
|
||||
method?: "POST" | "DELETE" | "GET" | "PUT";
|
||||
data?: D;
|
||||
};
|
||||
|
||||
export type APIResponse<D> = {
|
||||
ok: true;
|
||||
data: D;
|
||||
} | {
|
||||
ok: false;
|
||||
error: string;
|
||||
}
|
||||
export type Errorable<D> = ({ ready: true } & APIResponse<D>) | {
|
||||
ready: false;
|
||||
}
|
||||
export type APIResponse<D> =
|
||||
| {
|
||||
ok: true;
|
||||
data: D;
|
||||
}
|
||||
| {
|
||||
ok: false;
|
||||
error: string;
|
||||
};
|
||||
export type Errorable<D> =
|
||||
| ({ ready: true } & APIResponse<D>)
|
||||
| {
|
||||
ready: false;
|
||||
};
|
||||
export interface ListData<D> {
|
||||
params: ListParams;
|
||||
totalItems: number;
|
||||
|
||||
@@ -27,10 +27,10 @@ export const makeReloadAutomations = (
|
||||
});
|
||||
};
|
||||
const wrapCall = <Call extends (a: any) => Promise<any>>(fcn: Call) => {
|
||||
return async (args: Parameters<Call>[0]): Promise<ReturnType<Call>> => {
|
||||
return async (args: Parameters<Call>[0]) => {
|
||||
const resp = await fcn(args);
|
||||
await reload();
|
||||
return resp;
|
||||
return resp as ReturnType<Call>;
|
||||
};
|
||||
};
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user