You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.0 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query'
  2. import systemServ from '@/service/system.ts'
  3. import { IApiResult, IPage } from '@/types'
  4. import { IDepartment } from '@/types/department'
  5. import { atom, createStore } from 'jotai'
  6. import { t } from 'i18next'
  7. import { message } from 'antd'
  8. const store = createStore()
  9. export const departPageAtom = atom<IPage>({})
  10. export const defaultDepart = {
  11. id: 0,
  12. parent_id: 0,
  13. name: '',
  14. manager_user_id: 0,
  15. phone: '',
  16. sort: 0,
  17. } as IDepartment
  18. export const batchIdsAtom = atom<number[]>([])
  19. export const selectedDepartAtom = atom<IDepartment>({} as IDepartment)
  20. export const departTreeAtom = atomWithQuery(() => {
  21. return {
  22. queryKey: [ 'departTree' ],
  23. queryFn: async () => {
  24. return await systemServ.dept.tree()
  25. },
  26. select: (res) => {
  27. return res.data.tree ?? []
  28. }
  29. }
  30. })
  31. export const saveOrUpdateDepartAtom = atomWithMutation<IApiResult, IDepartment>((get) => {
  32. return {
  33. mutationKey: [ 'saveOrUpdateDepart' ],
  34. mutationFn: async (data: IDepartment) => {
  35. if (data.id) {
  36. return await systemServ.dept.update(data)
  37. }
  38. return await systemServ.dept.add(data)
  39. },
  40. onSuccess: (res) => {
  41. const isAdd = !!res.data?.id
  42. message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功'))
  43. if (isAdd) {
  44. store.set(selectedDepartAtom, prev => ({
  45. ...prev,
  46. id: res.data.id
  47. }))
  48. }
  49. get(queryClientAtom).invalidateQueries({ queryKey: [ 'departTree' ] }).then()
  50. }
  51. }
  52. })
  53. export const deleteDepartAtom = atomWithMutation<IApiResult, number[]>((get) => {
  54. return {
  55. mutationKey: [ 'deleteDepart', get(batchIdsAtom) ],
  56. mutationFn: async (ids: number[]) => {
  57. return await systemServ.dept.batchDelete(ids)
  58. },
  59. onSuccess: () => {
  60. message.success(t('message.deleteSuccess', '删除成功'))
  61. store.set(batchIdsAtom, [])
  62. get(queryClientAtom).invalidateQueries({ queryKey: [ 'departTree' ] }).then()
  63. }
  64. }
  65. })