Skip to content

Commit

Permalink
feat: Add langs in config.json
Browse files Browse the repository at this point in the history
  • Loading branch information
aXenDeveloper committed Jun 13, 2024
1 parent 02ef9a4 commit 0574a78
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 15 deletions.
4 changes: 4 additions & 0 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ export interface ConfigType {
};
sticky: boolean;
};
langs: {
code: string;
enabled: boolean;
}[];
rebuild_required: {
langs: boolean;
plugins: boolean;
Expand Down
22 changes: 19 additions & 3 deletions backend/src/functions/update-object.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
export function updateObject<T>(config: T, defaultData: T): T {
export function updateObject<T extends Record<string, any>>(
config: T,
defaultData: T
): T {
const updatedConfig = config;
for (const key in defaultData) {
if (typeof defaultData[key] === "object" && defaultData[key] !== null) {
if (Array.isArray(defaultData[key])) {
// If the key corresponds to an array and it's not empty, don't edit
if (!config[key] || config[key].length === 0) {
updatedConfig[key] = [] as any;
}
} else if (
typeof defaultData[key] === "object" &&
defaultData[key] !== null
) {
// Handle nested objects
if (!config[key]) {
updatedConfig[key] = {} as T[Extract<keyof T, string>];
}
updateObject(config[key], defaultData[key]);
updatedConfig[key] = updateObject(
(config[key] || {}) as T[Extract<keyof T, string>],
defaultData[key]
);
} else {
// Handle primitive values
if (!config[key]) {
updatedConfig[key] = defaultData[key];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { UseGuards } from "@nestjs/common";

import { EditAdminMembersService } from "./edit.service";
import { EditAdminMembersArgs } from "./dto/edit.args";
import { EditAdminMembersObj } from "./dto/edit.obj";

import { AdminAuthGuards } from "@/utils/guards/admin-auth.guard";
import { EditAdminMembersObj } from "./dto/edit.obj";

@Resolver()
export class EditAdminMembersResolver {
Expand Down
5 changes: 2 additions & 3 deletions backend/src/plugins/core/admin/members/edit/edit.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Injectable } from "@nestjs/common";
import { eq } from "drizzle-orm";
import { NotFoundError, AccessDeniedError } from "@vitnode/backend";

import { EditAdminMembersArgs } from "./dto/edit.args";
import { EditAdminMembersObj } from "./dto/edit.obj";

import { core_users } from "@/plugins/core/admin/database/schema/users";
import { DatabaseService } from "@/database/database.service";
import { NotFoundError } from "@vitnode/backend";
import { AccessDeniedError } from "@vitnode/backend";
import { EditAdminMembersObj } from "./dto/edit.obj";

@Injectable()
export class EditAdminMembersService {
Expand Down
12 changes: 11 additions & 1 deletion frontend/config/generate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ import { DEFAULT_CONFIG_DATA } from ".";
if (!fs.existsSync(configPath)) {
fs.writeFile(
configPath,
JSON.stringify(DEFAULT_CONFIG_DATA, null, 2),
JSON.stringify(
{
...DEFAULT_CONFIG_DATA,
lang: [
{ code: "en", enabled: true },
{ code: "pl", enabled: true }
]
},
null,
2
),
"utf8",
err => {
if (err) throw err;
Expand Down
22 changes: 19 additions & 3 deletions frontend/config/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@ import { ConfigType } from ".";

export const configPath = join(process.cwd(), "config", "config.json");

export function updateObject<T>(config: T, defaultData: T): T {
export function updateObject<T extends Record<string, any>>(
config: T,
defaultData: T
): T {
const updatedConfig = config;
for (const key in defaultData) {
if (typeof defaultData[key] === "object" && defaultData[key] !== null) {
if (Array.isArray(defaultData[key])) {
// If the key corresponds to an array and it's not empty, don't edit
if (!config[key] || config[key].length === 0) {
updatedConfig[key] = [] as any;
}
} else if (
typeof defaultData[key] === "object" &&
defaultData[key] !== null
) {
// Handle nested objects
if (!config[key]) {
updatedConfig[key] = {} as T[Extract<keyof T, string>];
}
updateObject(config[key], defaultData[key]);
updatedConfig[key] = updateObject(
(config[key] || {}) as T[Extract<keyof T, string>],
defaultData[key]
);
} else {
// Handle primitive values
if (!config[key]) {
updatedConfig[key] = defaultData[key];
}
Expand Down
16 changes: 15 additions & 1 deletion frontend/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export interface ConfigType {
};
sticky: boolean;
};
langs: {
code: string;
enabled: boolean;
}[];
rebuild_required: {
langs: boolean;
plugins: boolean;
Expand Down Expand Up @@ -43,7 +47,17 @@ export const DEFAULT_CONFIG_DATA: ConfigType = {
color_primary: "hsl(220, 74%, 50%)",
color_primary_foreground: "hsl(210, 40%, 98%)"
}
}
},
langs: [
{
code: "en",
enabled: true
},
{
code: "pl",
enabled: true
}
]
};

const ENVS = {
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/decorators/user.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createParamDecorator, ExecutionContext } from "@nestjs/common";
import { Field, GqlExecutionContext, Int, ObjectType } from "@nestjs/graphql";

import { TextLanguage } from "../utils";

export const CurrentUser = createParamDecorator(
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/utils/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Request, Response } from "express";

import { User } from "../decorators";

export interface AuthRequest extends Request {
Expand Down
4 changes: 1 addition & 3 deletions packages/frontend/src/helpers/flatten-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export type WithChildren<T extends object> = Omit<

export type FlatTree<T extends object> = WithChildren<T> & {
depth: number;
index: number;
parentId: number | string | null;
};

Expand All @@ -21,7 +20,7 @@ export function flattenTree<T extends object>({
depth?: number;
parentId?: number | string | null;
}): FlatTree<T>[] {
return tree.reduce<FlatTree<T>[]>((previousValue, currentValue, index) => {
return tree.reduce<FlatTree<T>[]>((previousValue, currentValue) => {
const children = currentValue.children
? flattenTree({
tree: currentValue.children,
Expand All @@ -36,7 +35,6 @@ export function flattenTree<T extends object>({
...currentValue,
parentId: parentId,
depth: depth,
index,
children
},
...children
Expand Down

0 comments on commit 0574a78

Please sign in to comment.