- Added configuration management page with table view and modal forms for adding/editing configurations. - Integrated Zustand for state management of configurations. - Implemented user management drawer for organizations with user addition/removal functionality. - Created user management page with CRUD operations for users. - Introduced admin store for user-related actions including user creation, deletion, and updates. - Developed reusable drawer component for UI consistency across user management. - Enhanced error handling and user feedback with toast notifications.
144 lines
3.8 KiB
TypeScript
144 lines
3.8 KiB
TypeScript
'use client';
|
|
import { create } from 'zustand';
|
|
import { query } from '@/modules/index';
|
|
import { toast as message } from 'sonner';
|
|
type OrgStore = {
|
|
showEdit: boolean;
|
|
setShowEdit: (showEdit: boolean) => void;
|
|
formData: any;
|
|
setFormData: (formData: any) => void;
|
|
showUserEdit: boolean;
|
|
setShowUserEdit: (showUserEdit: boolean) => void;
|
|
userFormData: any;
|
|
setUserFormData: (userFormData: any) => void;
|
|
loading: boolean;
|
|
setLoading: (loading: boolean) => void;
|
|
list: any[];
|
|
getList: () => Promise<void>;
|
|
updateData: (data: any) => Promise<any>;
|
|
deleteData: (id: string) => Promise<void>;
|
|
org: any;
|
|
setOrg: (org: any) => void;
|
|
users: { id: string; username: string; role?: string }[];
|
|
orgId: string;
|
|
setOrgId: (orgId: string) => void;
|
|
getOrg: () => Promise<any>;
|
|
addUser: (data: { userId?: string; username?: string; role?: string }) => Promise<any>;
|
|
removeUser: (userId: string) => Promise<void>;
|
|
};
|
|
export const useOrgStore = create<OrgStore>((set, get) => {
|
|
return {
|
|
showEdit: false,
|
|
setShowEdit: (showEdit) => set({ showEdit }),
|
|
formData: {},
|
|
setFormData: (formData) => set({ formData }),
|
|
loading: false,
|
|
setLoading: (loading) => set({ loading }),
|
|
showUserEdit: false,
|
|
setShowUserEdit: (showUserEdit) => set({ showUserEdit }),
|
|
userFormData: {},
|
|
setUserFormData: (userFormData) => set({ userFormData }),
|
|
list: [],
|
|
getList: async () => {
|
|
set({ loading: true });
|
|
|
|
const res = await query.post({
|
|
path: 'org',
|
|
key: 'list',
|
|
});
|
|
set({ loading: false });
|
|
if (res.code === 200) {
|
|
set({ list: res.data });
|
|
} else {
|
|
message.error(res.message || 'Request failed');
|
|
}
|
|
},
|
|
updateData: async (data) => {
|
|
const { getList } = get();
|
|
const res = await query.post({
|
|
path: 'org',
|
|
key: 'update',
|
|
data,
|
|
});
|
|
if (res.code === 200) {
|
|
message.success('Success');
|
|
set({ showEdit: false, formData: [] });
|
|
getList();
|
|
} else {
|
|
message.error(res.message || 'Request failed');
|
|
}
|
|
return res;
|
|
},
|
|
deleteData: async (id) => {
|
|
const { getList } = get();
|
|
const res = await query.post({
|
|
path: 'org',
|
|
key: 'delete',
|
|
payload: {
|
|
id,
|
|
}
|
|
});
|
|
if (res.code === 200) {
|
|
getList();
|
|
message.success('Success');
|
|
} else {
|
|
message.error(res.message || 'Request failed');
|
|
}
|
|
},
|
|
org: {},
|
|
setOrg: (org) => set({ org }),
|
|
orgId: '',
|
|
setOrgId: (orgId) => set({ orgId }),
|
|
users: [],
|
|
getOrg: async () => {
|
|
const { orgId } = get();
|
|
const res = await query.post({
|
|
path: 'org',
|
|
key: 'get',
|
|
payload: {
|
|
id: orgId,
|
|
}
|
|
});
|
|
if (res.code === 200) {
|
|
const { org, users } = res.data || {};
|
|
set({ org, users });
|
|
} else {
|
|
message.error(res.message || 'Request failed');
|
|
}
|
|
},
|
|
addUser: async (data) => {
|
|
const { orgId } = get();
|
|
const res = await query.post({
|
|
path: 'org-user',
|
|
key: 'operate',
|
|
data: { orgId, ...data, action: 'add' },
|
|
});
|
|
if (res.code === 200) {
|
|
message.success('Success');
|
|
get().getOrg();
|
|
} else {
|
|
message.error(res.message || 'Request failed');
|
|
}
|
|
return res
|
|
},
|
|
removeUser: async (userId: string) => {
|
|
const { orgId } = get();
|
|
const res = await query.post({
|
|
path: 'org-user',
|
|
key: 'operate',
|
|
data: {
|
|
orgId,
|
|
userId,
|
|
action: 'remove',
|
|
},
|
|
});
|
|
if (res.code === 200) {
|
|
message.success('Success');
|
|
get().getOrg();
|
|
} else {
|
|
message.error(res.message || 'Request failed');
|
|
}
|
|
},
|
|
};
|
|
});
|