Skip to content

Commit

Permalink
feat: Add locker for change theme and langs when rebuild app is required
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Feb 21, 2024
1 parent 0273ce7 commit 4ca7094
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Field, ObjectType } from "@nestjs/graphql";

import { AuthorizationCurrentUserObj } from "@/src/core/sessions/authorization/dto/authorization.obj";
@ObjectType()
class RebuildRequiredObj {
export class RebuildRequiredObj {
@Field(() => Boolean)
themes: boolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AuthorizationCoreSessionsObj } from "./dto/authorization.obj";

import { Ctx } from "@/types/context.type";
import { DatabaseService } from "@/database/database.service";
import { getConfigFile } from "@/functions/config/get-config-file";

@Injectable()
export class AuthorizationCoreSessionsService {
Expand Down Expand Up @@ -69,6 +70,7 @@ export class AuthorizationCoreSessionsService {
res
}: Ctx): Promise<AuthorizationCoreSessionsObj> {
const theme_id = await this.getThemeId({ req });
const config = await getConfigFile();

try {
const currentUser = await this.service.authorization({ req, res });
Expand Down Expand Up @@ -98,12 +100,14 @@ export class AuthorizationCoreSessionsService {
}),
avatar_color: user.avatar_color
},
theme_id
theme_id,
rebuild_required: config.rebuild_required
};
} catch (error) {
return {
user: null,
theme_id
theme_id,
rebuild_required: config.rebuild_required
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Field, Int, ObjectType } from "@nestjs/graphql";

import { User } from "@/utils/decorators/user.decorator";
import { RebuildRequiredObj } from "@/src/admin/core/sessions/authorization/dto/authorization.obj";

@ObjectType()
export class AuthorizationCurrentUserObj extends User {
Expand All @@ -23,4 +24,7 @@ export class AuthorizationCoreSessionsObj {

@Field(() => Int, { nullable: true })
theme_id: number | null;

@Field(() => RebuildRequiredObj)
rebuild_required: RebuildRequiredObj;
}
1 change: 1 addition & 0 deletions backend/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type AuthorizationAdminSessionsObj {
}

type AuthorizationCoreSessionsObj {
rebuild_required: RebuildRequiredObj!
theme_id: Int
user: AuthorizationCurrentUserObj
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/admin/core/layout/header/header-admin.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LanguageSwitcher } from "@/components/switchers/language-switcher";
import { UserBarAdmin } from "./user-bar/user-bar-admin";
import { DarkLightModeSwitcher } from "@/components/switchers/dark-light-mode-switcher";
import { LangSwitcherHeaderAdmin } from "./lang-switcher";

export const HeaderAdmin = () => {
return (
Expand All @@ -16,7 +16,7 @@ export const HeaderAdmin = () => {
)}

<div className="ml-auto flex items-center justify-center gap-2">
<LanguageSwitcher />
<LangSwitcherHeaderAdmin />
<DarkLightModeSwitcher />
<UserBarAdmin />
</div>
Expand Down
15 changes: 15 additions & 0 deletions frontend/admin/core/layout/header/lang-switcher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use client";

import { LanguageSwitcher } from "@/components/switchers/language-switcher";

import { useSessionAdmin } from "../../hooks/use-session-admin";

export const LangSwitcherHeaderAdmin = () => {
const { rebuild_required } = useSessionAdmin();

if (rebuild_required.langs && process.env.NODE_ENV !== "development") {
return null;
}

return <LanguageSwitcher />;
};
3 changes: 2 additions & 1 deletion frontend/app/[locale]/(apps)/(main)/session-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const SessionProvider = ({ children, data }: Props) => {
value={{
session: data?.core_sessions__authorization.user,
theme_id: data?.core_sessions__authorization.theme_id ?? null,
nav: data?.core_nav__show.edges ?? []
nav: data?.core_nav__show.edges ?? [],
rebuild_required: data?.core_sessions__authorization.rebuild_required
}}
>
{children}
Expand Down
9 changes: 8 additions & 1 deletion frontend/components/switchers/language-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { Button } from "@/components/ui/button";
import { usePathname, useRouter } from "@/i18n";
import { useGlobals } from "@/hooks/core/use-globals";
import { useSession } from "@/hooks/core/use-session";

export const LanguageSwitcher = () => {
const t = useTranslations("core");
Expand All @@ -21,8 +22,14 @@ export const LanguageSwitcher = () => {
const { replace } = useRouter();
const pathname = usePathname();
const enableLocales = languages.filter(lang => lang.enabled);
const { rebuild_required } = useSession();

if (enableLocales.length <= 1) return null;
if (
enableLocales.length <= 1 ||
(rebuild_required && process.env.NODE_ENV !== "development")
) {
return null;
}

return (
<DropdownMenu>
Expand Down
4 changes: 2 additions & 2 deletions frontend/components/switchers/theme/theme-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import { mutationApi } from "./mutation-api";
export const ThemeSwitcher = () => {
const t = useTranslations("core");
const { themes } = useGlobals();
const { theme_id } = useSession();
const { rebuild_required, theme_id } = useSession();

if (themes.length <= 1) return null;
if (themes.length <= 1 || rebuild_required.themes) return null;

return (
<DropdownMenu>
Expand Down
8 changes: 7 additions & 1 deletion frontend/graphql/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type AuthorizationAdminSessionsObj = {

export type AuthorizationCoreSessionsObj = {
__typename?: 'AuthorizationCoreSessionsObj';
rebuild_required: RebuildRequiredObj;
theme_id?: Maybe<Scalars['Int']['output']>;
user?: Maybe<AuthorizationCurrentUserObj>;
};
Expand Down Expand Up @@ -1422,7 +1423,7 @@ export type Core_Members__ProfilesQuery = { __typename?: 'Query', core_members__
export type Core_Sessions__AuthorizationQueryVariables = Exact<{ [key: string]: never; }>;


export type Core_Sessions__AuthorizationQuery = { __typename?: 'Query', core_sessions__authorization: { __typename?: 'AuthorizationCoreSessionsObj', theme_id?: number | null, user?: { __typename?: 'AuthorizationCurrentUserObj', email: string, id: number, name_seo: string, is_admin: boolean, is_mod: boolean, name: string, newsletter: boolean, avatar_color: string, avatar?: { __typename?: 'AvatarUser', id: number, dir_folder: string, name: string } | null, group: { __typename?: 'GroupUser', id: number, name: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }> } } | null }, core_languages__show: { __typename?: 'ShowCoreLanguagesObj', edges: Array<{ __typename?: 'ShowCoreLanguages', code: string }> }, core_nav__show: { __typename?: 'ShowCoreNavObj', edges: Array<{ __typename?: 'ShowCoreNav', id: number, href: string, external: boolean, position: number, children: Array<{ __typename?: 'ShowCoreNavItem', id: number, position: number, external: boolean, href: string, description: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }>, name: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }> }>, description: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }>, name: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }> }> } };
export type Core_Sessions__AuthorizationQuery = { __typename?: 'Query', core_sessions__authorization: { __typename?: 'AuthorizationCoreSessionsObj', theme_id?: number | null, user?: { __typename?: 'AuthorizationCurrentUserObj', email: string, id: number, name_seo: string, is_admin: boolean, is_mod: boolean, name: string, newsletter: boolean, avatar_color: string, avatar?: { __typename?: 'AvatarUser', id: number, dir_folder: string, name: string } | null, group: { __typename?: 'GroupUser', id: number, name: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }> } } | null, rebuild_required: { __typename?: 'RebuildRequiredObj', themes: boolean, langs: boolean, plugins: boolean } }, core_languages__show: { __typename?: 'ShowCoreLanguagesObj', edges: Array<{ __typename?: 'ShowCoreLanguages', code: string }> }, core_nav__show: { __typename?: 'ShowCoreNavObj', edges: Array<{ __typename?: 'ShowCoreNav', id: number, href: string, external: boolean, position: number, children: Array<{ __typename?: 'ShowCoreNavItem', id: number, position: number, external: boolean, href: string, description: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }>, name: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }> }>, description: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }>, name: Array<{ __typename?: 'TextLanguage', language_code: string, value: string }> }> } };

export type Forum_Forums__ShowQueryVariables = Exact<{ [key: string]: never; }>;

Expand Down Expand Up @@ -2349,6 +2350,11 @@ export const Core_Sessions__Authorization = gql`
}
}
theme_id
rebuild_required {
themes
langs
plugins
}
}
core_languages__show {
edges {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ query Core_sessions__authorization {
}
}
theme_id
rebuild_required {
themes
langs
plugins
}
}
core_languages__show {
edges {
Expand Down
14 changes: 12 additions & 2 deletions frontend/hooks/core/use-session.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { createContext, useContext } from "react";

import type { AuthorizationCurrentUserObj, ShowCoreNav } from "@/graphql/hooks";
import type {
AuthorizationCurrentUserObj,
RebuildRequiredObj,
ShowCoreNav
} from "@/graphql/hooks";

interface Args {
nav: ShowCoreNav[];
rebuild_required: RebuildRequiredObj;
session: Omit<AuthorizationCurrentUserObj, "posts"> | undefined | null;
theme_id: number | null;
}

export const SessionContext = createContext<Args>({
session: null,
theme_id: null,
nav: []
nav: [],
rebuild_required: {
themes: false,
langs: false,
plugins: false
}
});

export const useSession = () => useContext(SessionContext);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"prepare": "npx husky install",
"config:init": "cd backend && pnpm config:init && cd ..",
"config:finish": "cd backend && pnpm config:finish && cd ..",
"backend:build": "cd backend && pnpm build && cd ..",
"backend:build": "cd backend && pnpm build && cd .. pnpm config:finish",
"backend:start": "cd backend && pnpm start",
"backend:dev": "cd backend && pnpm dev",
"frontend:build": "cd frontend && pnpm build && cd ..",
Expand Down

0 comments on commit 4ca7094

Please sign in to comment.