Merge pull request #9 from Hexastack/fix/refactor-settings-load

Fix/refactor settings load
This commit is contained in:
Mohamed Marrouchi 2024-09-13 15:57:34 +01:00 committed by GitHub
commit 2a5e0f6f42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 39 deletions

View File

@ -58,16 +58,6 @@ export class SettingController {
return await this.settingService.find(filters, sort); 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`. * Updates a setting by its ID. If the setting does not exist, throws a `NotFoundException`.
* *

View File

@ -10,12 +10,12 @@
import { useEffect } from "react"; import { useEffect } from "react";
import { useMutation, useQuery } from "react-query"; 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 { ILoginAttributes } from "@/types/auth/login.types";
import { IUserPermissions } from "@/types/auth/permission.types"; import { IUserPermissions } from "@/types/auth/permission.types";
import { ISetting } from "@/types/setting.types";
import { IUser, IUserAttributes, IUserStub } from "@/types/user.types"; import { IUser, IUserAttributes, IUserStub } from "@/types/user.types";
import { useFind } from "../crud/useFind";
import { useApiClient } from "../useApiClient"; import { useApiClient } from "../useApiClient";
import { useAuth } from "../useAuth"; import { useAuth } from "../useAuth";
import { useLocalStorageState } from "../useLocalStorageState"; import { useLocalStorageState } from "../useLocalStorageState";
@ -128,26 +128,31 @@ export const useConfirmAccount = (
}); });
}; };
export const SETTINGS_STORAGE_KEY = "settings";
export const useLoadSettings = () => { export const useLoadSettings = () => {
const { apiClient } = useApiClient();
const { isAuthenticated } = useAuth(); const { isAuthenticated } = useAuth();
const { persist, value } = useLocalStorageState(SETTINGS_STORAGE_KEY); const { data: settings, ...rest } = useFind(
const storedSettings = value { entity: EntityType.SETTING },
? (JSON.parse(value || JSON.stringify({})) as { [key: string]: ISetting[] }) {
: undefined; hasCount: false,
initialSortState: [{ field: "weight", sort: "desc" }],
},
{
enabled: isAuthenticated,
},
);
return useQuery({ return {
enabled: isAuthenticated, ...rest,
queryKey: [SETTINGS_STORAGE_KEY, Format.BASIC], data:
async queryFn() { settings?.reduce((acc, curr) => {
return await apiClient.getSettings(); const group = acc[curr.group] || [];
},
onSuccess(data) { group.push(curr);
persist(JSON.stringify(data)); acc[curr.group] = group;
},
initialData: storedSettings || ({} as { [key: string]: ISetting[] }), return acc;
}); }, {}) || {},
};
}; };
export const useUpdateProfile = ( export const useUpdateProfile = (

View File

@ -16,7 +16,6 @@ import { ICsrf } from "@/types/csrf.types";
import { IInvitation, IInvitationAttributes } from "@/types/invitation.types"; import { IInvitation, IInvitationAttributes } from "@/types/invitation.types";
import { INlpDatasetSampleAttributes } from "@/types/nlp-sample.types"; import { INlpDatasetSampleAttributes } from "@/types/nlp-sample.types";
import { IResetPayload, IResetRequest } from "@/types/reset.types"; import { IResetPayload, IResetRequest } from "@/types/reset.types";
import { ISetting } from "@/types/setting.types";
import { IUser, IUserAttributes, IUserStub } from "@/types/user.types"; import { IUser, IUserAttributes, IUserStub } from "@/types/user.types";
import { EntityType, Format, TCount, TypeByFormat } from "./types"; import { EntityType, Format, TCount, TypeByFormat } from "./types";
@ -34,7 +33,6 @@ export const ROUTES = {
CSRF: "/csrftoken", CSRF: "/csrftoken",
BOTSTATS: "/botstats", BOTSTATS: "/botstats",
REFRESH_TRANSLATIONS: "/translation/refresh", REFRESH_TRANSLATIONS: "/translation/refresh",
LOAD_SETTINGS: "/setting/load",
RESET: "/user/reset", RESET: "/user/reset",
NLP_SAMPLE_IMPORT: "/nlpsample/import", NLP_SAMPLE_IMPORT: "/nlpsample/import",
NLP_SAMPLE_PREDICT: "/nlpsample/message", NLP_SAMPLE_PREDICT: "/nlpsample/message",
@ -191,14 +189,6 @@ export class ApiClient {
return data; return data;
} }
async getSettings() {
const { data } = await this.request.get<{
[key: string]: ISetting[];
}>(ROUTES.LOAD_SETTINGS);
return data;
}
async reset(token: string, payload: IResetPayload) { async reset(token: string, payload: IResetPayload) {
const { data } = await this.request.post< const { data } = await this.request.post<
IResetPayload, IResetPayload,