ground work for minimal tag update
This commit is contained in:
1 parent
2958fa49f8
commit
fabb5612d5
11 files changed
+200
-84
No files matched your search
@@ -1,33 +1,34 @@
|
||||
import { useRef } from 'react';
|
||||
import { AutomationData } from 'types/automations';
|
||||
import { API } from './base';
|
||||
import { AUTOMTAION_LIST, AUTOMTAION_ITEM } from './paths';
|
||||
|
||||
import { useRef } from "react";
|
||||
import { AutomationData } from "types/automations";
|
||||
import { API } from "./base";
|
||||
import {
|
||||
AUTOMTAION_LIST,
|
||||
AUTOMTAION_ITEM,
|
||||
AUTOMTAION_ITEM_TAGS,
|
||||
} from "./paths";
|
||||
|
||||
export const useMockAPI = (
|
||||
initialAutos: AutomationData[] = [],
|
||||
makeRef = useRef,
|
||||
makeRef = useRef
|
||||
): API => {
|
||||
const automationsRef = makeRef(initialAutos);
|
||||
return {
|
||||
async makeCall({
|
||||
path,
|
||||
method = "POST",
|
||||
data = {}
|
||||
}) {
|
||||
async makeCall({ path, method = "POST", data = {} }) {
|
||||
// console.debug("[[mockCall]]", method, path, data);
|
||||
if (path === AUTOMTAION_LIST) {
|
||||
return {
|
||||
ok: true,
|
||||
data: JSON.parse(JSON.stringify({
|
||||
totalItems: automationsRef.current.length,
|
||||
params: { offset: 0, limit: 100 },
|
||||
data: automationsRef.current,
|
||||
}))
|
||||
} as any
|
||||
data: JSON.parse(
|
||||
JSON.stringify({
|
||||
totalItems: automationsRef.current.length,
|
||||
params: { offset: 0, limit: 100 },
|
||||
data: automationsRef.current,
|
||||
})
|
||||
),
|
||||
} as any;
|
||||
}
|
||||
if (path === AUTOMTAION_ITEM) {
|
||||
if (method === 'POST') {
|
||||
if (method === "POST") {
|
||||
const { index, data: auto } = data as any;
|
||||
if (index >= automationsRef.current.length) {
|
||||
automationsRef.current = [...automationsRef.current, auto];
|
||||
@@ -35,29 +36,45 @@ export const useMockAPI = (
|
||||
automationsRef.current = [
|
||||
...automationsRef.current.slice(0, index),
|
||||
auto,
|
||||
...automationsRef.current.slice(index + 1)
|
||||
...automationsRef.current.slice(index + 1),
|
||||
];
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
data: {}
|
||||
}
|
||||
data: {},
|
||||
};
|
||||
} else if (method === "DELETE") {
|
||||
const { index } = data as any;
|
||||
automationsRef.current = [
|
||||
...automationsRef.current.slice(0, index),
|
||||
...automationsRef.current.slice(index + 1)
|
||||
...automationsRef.current.slice(index + 1),
|
||||
];
|
||||
return {
|
||||
ok: true,
|
||||
data: {}
|
||||
data: {},
|
||||
};
|
||||
}
|
||||
}
|
||||
if (path === AUTOMTAION_ITEM_TAGS) {
|
||||
if (method === "POST") {
|
||||
const { automation_id, tags } = data as any;
|
||||
for (let i = 0; i < automationsRef.current.length; i++) {
|
||||
const auto = automationsRef.current[i];
|
||||
if (auto.metadata.id === automation_id) {
|
||||
automationsRef.current[i].tags = JSON.parse(JSON.stringify(tags));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
data: {},
|
||||
};
|
||||
}
|
||||
}
|
||||
return {
|
||||
ok: false,
|
||||
error: "PATH NOT FOUND " + method + ": " + path,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
},
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user