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