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.

90 lines
2.3 KiB

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 { DB } from '@/types/db/vod'
  7. import dBServ from '@/service/db/vod'
  8. type SearchParams = IPage & {
  9. key?: string
  10. [key: string]: any
  11. }
  12. export const vodIdAtom = atom(0)
  13. export const vodIdsAtom = atom<number[]>([])
  14. export const vodAtom = atom<DB.IVod>(undefined as unknown as DB.IVod )
  15. export const vodSearchAtom = atom<SearchParams>({
  16. key: '',
  17. pageSize: 10,
  18. page: 1,
  19. } as SearchParams)
  20. export const vodPageAtom = atom<IPage>({
  21. pageSize: 10,
  22. page: 1,
  23. })
  24. export const vodsAtom = atomWithQuery((get) => {
  25. return {
  26. queryKey: [ 'vods', get(vodSearchAtom) ],
  27. queryFn: async ({ queryKey: [ , params ] }) => {
  28. return await dBServ.list(params as SearchParams)
  29. },
  30. select: res => {
  31. const data = res.data
  32. data.rows = data.rows?.map(row => {
  33. return {
  34. ...row,
  35. //status: convertToBool(row.status)
  36. }
  37. })
  38. return data
  39. }
  40. }
  41. })
  42. //saveOrUpdateAtom
  43. export const saveOrUpdateVodAtom = atomWithMutation<IApiResult, DB.IVod>((get) => {
  44. return {
  45. mutationKey: [ 'updateVod' ],
  46. mutationFn: async (data) => {
  47. //data.status = data.status ? '1' : '0'
  48. if (data.id === 0) {
  49. return await dBServ.add(data)
  50. }
  51. return await dBServ.update(data)
  52. },
  53. onSuccess: (res) => {
  54. const isAdd = !!res.data?.id
  55. message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功'))
  56. //更新列表
  57. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  58. // @ts-ignore fix
  59. get(queryClientAtom).invalidateQueries({ queryKey: [ 'vods', get(vodSearchAtom) ] })
  60. return res
  61. }
  62. }
  63. })
  64. export const deleteVodAtom = atomWithMutation((get) => {
  65. return {
  66. mutationKey: [ 'deleteVod' ],
  67. mutationFn: async (ids: number[]) => {
  68. return await dBServ.batchDelete(ids ?? get(vodIdsAtom))
  69. },
  70. onSuccess: (res) => {
  71. message.success('message.deleteSuccess')
  72. //更新列表
  73. get(queryClientAtom).invalidateQueries({ queryKey: [ 'vods', get(vodSearchAtom) ] })
  74. return res
  75. }
  76. }
  77. })