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.

108 lines
2.9 KiB

  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 { Cms } from '@/types'
  7. import cmsServ from '@/service/cms.ts'
  8. type SearchParams = IPage & {
  9. key?: string
  10. }
  11. export const categoryIdAtom = atom(0)
  12. export const categoryIdsAtom = atom<number[]>([])
  13. export const categoryAtom = atom<Cms.ICategory>(undefined as unknown as Cms.ICategory)
  14. export const categorySearchAtom = atom<SearchParams>({
  15. key: ''
  16. } as SearchParams)
  17. export const categoryPageAtom = atom<IPage>({
  18. pageSize: 10,
  19. page: 1,
  20. })
  21. export const categoriesAtom = atomWithQuery((get) => {
  22. return {
  23. queryKey: [ 'categories', get(categorySearchAtom) ],
  24. queryFn: async ({ queryKey: [ , params ] }) => {
  25. return await cmsServ.category.list(params as SearchParams)
  26. },
  27. select: res => {
  28. const data = res.data
  29. data.rows = data.rows?.map(row => {
  30. return {
  31. ...row,
  32. //status: convertToBool(row.status)
  33. }
  34. })
  35. return data
  36. }
  37. }
  38. })
  39. //saveOrUpdateAtom
  40. export const saveOrUpdateCategoryAtom = atomWithMutation<IApiResult, Cms.ICategory>((get) => {
  41. return {
  42. mutationKey: [ 'updateCategory' ],
  43. mutationFn: async (data) => {
  44. //data.status = data.status ? '1' : '0'
  45. if (data.id === 0) {
  46. return await cmsServ.category.add(data)
  47. }
  48. return await cmsServ.category.update(data)
  49. },
  50. onSuccess: (res) => {
  51. const isAdd = !!res.data?.id
  52. message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功'))
  53. //更新列表
  54. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  55. // @ts-ignore fix
  56. get(queryClientAtom).invalidateQueries({ queryKey: [ 'categories', get(categorySearchAtom) ] })
  57. return res
  58. }
  59. }
  60. })
  61. export const deleteCategoryAtom = atomWithMutation((get) => {
  62. return {
  63. mutationKey: [ 'deleteCategory' ],
  64. mutationFn: async (ids: number[]) => {
  65. return await cmsServ.category.batchDelete(ids ?? get(categoryIdsAtom))
  66. },
  67. onSuccess: (res) => {
  68. message.success('message.deleteSuccess')
  69. //更新列表
  70. get(queryClientAtom).invalidateQueries({ queryKey: [ 'categories', get(categorySearchAtom) ] })
  71. return res
  72. }
  73. }
  74. })
  75. //getById
  76. export const categoryByIdAtom = atomWithQuery((get) => {
  77. return {
  78. enabled: !!get(categoryIdAtom),
  79. queryKey: [ 'category', get(categoryIdAtom) ],
  80. queryFn: async ({ queryKey: [ , id ] }) => {
  81. const res = await cmsServ.category.info(id as number)
  82. //res.data.status = convertToBool(res.data.status)
  83. return res
  84. },
  85. select: (res) => {
  86. const data = res.data
  87. if (data.extend) {
  88. data.extend = JSON.parse(data.extend)
  89. }
  90. return data
  91. }
  92. }
  93. })