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.

87 lines
2.4 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 videoMagnetIdAtom = atom(0)
  12. export const videoMagnetIdsAtom = atom<number[]>([])
  13. export const videoMagnetAtom = atom<Cms.IVideoMagnet>(undefined as unknown as Cms.IVideoMagnet)
  14. export const videoMagnetSearchAtom = atom<SearchParams>({
  15. key: ''
  16. } as SearchParams)
  17. export const videoMagnetPageAtom = atom<IPage>({
  18. pageSize: 10,
  19. page: 1,
  20. })
  21. export const videoMagnetsAtom = atomWithQuery((get) => {
  22. return {
  23. queryKey: [ 'videoMagnets', get(videoMagnetSearchAtom) ],
  24. queryFn: async ({ queryKey: [ , params ] }) => {
  25. return await cmsServ.videoMagnet.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 saveOrUpdateVideoMagnetAtom = atomWithMutation<IApiResult, Cms.IVideoMagnet>((get) => {
  41. return {
  42. mutationKey: [ 'updateVideoMagnet' ],
  43. mutationFn: async (data) => {
  44. //data.status = data.status ? '1' : '0'
  45. if (data.id === 0) {
  46. return await cmsServ.videoMagnet.add(data)
  47. }
  48. return await cmsServ.videoMagnet.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: [ 'videoMagnets', get(videoMagnetSearchAtom) ] })
  57. return res
  58. }
  59. }
  60. })
  61. export const deleteVideoMagnetAtom = atomWithMutation((get) => {
  62. return {
  63. mutationKey: [ 'deleteVideoMagnet' ],
  64. mutationFn: async (ids: number[]) => {
  65. return await cmsServ.videoMagnet.batchDelete(ids ?? get(videoMagnetIdsAtom))
  66. },
  67. onSuccess: (res) => {
  68. message.success('message.deleteSuccess')
  69. //更新列表
  70. get(queryClientAtom).invalidateQueries({ queryKey: [ 'videoMagnets', get(videoMagnetSearchAtom) ] })
  71. return res
  72. }
  73. }
  74. })