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,27 +1,46 @@
|
||||
import { AutomationData } from "types/automations";
|
||||
import { API } from "./base";
|
||||
import { ListData, ListParams } from "./types";
|
||||
import { AUTOMTAION_LIST, AUTOMTAION_ITEM } from './paths';
|
||||
|
||||
import {
|
||||
AUTOMTAION_LIST,
|
||||
AUTOMTAION_ITEM,
|
||||
AUTOMTAION_ITEM_TAGS,
|
||||
} from "./paths";
|
||||
|
||||
export const makeAutomationAPI = (api: API) => ({
|
||||
async list({ offset, limit }: ListParams) {
|
||||
const data = await api.makeCall<ListData<AutomationData>, ListParams>({
|
||||
path: AUTOMTAION_LIST,
|
||||
data: {
|
||||
offset, limit,
|
||||
}
|
||||
offset,
|
||||
limit,
|
||||
},
|
||||
});
|
||||
return data;
|
||||
},
|
||||
async update({ index, auto }: { index: number, auto: AutomationData }) {
|
||||
async update({ index, auto }: { index: number; auto: AutomationData }) {
|
||||
return await api.makeCall({
|
||||
path: AUTOMTAION_ITEM,
|
||||
data: {
|
||||
index,
|
||||
data: auto
|
||||
data: auto,
|
||||
},
|
||||
})
|
||||
});
|
||||
},
|
||||
async updateTags({
|
||||
automation_id,
|
||||
tags,
|
||||
}: {
|
||||
automation_id: string;
|
||||
tags: Record<string, string>;
|
||||
}) {
|
||||
return await api.makeCall({
|
||||
path: AUTOMTAION_ITEM_TAGS,
|
||||
data: {
|
||||
automation_id,
|
||||
tags,
|
||||
},
|
||||
});
|
||||
},
|
||||
async remove({ index }: { index: number }) {
|
||||
return await api.makeCall({
|
||||
@@ -29,10 +48,9 @@ export const makeAutomationAPI = (api: API) => ({
|
||||
path: AUTOMTAION_ITEM,
|
||||
data: {
|
||||
index,
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
export type AutomationAPI = ReturnType<typeof makeAutomationAPI>;
|
||||
@@ -1,15 +1,16 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useDefaultApiState, makeReloadAutomations } from './util';
|
||||
import { AutomationAPI } from './automations';
|
||||
import { useEffect } from "react";
|
||||
import { useDefaultApiState, makeReloadAutomations } from "./util";
|
||||
import { AutomationAPI } from "./automations";
|
||||
|
||||
export type ApiService = ReturnType<typeof useAPIService>;
|
||||
export const useAPIService = (
|
||||
automationAPI: AutomationAPI
|
||||
) => {
|
||||
export const useAPIService = (automationAPI: AutomationAPI) => {
|
||||
// state
|
||||
const [state, setState] = useDefaultApiState();
|
||||
const { reload, wrapCall } = makeReloadAutomations(automationAPI, state, setState);
|
||||
|
||||
const { reload, wrapCall } = makeReloadAutomations(
|
||||
automationAPI,
|
||||
state,
|
||||
setState
|
||||
);
|
||||
|
||||
// initial load of autos
|
||||
useEffect(() => {
|
||||
@@ -21,5 +22,6 @@ export const useAPIService = (
|
||||
state,
|
||||
removeAutomation: wrapCall(automationAPI.remove),
|
||||
updateAutomation: wrapCall(automationAPI.update),
|
||||
}
|
||||
}
|
||||
updateTags: wrapCall(automationAPI.updateTags),
|
||||
};
|
||||
};
|
||||
@@ -1,57 +1,93 @@
|
||||
import { useMockAPI } from "./mock"
|
||||
import { useMockAPI } from "./mock";
|
||||
import { makeAutomationAPI } from "./automations";
|
||||
import { createMockAuto } from '../utils/mocks';
|
||||
import { createMockAuto } from "../utils/mocks";
|
||||
import { AutomationData } from "types/automations";
|
||||
|
||||
const fakeUseRef = (a: any) => ({ current: a });
|
||||
|
||||
test('mock api uses initial autos for initial population -- empty', async () => {
|
||||
test("mock api uses initial autos for initial population -- empty", async () => {
|
||||
const mockApi = makeAutomationAPI(useMockAPI([], fakeUseRef as any));
|
||||
const data = await mockApi.list({ limit: 10, offset: 0 });
|
||||
expect(data.ok).toBe(true);
|
||||
expect((data as any).data.data).toHaveLength(0);
|
||||
expect((data as any).data.totalItems).toBe(0);
|
||||
})
|
||||
});
|
||||
|
||||
test('mock api uses initial autos for initial population -- some data', async () => {
|
||||
const initials = [
|
||||
createMockAuto(),
|
||||
createMockAuto()
|
||||
];
|
||||
test("mock api uses initial autos for initial population -- some data", async () => {
|
||||
const initials = [createMockAuto(), createMockAuto()];
|
||||
const mockApi = makeAutomationAPI(useMockAPI(initials, fakeUseRef as any));
|
||||
const data = await mockApi.list({ limit: 10, offset: 0 });
|
||||
expect(data.ok).toBe(true);
|
||||
expect((data as any).data.data).toHaveLength(2);
|
||||
expect((data as any).data.totalItems).toBe(2);
|
||||
})
|
||||
});
|
||||
|
||||
test('populate mock api with some data', async () => {
|
||||
test("populate mock api with some data", async () => {
|
||||
const mockApi = makeAutomationAPI(useMockAPI([], fakeUseRef as any));
|
||||
await mockApi.update({index: 0, auto: createMockAuto()})
|
||||
await mockApi.update({ index: 0, auto: createMockAuto() });
|
||||
let data = await mockApi.list({ limit: 10, offset: 0 });
|
||||
expect(data.ok).toBe(true);
|
||||
expect((data as any).data.data).toHaveLength(1);
|
||||
expect((data as any).data.totalItems).toBe(1);
|
||||
await mockApi.update({index: 1, auto: createMockAuto()})
|
||||
await mockApi.update({ index: 1, auto: createMockAuto() });
|
||||
data = await mockApi.list({ limit: 10, offset: 0 });
|
||||
expect((data as any).data.data).toHaveLength(2);
|
||||
expect((data as any).data.totalItems).toBe(2);
|
||||
})
|
||||
});
|
||||
|
||||
test('delete/update data from mock api', async () => {
|
||||
const mockApi = makeAutomationAPI(useMockAPI([
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
], fakeUseRef as any));
|
||||
let data = (await mockApi.list({ limit: 10, offset: 0 }) as any).data.data;
|
||||
test("delete/update data from mock api", async () => {
|
||||
const mockApi = makeAutomationAPI(
|
||||
useMockAPI(
|
||||
[
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
],
|
||||
fakeUseRef as any
|
||||
)
|
||||
);
|
||||
let data = ((await mockApi.list({ limit: 10, offset: 0 })) as any).data.data;
|
||||
const toBeEdited = data[0];
|
||||
toBeEdited.metadata.alias = "Bob is my uncle";
|
||||
await mockApi.update({index: 0, auto: toBeEdited})
|
||||
data = (await mockApi.list({ limit: 10, offset: 0 }) as any).data.data;
|
||||
await mockApi.update({ index: 0, auto: toBeEdited });
|
||||
data = ((await mockApi.list({ limit: 10, offset: 0 })) as any).data.data;
|
||||
expect(data[0].metadata.alias).toEqual("Bob is my uncle");
|
||||
await mockApi.remove({index: 0})
|
||||
data = (await mockApi.list({ limit: 10, offset: 0 }) as any).data.data;
|
||||
await mockApi.remove({ index: 0 });
|
||||
data = ((await mockApi.list({ limit: 10, offset: 0 })) as any).data.data;
|
||||
expect(data[0].metadata.alias === "Bob is my uncle").toEqual(false);
|
||||
})
|
||||
});
|
||||
|
||||
test("update tags from mock api", async () => {
|
||||
const mockApi = makeAutomationAPI(
|
||||
useMockAPI(
|
||||
[
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
createMockAuto(),
|
||||
],
|
||||
fakeUseRef as any
|
||||
)
|
||||
);
|
||||
let data = ((await mockApi.list({ limit: 10, offset: 0 })) as any).data
|
||||
.data as AutomationData[];
|
||||
const autoId = data[0].metadata.id;
|
||||
expect(data[0].tags).toStrictEqual({});
|
||||
mockApi.updateTags({
|
||||
automation_id: autoId,
|
||||
tags: {
|
||||
room: "bathroom",
|
||||
type: "popup",
|
||||
},
|
||||
});
|
||||
data = ((await mockApi.list({ limit: 10, offset: 0 })) as any).data
|
||||
.data as AutomationData[];
|
||||
expect(data[0].metadata.id).toEqual(autoId);
|
||||
expect(data[0].tags).toStrictEqual({
|
||||
room: "bathroom",
|
||||
type: "popup",
|
||||
});
|
||||
});
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -1,5 +1,4 @@
|
||||
|
||||
|
||||
export const AUTOMTAION_ROOT = '/' as const;
|
||||
export const AUTOMTAION_ROOT = "/" as const;
|
||||
export const AUTOMTAION_LIST = `${AUTOMTAION_ROOT}list` as const;
|
||||
export const AUTOMTAION_ITEM = `${AUTOMTAION_ROOT}item` as const;
|
||||
export const AUTOMTAION_ITEM_TAGS = `${AUTOMTAION_ITEM}/tags` as const;
|
||||
Reference in new issue
Block a user