Skip to content

Commit

Permalink
refactor: load settings
Browse files Browse the repository at this point in the history
  • Loading branch information
marrouchi committed Sep 13, 2024
1 parent 51931f8 commit d60c5df
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 38 deletions.
10 changes: 0 additions & 10 deletions api/src/setting/controllers/setting.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ export class SettingController {
return await this.settingService.find(filters, sort);
}

/**
* Loads all settings available in the system.
*
* @returns A list of all settings.
*/
@Get('load')
async load() {
return await this.settingService.load();
}

/**
* Updates a setting by its ID. If the setting does not exist, throws a `NotFoundException`.
*
Expand Down
41 changes: 23 additions & 18 deletions frontend/src/hooks/entities/auth-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import { useEffect } from "react";
import { useMutation, useQuery } from "react-query";

import { Format, TMutationOptions } from "@/services/types";
import { EntityType, TMutationOptions } from "@/services/types";
import { ILoginAttributes } from "@/types/auth/login.types";
import { IUserPermissions } from "@/types/auth/permission.types";
import { ISetting } from "@/types/setting.types";
import { IUser, IUserAttributes, IUserStub } from "@/types/user.types";

import { useFind } from "../crud/useFind";
import { useApiClient } from "../useApiClient";
import { useAuth } from "../useAuth";
import { useLocalStorageState } from "../useLocalStorageState";
Expand Down Expand Up @@ -128,26 +128,31 @@ export const useConfirmAccount = (
});
};

export const SETTINGS_STORAGE_KEY = "settings";
export const useLoadSettings = () => {
const { apiClient } = useApiClient();
const { isAuthenticated } = useAuth();
const { persist, value } = useLocalStorageState(SETTINGS_STORAGE_KEY);
const storedSettings = value
? (JSON.parse(value || JSON.stringify({})) as { [key: string]: ISetting[] })
: undefined;

return useQuery({
enabled: isAuthenticated,
queryKey: [SETTINGS_STORAGE_KEY, Format.BASIC],
async queryFn() {
return await apiClient.getSettings();
const { data: settings, ...rest } = useFind(
{ entity: EntityType.SETTING },
{
hasCount: false,
initialSortState: [{ field: "weight", sort: "desc" }],
},
onSuccess(data) {
persist(JSON.stringify(data));
{
enabled: isAuthenticated,
},
initialData: storedSettings || ({} as { [key: string]: ISetting[] }),
});
);

return {
...rest,
data:
settings?.reduce((acc, curr) => {
const group = acc[curr.group] || [];

group.push(curr);
acc[curr.group] = group;

return acc;
}, {}) || {},
};
};

export const useUpdateProfile = (
Expand Down
10 changes: 0 additions & 10 deletions frontend/src/services/api.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { ICsrf } from "@/types/csrf.types";
import { IInvitation, IInvitationAttributes } from "@/types/invitation.types";
import { INlpDatasetSampleAttributes } from "@/types/nlp-sample.types";
import { IResetPayload, IResetRequest } from "@/types/reset.types";
import { ISetting } from "@/types/setting.types";
import { IUser, IUserAttributes, IUserStub } from "@/types/user.types";

import { EntityType, Format, TCount, TypeByFormat } from "./types";
Expand All @@ -34,7 +33,6 @@ export const ROUTES = {
CSRF: "/csrftoken",
BOTSTATS: "/botstats",
REFRESH_TRANSLATIONS: "/translation/refresh",
LOAD_SETTINGS: "/setting/load",
RESET: "/user/reset",
NLP_SAMPLE_IMPORT: "/nlpsample/import",
NLP_SAMPLE_PREDICT: "/nlpsample/message",
Expand Down Expand Up @@ -191,14 +189,6 @@ export class ApiClient {
return data;
}

async getSettings() {
const { data } = await this.request.get<{
[key: string]: ISetting[];
}>(ROUTES.LOAD_SETTINGS);

return data;
}

async reset(token: string, payload: IResetPayload) {
const { data } = await this.request.post<
IResetPayload,
Expand Down

0 comments on commit d60c5df

Please sign in to comment.