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.

141 lines
3.7 KiB

9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
  1. import { atom } from 'jotai'
  2. import { IApiResult, IPage } from '@/global'
  3. import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query'
  4. import { message } from 'antd'
  5. import { t } from 'i18next'
  6. import { XForm } from '@/types/x-form/model'
  7. import * as modelServ from '@/service/x-form/model'
  8. import { atomWithStorage } from 'jotai/utils'
  9. type SearchParams = IPage & {
  10. key?: string
  11. api: string
  12. }
  13. export const apiAtom = atomWithStorage('api', '')
  14. export const modelIdAtom = atom(0)
  15. export const modelIdsAtom = atom<number[]>([])
  16. export const modelAtom = atom<XForm.IModel>(undefined as unknown as XForm.IModel)
  17. export const modelSearchAtom = atom<SearchParams>({
  18. key: '',
  19. pageSize: 10,
  20. page: 1,
  21. } as SearchParams)
  22. export const modelPageAtom = atom<IPage>({
  23. pageSize: 10,
  24. page: 1,
  25. })
  26. export const modelCURDAtom = atomWithQuery<IApiResult<XForm.IModelCURD>, any, any>((get) => {
  27. const api = get(apiAtom)
  28. console.log('api', api)
  29. return {
  30. enabled: !!api,
  31. queryKey: [ 'modelCURD' ],
  32. queryFn: async () => {
  33. return await modelServ.model(api).proxy()
  34. },
  35. select: (res) => {
  36. return res.data.data
  37. }
  38. }
  39. })
  40. export const modelsAtom = atomWithQuery((get) => {
  41. const api = get(apiAtom)
  42. const curd = get(modelCURDAtom)
  43. return {
  44. enabled: curd.isSuccess && !!api,
  45. queryKey: [ 'models', get(modelSearchAtom) ],
  46. queryFn: async ({ queryKey: [ , params ] }) => {
  47. if (api.startsWith('http')) {
  48. return await modelServ.model(api).proxy<XForm.IModel>({
  49. path: '/list',
  50. body: params,
  51. })
  52. }
  53. return await modelServ.model(api).list(params as SearchParams)
  54. },
  55. select: res => {
  56. const data = res.data
  57. data.rows = data.rows?.map(row => {
  58. return {
  59. ...row,
  60. //status: convertToBool(row.status)
  61. }
  62. })
  63. return data
  64. }
  65. }
  66. })
  67. //saveOrUpdateAtom
  68. export const saveOrUpdateModelAtom = atomWithMutation<IApiResult, XForm.IModel>((get) => {
  69. return {
  70. mutationKey: [ 'updateModel' ],
  71. mutationFn: async (data) => {
  72. const api = get(apiAtom)
  73. if (!api) {
  74. return Promise.reject('api 不能为空')
  75. }
  76. if (data.id === 0) {
  77. if (api.startsWith('http')) {
  78. return await modelServ.model(api).proxy<XForm.IModel>({
  79. body: data,
  80. path: '/add',
  81. })
  82. }
  83. return await modelServ.model(api).add(data)
  84. }
  85. if (api.startsWith('http')) {
  86. return await modelServ.model(api).proxy<XForm.IModel>({
  87. body: data,
  88. path: '/edit',
  89. })
  90. }
  91. return await modelServ.model(api).update(data)
  92. },
  93. onSuccess: (res) => {
  94. const isAdd = !!res.data?.id
  95. message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功'))
  96. //更新列表
  97. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  98. // @ts-ignore fix
  99. get(queryClientAtom).invalidateQueries({ queryKey: [ 'models', get(modelSearchAtom) ] })
  100. return res
  101. }
  102. }
  103. })
  104. export const deleteModelAtom = atomWithMutation((get) => {
  105. return {
  106. mutationKey: [ 'deleteModel' ],
  107. mutationFn: async (ids: number[]) => {
  108. const api = get(apiAtom)
  109. if (api.startsWith('http')) {
  110. return await modelServ.model(api).proxy<XForm.IModel>({
  111. body: ids ?? get(modelIdsAtom) ,
  112. path: '/deletes',
  113. })
  114. }
  115. return await modelServ.model(api).batchDelete(ids ?? get(modelIdsAtom))
  116. },
  117. onSuccess: (res) => {
  118. message.success('message.deleteSuccess')
  119. //更新列表
  120. get(queryClientAtom).invalidateQueries({ queryKey: [ 'models', get(modelSearchAtom) ] })
  121. return res
  122. }
  123. }
  124. })