|
@ -9,9 +9,9 @@ import { message } from 'antd' |
|
|
|
|
|
|
|
|
const store = createStore() |
|
|
const store = createStore() |
|
|
|
|
|
|
|
|
const departPageAtom = atom<IPage>({}) |
|
|
|
|
|
|
|
|
export const departPageAtom = atom<IPage>({}) |
|
|
|
|
|
|
|
|
const defaultDepart = { |
|
|
|
|
|
|
|
|
export const defaultDepart = { |
|
|
id: 0, |
|
|
id: 0, |
|
|
parent_id: 0, |
|
|
parent_id: 0, |
|
|
name: '', |
|
|
name: '', |
|
@ -20,11 +20,11 @@ const defaultDepart = { |
|
|
sort: 0, |
|
|
sort: 0, |
|
|
} as IDepartment |
|
|
} as IDepartment |
|
|
|
|
|
|
|
|
const batchIdsAtom = atom<number[]>([]) |
|
|
|
|
|
|
|
|
export const batchIdsAtom = atom<number[]>([]) |
|
|
|
|
|
|
|
|
const selectedDepartAtom = atom<IDepartment>({} as IDepartment) |
|
|
|
|
|
|
|
|
export const selectedDepartAtom = atom<IDepartment>({} as IDepartment) |
|
|
|
|
|
|
|
|
const departTreeAtom = atomWithQuery(() => { |
|
|
|
|
|
|
|
|
export const departTreeAtom = atomWithQuery(() => { |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
queryKey: [ 'departTree' ], |
|
|
queryKey: [ 'departTree' ], |
|
@ -37,10 +37,10 @@ const departTreeAtom = atomWithQuery(() => { |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
const saveOrUpdateDepartAtom = atomWithMutation<IApiResult, IDepartment>((get) => { |
|
|
|
|
|
|
|
|
export const saveOrUpdateDepartAtom = atomWithMutation<IApiResult, IDepartment>((get) => { |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
mutationKey: [ 'saveOrUpdateDepart', get(selectedDepartAtom) ], |
|
|
|
|
|
|
|
|
mutationKey: [ 'saveOrUpdateDepart' ], |
|
|
mutationFn: async (data: IDepartment) => { |
|
|
mutationFn: async (data: IDepartment) => { |
|
|
if (data.id) { |
|
|
if (data.id) { |
|
|
return await systemServ.dept.update(data) |
|
|
return await systemServ.dept.update(data) |
|
@ -50,18 +50,22 @@ const saveOrUpdateDepartAtom = atomWithMutation<IApiResult, IDepartment>((get) = |
|
|
onSuccess: (res) => { |
|
|
onSuccess: (res) => { |
|
|
const isAdd = !!res.data?.id |
|
|
const isAdd = !!res.data?.id |
|
|
message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功')) |
|
|
message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功')) |
|
|
|
|
|
|
|
|
|
|
|
if (isAdd) { |
|
|
store.set(selectedDepartAtom, prev => ({ |
|
|
store.set(selectedDepartAtom, prev => ({ |
|
|
...prev, |
|
|
...prev, |
|
|
...(isAdd ? res.data : {}) |
|
|
|
|
|
|
|
|
id: res.data.id |
|
|
})) |
|
|
})) |
|
|
get(queryClientAtom).invalidateQueries({ queryKey: [ 'departTree', get(departPageAtom) ] }).then() |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
get(queryClientAtom).invalidateQueries({ queryKey: [ 'departTree' ] }).then() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const deleteDepartAtom = atomWithMutation<IApiResult, number[]>((get) => { |
|
|
|
|
|
|
|
|
export const deleteDepartAtom = atomWithMutation<IApiResult, number[]>((get) => { |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
mutationKey: [ 'deleteDepart', get(batchIdsAtom) ], |
|
|
mutationKey: [ 'deleteDepart', get(batchIdsAtom) ], |
|
@ -71,19 +75,10 @@ const deleteDepartAtom = atomWithMutation<IApiResult, number[]>((get) => { |
|
|
onSuccess: () => { |
|
|
onSuccess: () => { |
|
|
message.success(t('message.deleteSuccess', '删除成功')) |
|
|
message.success(t('message.deleteSuccess', '删除成功')) |
|
|
store.set(batchIdsAtom, []) |
|
|
store.set(batchIdsAtom, []) |
|
|
get(queryClientAtom).invalidateQueries({ queryKey: [ 'departTree', get(departPageAtom) ] }).then() |
|
|
|
|
|
|
|
|
get(queryClientAtom).invalidateQueries({ queryKey: [ 'departTree' ] }).then() |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
export const useDepartStore = () => ({ |
|
|
|
|
|
defaultDepart, |
|
|
|
|
|
departPageAtom, |
|
|
|
|
|
selectedDepartAtom, |
|
|
|
|
|
departTreeAtom, |
|
|
|
|
|
deleteDepartAtom, |
|
|
|
|
|
batchIdsAtom, |
|
|
|
|
|
saveOrUpdateDepartAtom, |
|
|
|
|
|
}) |
|
|
|