Skip to content

Commit

Permalink
Move theming logic from user to shared
Browse files Browse the repository at this point in the history
  • Loading branch information
pijng committed Mar 20, 2024
1 parent 6608fdd commit a911ba3
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 34 deletions.
2 changes: 1 addition & 1 deletion internal/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ info:
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.6.7
version: 1.6.8
externalDocs:
description: Find out more about spec
url: ''
Expand Down
5 changes: 3 additions & 2 deletions web/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { linkRouter, onAppMount } from "atomic-router-forest";
import { createEvent, sample } from "effector";
import { h, spec } from "forest";
import { Pages } from "./pages";
import { loadThemeFromStorageFx } from "@/shared/lib";

export const Application = () => {
h("body", () => {
Expand All @@ -18,7 +19,6 @@ export const Application = () => {
"text-slate-700": true,
"dark:bg-eigengrau": true,
"dark:text-slate-300": true,
dark: userModel.$currentTheme.map((theme) => theme === "dark"),
},
});

Expand Down Expand Up @@ -56,7 +56,8 @@ sample({

sample({
clock: appMounted,
target: [userModel.effects.loadThemeFromStorageFx, userModel.effects.loadLocaleFromStorageFx],
// TODO: move loadLocale to shared/lib/i18n
target: [loadThemeFromStorageFx, userModel.effects.loadLocaleFromStorageFx],
});

sample({
Expand Down
27 changes: 1 addition & 26 deletions web/src/entities/user/model.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { User, getUsers } from "@/shared/api";
import { getUser } from "@/shared/api/users";
import { User, getUsers, getUser } from "@/shared/api";
import { setCurrentAccount } from "@/shared/auth";
import { setLanguage } from "@/shared/lib/i18n";
import { createEffect, createEvent, createStore, sample } from "effector";

const THEME_KEY = "theme";
const LOCALE_KEY = "locale";

const getUsersFx = createEffect(() => {
Expand Down Expand Up @@ -42,28 +40,6 @@ sample({
target: setCurrentAccount,
});

const loadThemeFromStorageFx = createEffect(() => {
return localStorage.getItem(THEME_KEY) || "light";
});

const setThemeToStorageFx = createEffect((theme: string) => {
return localStorage.setItem(THEME_KEY, theme);
});

export const $currentTheme = createStore<string | null>("light");
export const themeChanged = createEvent<string>();

sample({
source: themeChanged,
fn: (theme) => theme.toLowerCase(),
target: [setThemeToStorageFx, $currentTheme],
});

sample({
source: loadThemeFromStorageFx.doneData,
target: $currentTheme,
});

const loadLocaleFromStorageFx = createEffect(() => {
return localStorage.getItem(LOCALE_KEY) || "en";
});
Expand Down Expand Up @@ -96,7 +72,6 @@ sample({
export const effects = {
getUsersFx,
getUserFx,
loadThemeFromStorageFx,
loadLocaleFromStorageFx,
};

Expand Down
4 changes: 2 additions & 2 deletions web/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<title>Moonlogs</title>
<script>
if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.body?.classList?.add('dark')
document.querySelector('html')?.classList?.add('dark')
} else {
document.body?.classList?.remove('dark')
document.querySelector('html')?.classList?.remove('dark')
}
</script>
</head>
Expand Down
2 changes: 1 addition & 1 deletion web/src/shared/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export {
} from "./schemas";
export { type Tag, type TagToCreate, type TagToUpdate, getTags, getTag, editTag, createTag, deleteTag } from "./tags";
export { type Log, type Level, getLogs } from "./logs";
export { type User, type UserToCreate, type UserToUpdate, getUsers, editUser, createUser } from "./users";
export { type User, type UserToCreate, type UserToUpdate, getUsers, getUser, editUser, createUser } from "./users";
export {
type ApiToken,
type ApiTokenToCreate,
Expand Down
1 change: 1 addition & 0 deletions web/src/shared/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { rules } from "./forms";
export { trigger } from "./trigger";
export { hashCode } from "./hash";
export { isObjectPresent } from "./presence";
export { themeChanged, $currentTheme, loadThemeFromStorageFx } from "./theming";
31 changes: 31 additions & 0 deletions web/src/shared/lib/theming/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createEffect, createEvent, createStore, sample } from "effector";

const THEME_KEY = "theme";

type Theme = "dark" | "light";

export const $currentTheme = createStore<Theme>("light");
export const themeChanged = createEvent<Theme>();

export const loadThemeFromStorageFx = createEffect(() => {
const theme: Theme = (localStorage.getItem(THEME_KEY) || "light") as Theme;

return theme;
});

const setThemeToStorageFx = createEffect((theme: Theme) => {
return localStorage.setItem(THEME_KEY, theme);
});

const applyThemeFx = createEffect((theme: Theme) => {
if (theme === "dark") {
return document.querySelector("html")?.classList?.add("dark");
}

return document.querySelector("html")?.classList?.remove("dark");
});

sample({
clock: [themeChanged, loadThemeFromStorageFx.doneData],
target: [applyThemeFx, setThemeToStorageFx, $currentTheme],
});
5 changes: 3 additions & 2 deletions web/src/widgets/user-profile/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { tagModel } from "@/entities/tag";
import { userModel } from "@/entities/user";
import { $currentTheme, themeChanged } from "@/shared/lib";
import { i18n } from "@/shared/lib/i18n";
import { Select } from "@/shared/ui";
import { combine, createStore } from "effector";
Expand Down Expand Up @@ -88,9 +89,9 @@ export const UserProfile = () => {
h("p", { text: i18n("profile.theme") });

Select({
value: userModel.$currentTheme,
value: $currentTheme,
options: createStore(["dark", "light"]),
optionSelected: userModel.themeChanged,
optionSelected: themeChanged,
withBlank: createStore(false),
});
});
Expand Down

0 comments on commit a911ba3

Please sign in to comment.