functioning profiles

This commit is contained in:
ari committed 2022-08-31 15:15:30 -04:00
1 parent 8985312dd2
commit d890c1aa64
17 files changed
+136 -78

No files matched your search

+10 -4
View File
@@ -23,10 +23,16 @@ export const useAPIService = (
// initial load of autos
useEffect(() => {
autoManager.reload();
userProfileManager.reload();
// eslint-disable-next-line
}, []);
if (state.automations.ready === undefined) {
autoManager.reload();
}
}, [state.automations.ready, autoManager]);
// initial load of profile
useEffect(() => {
if (state.userProfile.ready === undefined) {
userProfileManager.reload();
}
}, [state.userProfile.ready, userProfileManager]);
return {
state: {
+1 -1
View File
@@ -15,7 +15,7 @@ const baseURL = new URL(
);
export const wsURL = baseURL + "socket";
export const remoteAutoAPI = makeRemoteAPI(baseURL + "automations");
export const detailsAPI = makeRemoteAPI(baseURL + "details/");
export const detailsAPI = makeRemoteAPI(baseURL + "details");
export const useConnectedApiService = () =>
useAPIService(makeAutomationAPI(remoteAutoAPI), makeProfileAPI(detailsAPI));
export const useMockApiService = (
+4 -3
View File
@@ -19,7 +19,8 @@ export type APIResponse<D> =
ok: false;
error: string;
};
export type Errorable<D> =
export type Waitable<D> =
| { ready: undefined }
| ({ ready: true } & APIResponse<D>)
| {
ready: false;
@@ -31,8 +32,8 @@ export interface ListData<D> {
}
export interface ApiState {
automations: Errorable<ListData<AutomationData>>;
userProfile: Errorable<UserProfile>;
automations: Waitable<ListData<AutomationData>>;
userProfile: Waitable<UserProfile>;
}
export type UserProfile = {
+12 -3
View File
@@ -6,8 +6,8 @@ import { UserProfile } from "apiService/types";
export const useDefaultApiState = () =>
useState<ApiState>({
automations: { ready: false },
userProfile: { ready: false },
automations: { ready: undefined },
userProfile: { ready: undefined },
});
export const makeReloadAutomations = (
@@ -76,6 +76,15 @@ export const makeProfileManager = (
userProfileAPI
.update(data)
.then(() => reload())
.catch(() => reload()),
.catch((err) =>
setState({
...state,
userProfile: {
ok: false,
error: String(err),
ready: true,
},
})
),
};
};