Skip to content

Commit

Permalink
added user profile api
Browse files Browse the repository at this point in the history
  • Loading branch information
asosnovsky committed Aug 29, 2022
1 parent 95ac286 commit 416d88b
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 11 deletions.
8 changes: 7 additions & 1 deletion webapp/src/apiService/core.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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),
};
};
9 changes: 6 additions & 3 deletions webapp/src/apiService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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())
);
14 changes: 8 additions & 6 deletions webapp/src/apiService/mock.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useMockAPI } from "./mock";
import { useAutoMockAPI } from "./mock";
import { makeAutomationAPI } from "./automations";
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 () => {
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);
Expand All @@ -15,15 +15,17 @@ 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);
expect((data as any).data.totalItems).toBe(2);
});

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);
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
40 changes: 39 additions & 1 deletion webapp/src/apiService/mock.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 changes: 2 additions & 0 deletions webapp/src/apiService/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 21 additions & 0 deletions webapp/src/apiService/profile.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions webapp/src/apiService/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ export interface ListData<D> {
export interface ApiState {
automations: Errorable<ListData<AutomationData>>;
}

export type UserProfile = {
theme: string;
lang: string;
};

0 comments on commit 416d88b

Please sign in to comment.