mirror of
https://github.com/hexastack/hexabot
synced 2025-01-22 10:35:37 +00:00
Merge pull request #9 from Hexastack/fix/refactor-settings-load
Fix/refactor settings load
This commit is contained in:
commit
2a5e0f6f42
@ -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`.
|
||||
*
|
||||
|
@ -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 = (
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user