added user profile api

This commit is contained in:
ari committed 2022-08-29 12:51:24 -04:00
1 parent 95ac286b53
commit 416d88b965
7 files changed
+88 -11

No files matched your search

+7 -1
View File
@@ -1,9 +1,13 @@
import { useEffect } from "react";
import { useDefaultApiState, makeReloadAutomations } from "./util";
import { AutomationAPI } from "./automations";
import { UserProfileAPI } from "./profile";
export type ApiService = ReturnType<typeof useAPIService>;
export const useAPIService = (automationAPI: AutomationAPI) => {
export const useAPIService = (
automationAPI: AutomationAPI,
userProfileAPI: UserProfileAPI
) => {
// state
const [state, setState] = useDefaultApiState();
const { reload, wrapCall } = makeReloadAutomations(
@@ -24,5 +28,7 @@ export const useAPIService = (automationAPI: AutomationAPI) => {
updateAutomation: wrapCall(automationAPI.update),
createAutomation: wrapCall(automationAPI.create),
updateTags: wrapCall(automationAPI.updateTags),
getProfile: wrapCall(userProfileAPI.get),
setProfile: wrapCall(userProfileAPI.update),
};
};
+6 -3
View File
@@ -2,8 +2,9 @@ import { AutomationData } from "types/automations";
import { makeAutomationAPI } from "./automations";
import { makeRemoteAPI } from "./base";
import { useAPIService } from "./core";
import { useMockAPI } from "./mock";
import { useAutoMockAPI, useProfileMockAPI } from "./mock";
import { useRef } from "react";
import { makeProfileAPI } from "./profile";
const locationPrefixWeb = window.location.pathname.match(/(\/.+\/)web/i);
const baseURL = new URL(
@@ -13,12 +14,14 @@ const baseURL = new URL(
);
export const wsURL = baseURL + "socket";
export const remoteAutoAPI = makeRemoteAPI(baseURL + "automations");
export const detailsAPI = makeRemoteAPI(baseURL + "details/");
export const useConnectedApiService = () =>
useAPIService(makeAutomationAPI(remoteAutoAPI));
useAPIService(makeAutomationAPI(remoteAutoAPI), makeProfileAPI(detailsAPI));
export const useMockApiService = (
initialAutos: AutomationData[],
returnErrors: boolean = false
) =>
useAPIService(
makeAutomationAPI(useMockAPI(initialAutos, useRef, returnErrors))
makeAutomationAPI(useAutoMockAPI(initialAutos, useRef, returnErrors)),
makeProfileAPI(useProfileMockAPI())
);
+8 -6
View File
@@ -1,4 +1,4 @@
import { useMockAPI } from "./mock";
import { useAutoMockAPI } from "./mock";
import { makeAutomationAPI } from "./automations";
import { createMockAuto } from "../utils/mocks";
import { AutomationData } from "types/automations";
@@ -6,7 +6,7 @@ import { AutomationData } from "types/automations";
const fakeUseRef = (a: any) => ({ current: a });
test("mock api uses initial autos for initial population -- empty", async () => {
const mockApi = makeAutomationAPI(useMockAPI([], fakeUseRef as any));
const mockApi = makeAutomationAPI(useAutoMockAPI([], 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);
@@ -15,7 +15,9 @@ test("mock api uses initial autos for initial population -- empty", async () =>
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 mockApi = makeAutomationAPI(
useAutoMockAPI(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);
@@ -23,7 +25,7 @@ test("mock api uses initial autos for initial population -- some data", async ()
});
test("populate mock api with some data", async () => {
const mockApi = makeAutomationAPI(useMockAPI([], fakeUseRef as any));
const mockApi = makeAutomationAPI(useAutoMockAPI([], fakeUseRef as any));
await mockApi.create(createMockAuto());
let data = await mockApi.list({ limit: 10, offset: 0 });
expect(data.ok).toBe(true);
@@ -37,7 +39,7 @@ test("populate mock api with some data", async () => {
test("delete/update data from mock api", async () => {
const mockApi = makeAutomationAPI(
useMockAPI(
useAutoMockAPI(
[
createMockAuto(),
createMockAuto(),
@@ -61,7 +63,7 @@ test("delete/update data from mock api", async () => {
test("update tags from mock api", async () => {
const mockApi = makeAutomationAPI(
useMockAPI(
useAutoMockAPI(
[
createMockAuto(),
createMockAuto(),
+39 -1
View File
@@ -1,13 +1,15 @@
import { useRef } from "react";
import { AutomationData, BareAutomationData } from "types/automations";
import { API } from "./base";
import { UserProfile } from "./types";
import {
AUTOMTAION_LIST,
AUTOMTAION_ITEM,
AUTOMTAION_ITEM_TAGS,
USER_PROFILE_ROOT,
} from "./paths";
export const useMockAPI = (
export const useAutoMockAPI = (
initialAutos: AutomationData[] = [],
makeRef = useRef,
returnErrors: boolean = false
@@ -105,3 +107,39 @@ export const useMockAPI = (
},
};
};
export const useProfileMockAPI = (
initialProfile: UserProfile = {
lang: "eng",
theme: "dark",
},
makeRef = useRef,
returnErrors: boolean = false
): API => {
const profileRef = makeRef(initialProfile);
return {
async makeCall({ path, method = "POST", data = {} }) {
if (returnErrors) {
return {
ok: false,
error: "Test Error",
};
}
if (path === USER_PROFILE_ROOT) {
if (method === "GET") {
return {
ok: true,
data: JSON.parse(JSON.stringify(profileRef.current)),
} as any;
} else if (method === "PUT") {
profileRef.current = data as any;
return { saved: "" };
}
}
return {
ok: false,
error: "PATH NOT FOUND " + method + ": " + path,
};
},
};
};
+2
View File
@@ -2,3 +2,5 @@ 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;
export const USER_PROFILE_ROOT = "/profile" as const;
+21
View File
@@ -0,0 +1,21 @@
import { API } from "./base";
import { UserProfile } from "./types";
import { USER_PROFILE_ROOT } from "./paths";
export const makeProfileAPI = (api: API) => ({
async get() {
const data = await api.makeCall<UserProfile>({
path: USER_PROFILE_ROOT,
});
return data;
},
async update(data: UserProfile) {
return await api.makeCall({
method: "PUT",
path: USER_PROFILE_ROOT,
data,
});
},
});
export type UserProfileAPI = ReturnType<typeof makeProfileAPI>;
+5
View File
@@ -33,3 +33,8 @@ export interface ListData<D> {
export interface ApiState {
automations: Errorable<ListData<AutomationData>>;
}
export type UserProfile = {
theme: string;
lang: string;
};