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.

85 lines
2.3 KiB

  1. import { convertToBool } from '@/utils'
  2. import { atom } from 'jotai/index'
  3. import { IRole } from '@/types/roles'
  4. import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query'
  5. import { IApiResult, IPage } from '@/types'
  6. import systemServ from '@/service/system.ts'
  7. import { message } from 'antd'
  8. import { t } from '@/i18n.ts'
  9. type SearchParams = IPage & {
  10. key?: string
  11. }
  12. export const idAtom = atom(0)
  13. export const roleIdsAtom = atom<number[]>([])
  14. export const roleAtom = atom<IRole>(undefined as unknown as IRole)
  15. export const searchAtom = atom<SearchParams>({} as SearchParams)
  16. export const pageAtom = atom<IPage>({
  17. pageSize: 10,
  18. page: 1,
  19. })
  20. export const rolesAtom = atomWithQuery((get) => {
  21. return {
  22. queryKey: [ 'roles', get(searchAtom) ],
  23. queryFn: async ({ queryKey: [ , params ] }) => {
  24. return await systemServ.role.list(params as SearchParams)
  25. },
  26. select: res => {
  27. const data = res.data
  28. data.rows = data.rows?.map(row => {
  29. return {
  30. ...row,
  31. status: convertToBool(row.status)
  32. }
  33. })
  34. return data
  35. }
  36. }
  37. })
  38. //saveOrUpdateRoleAtom
  39. export const saveOrUpdateRoleAtom = atomWithMutation<IApiResult, IRole>((get) => {
  40. return {
  41. mutationKey: [ 'updateMenu' ],
  42. mutationFn: async (data) => {
  43. data.status = data.status ? '1' : '0'
  44. if (data.id === 0) {
  45. return await systemServ.role.add(data)
  46. }
  47. return await systemServ.role.update(data)
  48. },
  49. onSuccess: (res) => {
  50. const isAdd = !!res.data?.id
  51. message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功'))
  52. //更新列表
  53. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  54. // @ts-ignore fix
  55. get(queryClientAtom).invalidateQueries({ queryKey: [ 'roles', get(searchAtom) ] })
  56. return res
  57. }
  58. }
  59. })
  60. export const deleteRoleAtom = atomWithMutation((get) => {
  61. return {
  62. mutationKey: [ 'deleteMenu' ],
  63. mutationFn: async (ids: number[]) => {
  64. return await systemServ.role.batchDelete(ids ?? get(roleIdsAtom))
  65. },
  66. onSuccess: (res) => {
  67. message.success('message.deleteSuccess')
  68. //更新列表
  69. get(queryClientAtom).invalidateQueries({ queryKey: [ 'roles', get(searchAtom) ] })
  70. return res
  71. }
  72. }
  73. })