From 416d88b965db3e562c0b07f5be06adb584005148 Mon Sep 17 00:00:00 2001 From: Ari Date: Mon, 29 Aug 2022 12:51:07 -0400 Subject: [PATCH] added user profile api --- webapp/src/apiService/core.ts | 8 +++++- webapp/src/apiService/index.ts | 9 ++++--- webapp/src/apiService/mock.test.ts | 14 ++++++----- webapp/src/apiService/mock.ts | 40 +++++++++++++++++++++++++++++- webapp/src/apiService/paths.ts | 2 ++ webapp/src/apiService/profile.ts | 21 ++++++++++++++++ webapp/src/apiService/types.ts | 5 ++++ 7 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 webapp/src/apiService/profile.ts diff --git a/webapp/src/apiService/core.ts b/webapp/src/apiService/core.ts index fc98f19..7279911 100644 --- a/webapp/src/apiService/core.ts +++ b/webapp/src/apiService/core.ts @@ -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; -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), }; }; diff --git a/webapp/src/apiService/index.ts b/webapp/src/apiService/index.ts index 2414db4..5355d2a 100644 --- a/webapp/src/apiService/index.ts +++ b/webapp/src/apiService/index.ts @@ -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()) ); diff --git a/webapp/src/apiService/mock.test.ts b/webapp/src/apiService/mock.test.ts index 635aa81..e0fffcd 100644 --- a/webapp/src/apiService/mock.test.ts +++ b/webapp/src/apiService/mock.test.ts @@ -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(), diff --git a/webapp/src/apiService/mock.ts b/webapp/src/apiService/mock.ts index 23deb44..7f8eddc 100644 --- a/webapp/src/apiService/mock.ts +++ b/webapp/src/apiService/mock.ts @@ -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, + }; + }, + }; +}; diff --git a/webapp/src/apiService/paths.ts b/webapp/src/apiService/paths.ts index e5865c7..ab81588 100644 --- a/webapp/src/apiService/paths.ts +++ b/webapp/src/apiService/paths.ts @@ -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; diff --git a/webapp/src/apiService/profile.ts b/webapp/src/apiService/profile.ts new file mode 100644 index 0000000..836fade --- /dev/null +++ b/webapp/src/apiService/profile.ts @@ -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({ + 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; diff --git a/webapp/src/apiService/types.ts b/webapp/src/apiService/types.ts index 09e8431..06712cc 100644 --- a/webapp/src/apiService/types.ts +++ b/webapp/src/apiService/types.ts @@ -33,3 +33,8 @@ export interface ListData { export interface ApiState { automations: Errorable>; } + +export type UserProfile = { + theme: string; + lang: string; +};