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.

94 lines
2.5 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. const i18nPrefix = 'cms.video'
  9. type SearchParams = IPage & {
  10. key?: string
  11. }
  12. export const videoTypes = [
  13. { label: t(`${i18nPrefix}.type_id.0`), value: 0 },
  14. { label: t(`${i18nPrefix}.type_id.1`), value: 1 },
  15. { label: t(`${i18nPrefix}.type_id.2`), value: 2 },
  16. ]
  17. export const videoIdAtom = atom(0)
  18. export const videoIdsAtom = atom<number[]>([])
  19. export const videoAtom = atom<Cms.IVideo>(undefined as unknown as Cms.IVideo)
  20. export const videoSearchAtom = atom<SearchParams>({
  21. key: ''
  22. } as SearchParams)
  23. export const videoPageAtom = atom<IPage>({
  24. pageSize: 10,
  25. page: 1,
  26. })
  27. export const videosAtom = atomWithQuery((get) => {
  28. return {
  29. queryKey: [ 'videos', get(videoSearchAtom) ],
  30. queryFn: async ({ queryKey: [ , params ] }) => {
  31. return await cmsServ.video.list(params as SearchParams)
  32. },
  33. select: res => {
  34. const data = res.data
  35. data.rows = data.rows?.map(row => {
  36. return {
  37. ...row,
  38. //status: convertToBool(row.status)
  39. }
  40. })
  41. return data
  42. }
  43. }
  44. })
  45. //saveOrUpdateAtom
  46. export const saveOrUpdateVideoAtom = atomWithMutation<IApiResult, Cms.IVideo>((get) => {
  47. return {
  48. mutationKey: [ 'updateVideo' ],
  49. mutationFn: async (data) => {
  50. //data.status = data.status ? '1' : '0'
  51. if (data.id === 0) {
  52. return await cmsServ.video.add(data)
  53. }
  54. return await cmsServ.video.update(data)
  55. },
  56. onSuccess: (res) => {
  57. const isAdd = !!res.data?.id
  58. message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功'))
  59. //更新列表
  60. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  61. // @ts-ignore fix
  62. get(queryClientAtom).invalidateQueries({ queryKey: [ 'videos', get(videoSearchAtom) ] })
  63. return res
  64. }
  65. }
  66. })
  67. export const deleteVideoAtom = atomWithMutation((get) => {
  68. return {
  69. mutationKey: [ 'deleteVideo' ],
  70. mutationFn: async (ids: number[]) => {
  71. return await cmsServ.video.batchDelete(ids ?? get(videoIdsAtom))
  72. },
  73. onSuccess: (res) => {
  74. message.success('message.deleteSuccess')
  75. //更新列表
  76. get(queryClientAtom).invalidateQueries({ queryKey: [ 'videos', get(videoSearchAtom) ] })
  77. return res
  78. }
  79. }
  80. })