feat: 添加store
This commit is contained in:
@@ -7,7 +7,7 @@ export const queryConfig = new QueryConfig({ query: query as any });
|
||||
|
||||
interface ConfigStore {
|
||||
list: any[];
|
||||
getConfig: () => Promise<void>;
|
||||
getConfigList: () => Promise<void>;
|
||||
updateData: (data: any, opts?: { refresh?: boolean }) => Promise<any>;
|
||||
showEdit: boolean;
|
||||
setShowEdit: (showEdit: boolean) => void;
|
||||
@@ -15,11 +15,12 @@ interface ConfigStore {
|
||||
setFormData: (formData: any) => void;
|
||||
deleteConfig: (id: string) => Promise<void>;
|
||||
detectConfig: () => Promise<void>;
|
||||
onOpenKey: (key: string) => Promise<void>;
|
||||
}
|
||||
|
||||
export const useConfigStore = create<ConfigStore>((set, get) => ({
|
||||
list: [],
|
||||
getConfig: async () => {
|
||||
getConfigList: async () => {
|
||||
const res = await queryConfig.listConfig();
|
||||
if (res.code === 200) {
|
||||
set({ list: res.data?.list || [] });
|
||||
@@ -30,7 +31,7 @@ export const useConfigStore = create<ConfigStore>((set, get) => ({
|
||||
if (res.code === 200) {
|
||||
get().setFormData(res.data);
|
||||
if (opts?.refresh ?? true) {
|
||||
get().getConfig();
|
||||
get().getConfigList();
|
||||
}
|
||||
toast.success('保存成功');
|
||||
} else {
|
||||
@@ -45,7 +46,7 @@ export const useConfigStore = create<ConfigStore>((set, get) => ({
|
||||
deleteConfig: async (id: string) => {
|
||||
const res = await queryConfig.deleteConfig(id);
|
||||
if (res.code === 200) {
|
||||
get().getConfig();
|
||||
get().getConfigList();
|
||||
toast.success('删除成功');
|
||||
} else {
|
||||
toast.error('删除失败');
|
||||
@@ -61,4 +62,17 @@ export const useConfigStore = create<ConfigStore>((set, get) => ({
|
||||
toast.error('检测失败');
|
||||
}
|
||||
},
|
||||
onOpenKey: async (key: string) => {
|
||||
const { setFormData, setShowEdit, getConfigList } = get();
|
||||
const res = await queryConfig.getConfigByKey(key);
|
||||
if (res.code === 200) {
|
||||
const data = res.data;
|
||||
setFormData(data);
|
||||
setShowEdit(true);
|
||||
getConfigList();
|
||||
} else {
|
||||
console.log(res);
|
||||
toast.error('获取配置失败');
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user