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.

85 lines
2.3 KiB

9 months ago
  1. import { atom } from 'jotai/index'
  2. import { IApiResult, IPage } from '@/global'
  3. import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query'
  4. import { IMsgTemplate } from '@/types/system/message.ts'
  5. import mdwMessage from '@/service/message/message.ts'
  6. import { message } from 'antd'
  7. import { t } from 'i18next'
  8. type SearchParams = IPage & {
  9. key?: string
  10. [key: string]: any
  11. }
  12. export const templateAIdsAtom = atom<number>(0)
  13. export const templateAtom = atom<IMsgTemplate>(undefined as unknown as IMsgTemplate)
  14. export const templateSearchAtom = atom<SearchParams>({
  15. key: '',
  16. pageSize: 10,
  17. page: 1,
  18. } as SearchParams)
  19. export const templatePageAtom = atom<IPage>({
  20. pageSize: 10,
  21. page: 1,
  22. })
  23. export const templateListAtom = atomWithQuery((get) => {
  24. return {
  25. queryKey: [ 'templateList', get(templateSearchAtom) ],
  26. queryFn: async ({ queryKey: [ , params ] }) => {
  27. const list = await mdwMessage.list(params as SearchParams)
  28. return list.data
  29. }
  30. }
  31. })
  32. export const templateFieldAtom = atomWithQuery(() => {
  33. return {
  34. queryKey: [ 'templateField' ],
  35. queryFn: async ({ queryKey: [ , params ] }) => {
  36. const list = await mdwMessage.fieldList(params)
  37. return list.data
  38. }
  39. }
  40. })
  41. export const deleteTemplateAtom = atomWithMutation((get) => {
  42. return {
  43. mutationKey: [ 'deleteTemplate' ],
  44. mutationFn: async (ids: number) => {
  45. return await mdwMessage.delete(ids ?? get(templateAIdsAtom) as number)
  46. },
  47. onSuccess: (res) => {
  48. message.success('message.deleteSuccess')
  49. //更新列表
  50. get(queryClientAtom).invalidateQueries({ queryKey: [ 'templateList', get(templateSearchAtom) ] })
  51. return res
  52. }
  53. }
  54. })
  55. export const saveOrUpdateTemplateAtom = atomWithMutation<IApiResult, IMsgTemplate>((get) => {
  56. return {
  57. mutationKey: [ 'updateTemplate' ],
  58. mutationFn: async (data) => {
  59. //data.status = data.status ? '1' : '0'
  60. if (data.id === 0) {
  61. return await mdwMessage.add(data)
  62. }
  63. return await mdwMessage.update(data)
  64. },
  65. onSuccess: (res) => {
  66. const isAdd = !!res.data?.id
  67. message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功'))
  68. get(queryClientAtom).invalidateQueries({ queryKey: [ 'templateList', get(templateSearchAtom) ] })
  69. return res
  70. }
  71. }
  72. })