settings -> dark/light mode
This commit is contained in:
1 parent
416d88b965
commit
03a735b281
47 files changed
+867
-736
No files matched your search
@@ -1,5 +1,9 @@
|
||||
import { useEffect } from "react";
|
||||
import { useDefaultApiState, makeReloadAutomations } from "./util";
|
||||
import {
|
||||
useDefaultApiState,
|
||||
makeReloadAutomations,
|
||||
makeProfileManager,
|
||||
} from "./util";
|
||||
import { AutomationAPI } from "./automations";
|
||||
import { UserProfileAPI } from "./profile";
|
||||
|
||||
@@ -10,25 +14,36 @@ export const useAPIService = (
|
||||
) => {
|
||||
// state
|
||||
const [state, setState] = useDefaultApiState();
|
||||
const { reload, wrapCall } = makeReloadAutomations(
|
||||
automationAPI,
|
||||
const autoManager = makeReloadAutomations(automationAPI, state, setState);
|
||||
const userProfileManager = makeProfileManager(
|
||||
userProfileAPI,
|
||||
state,
|
||||
setState
|
||||
);
|
||||
|
||||
// initial load of autos
|
||||
useEffect(() => {
|
||||
reload();
|
||||
autoManager.reload();
|
||||
userProfileManager.reload();
|
||||
// eslint-disable-next-line
|
||||
}, []);
|
||||
|
||||
return {
|
||||
state,
|
||||
removeAutomation: wrapCall(automationAPI.remove),
|
||||
updateAutomation: wrapCall(automationAPI.update),
|
||||
createAutomation: wrapCall(automationAPI.create),
|
||||
updateTags: wrapCall(automationAPI.updateTags),
|
||||
getProfile: wrapCall(userProfileAPI.get),
|
||||
setProfile: wrapCall(userProfileAPI.update),
|
||||
state: {
|
||||
...state,
|
||||
get theme(): "dark" | "light" {
|
||||
if (state.userProfile.ready && state.userProfile.ok) {
|
||||
if (["dark", "light"].includes(state.userProfile.data.theme)) {
|
||||
return state.userProfile.data.theme as any;
|
||||
}
|
||||
}
|
||||
return "dark";
|
||||
},
|
||||
},
|
||||
removeAutomation: autoManager.wrapCall(automationAPI.remove),
|
||||
updateAutomation: autoManager.wrapCall(automationAPI.update),
|
||||
createAutomation: autoManager.wrapCall(automationAPI.create),
|
||||
updateTags: autoManager.wrapCall(automationAPI.updateTags),
|
||||
setProfile: userProfileManager.update,
|
||||
};
|
||||
};
|
||||
@@ -5,6 +5,7 @@ import { USER_PROFILE_ROOT } from "./paths";
|
||||
export const makeProfileAPI = (api: API) => ({
|
||||
async get() {
|
||||
const data = await api.makeCall<UserProfile>({
|
||||
method: "GET",
|
||||
path: USER_PROFILE_ROOT,
|
||||
});
|
||||
return data;
|
||||
|
||||
@@ -32,6 +32,7 @@ export interface ListData<D> {
|
||||
|
||||
export interface ApiState {
|
||||
automations: Errorable<ListData<AutomationData>>;
|
||||
userProfile: Errorable<UserProfile>;
|
||||
}
|
||||
|
||||
export type UserProfile = {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { useState } from "react";
|
||||
import { ApiState } from "./types";
|
||||
import { AutomationAPI } from "./automations";
|
||||
import { UserProfileAPI } from "./profile";
|
||||
import { UserProfile } from "apiService/types";
|
||||
|
||||
export const useDefaultApiState = () =>
|
||||
useState<ApiState>({
|
||||
automations: { ready: false },
|
||||
userProfile: { ready: false },
|
||||
});
|
||||
|
||||
export const makeReloadAutomations = (
|
||||
@@ -38,3 +41,41 @@ export const makeReloadAutomations = (
|
||||
wrapCall,
|
||||
};
|
||||
};
|
||||
|
||||
export const makeProfileManager = (
|
||||
userProfileAPI: UserProfileAPI,
|
||||
state: ApiState,
|
||||
setState: (s: ApiState) => void
|
||||
) => {
|
||||
const reload = () =>
|
||||
userProfileAPI
|
||||
.get()
|
||||
.then((data) => {
|
||||
setState({
|
||||
...state,
|
||||
userProfile: {
|
||||
ready: true,
|
||||
...data,
|
||||
},
|
||||
});
|
||||
})
|
||||
.catch((error) =>
|
||||
setState({
|
||||
...state,
|
||||
userProfile: {
|
||||
ready: true,
|
||||
ok: false,
|
||||
error: JSON.stringify(error),
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
reload,
|
||||
update: (data: UserProfile) =>
|
||||
userProfileAPI
|
||||
.update(data)
|
||||
.then(() => reload())
|
||||
.catch(() => reload()),
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user