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);
}
/**
* 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`.
*

View File

@ -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";
@ -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;
const { data: settings, ...rest } = useFind(
{ entity: EntityType.SETTING },
{
hasCount: false,
initialSortState: [{ field: "weight", sort: "desc" }],
},
{
enabled: isAuthenticated,
},
);
return useQuery({
enabled: isAuthenticated,
queryKey: [SETTINGS_STORAGE_KEY, Format.BASIC],
async queryFn() {
return await apiClient.getSettings();
},
onSuccess(data) {
persist(JSON.stringify(data));
},
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 = (

View File

@ -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";
@ -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",
@ -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,