2025-02-28 15:31:32 +08:00

98 lines
2.5 KiB
TypeScript

import { create } from 'zustand';
import { query } from '@/modules';
import { message } from '@/modules/message';
import { sortBy } from 'lodash-es';
type FileStore = {
showEdit: boolean;
setShowEdit: (showEdit: boolean) => void;
formData: any;
setFormData: (formData: any) => void;
loading: boolean;
setLoading: (loading: boolean) => void;
path: string;
setPath: (path: string) => void;
list: any[];
getList: () => Promise<void>;
updateData: (data: any) => Promise<void>;
deleteData: (id: string) => Promise<void>;
getFile: (path: string) => Promise<any>;
file: any;
setFile: (file: any) => void;
};
export const useFileStore = create<FileStore>((set, get) => {
return {
showEdit: false,
setShowEdit: (showEdit) => set({ showEdit }),
formData: {},
setFormData: (formData) => set({ formData }),
loading: false,
setLoading: (loading) => set({ loading }),
path: '',
setPath: (path) => set({ path }),
list: [],
getList: async () => {
const { path } = get();
set({ loading: true });
const res = await query.post({
path: 'file',
key: 'list',
data: {
prefix: path,
},
});
set({ loading: false });
if (res.code === 200) {
const list = res.data;
const sortedList = sortBy(list, [(item) => !item.prefix]);
set({ list: sortedList });
} else {
message.error(res.message || 'Request failed');
}
},
updateData: async (data) => {
const { getList } = get();
const res = await query.post({
path: 'file',
key: 'update',
data,
});
if (res.code === 200) {
message.success('Success');
set({ showEdit: false, formData: res.data });
getList();
} else {
message.error(res.message || 'Request failed');
}
},
deleteData: async (id) => {
const { getList } = get();
const res = await query.post({
path: 'file',
key: 'delete',
id,
});
if (res.code === 200) {
getList();
message.success('Success');
} else {
message.error(res.message || 'Request failed');
}
},
getFile: async (path) => {
const res = await query.post({
path: 'file',
key: 'stat',
data: { prefix: path },
});
if (res.code === 200) {
set({ file: res.data });
} else {
message.error(res.message || 'Request failed');
}
},
file: {},
setFile: (file) => set({ file }),
};
});