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

import { atom } from 'jotai/index'
import { IApiResult, IPage } from '@/global'
import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query'
import { IMsgTemplate } from '@/types/system/message.ts'
import mdwMessage from '@/service/message/message.ts'
import { message } from 'antd'
import { t } from 'i18next'
type SearchParams = IPage & {
key?: string
[key: string]: any
}
export const templateAIdsAtom = atom<number>(0)
export const templateAtom = atom<IMsgTemplate>(undefined as unknown as IMsgTemplate)
export const templateSearchAtom = atom<SearchParams>({
key: '',
pageSize: 10,
page: 1,
} as SearchParams)
export const templatePageAtom = atom<IPage>({
pageSize: 10,
page: 1,
})
export const templateListAtom = atomWithQuery((get) => {
return {
queryKey: [ 'templateList', get(templateSearchAtom) ],
queryFn: async ({ queryKey: [ , params ] }) => {
const list = await mdwMessage.list(params as SearchParams)
return list.data
}
}
})
export const templateFieldAtom = atomWithQuery(() => {
return {
queryKey: [ 'templateField' ],
queryFn: async ({ queryKey: [ , params ] }) => {
const list = await mdwMessage.fieldList(params)
return list.data
}
}
})
export const deleteTemplateAtom = atomWithMutation((get) => {
return {
mutationKey: [ 'deleteTemplate' ],
mutationFn: async (ids: number) => {
return await mdwMessage.delete(ids ?? get(templateAIdsAtom) as number)
},
onSuccess: (res) => {
message.success('message.deleteSuccess')
//更新列表
get(queryClientAtom).invalidateQueries({ queryKey: [ 'templateList', get(templateSearchAtom) ] })
return res
}
}
})
export const saveOrUpdateTemplateAtom = atomWithMutation<IApiResult, IMsgTemplate>((get) => {
return {
mutationKey: [ 'updateTemplate' ],
mutationFn: async (data) => {
//data.status = data.status ? '1' : '0'
if (data.id === 0) {
return await mdwMessage.add(data)
}
return await mdwMessage.update(data)
},
onSuccess: (res) => {
const isAdd = !!res.data?.id
message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功'))
get(queryClientAtom).invalidateQueries({ queryKey: [ 'templateList', get(templateSearchAtom) ] })
return res
}
}
})