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
87 lines
2.4 KiB
import { atom } from 'jotai'
|
|
import { IApiResult, IPage } from '@/global'
|
|
import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query'
|
|
import { message } from 'antd'
|
|
import { t } from 'i18next'
|
|
import { Cms } from '@/types'
|
|
import cmsServ from '@/service/cms.ts'
|
|
|
|
|
|
type SearchParams = IPage & {
|
|
key?: string
|
|
}
|
|
|
|
export const videoMagnetIdAtom = atom(0)
|
|
|
|
export const videoMagnetIdsAtom = atom<number[]>([])
|
|
|
|
export const videoMagnetAtom = atom<Cms.IVideoMagnet>(undefined as unknown as Cms.IVideoMagnet)
|
|
|
|
export const videoMagnetSearchAtom = atom<SearchParams>({
|
|
key: ''
|
|
} as SearchParams)
|
|
|
|
export const videoMagnetPageAtom = atom<IPage>({
|
|
pageSize: 10,
|
|
page: 1,
|
|
})
|
|
|
|
export const videoMagnetsAtom = atomWithQuery((get) => {
|
|
return {
|
|
queryKey: [ 'videoMagnets', get(videoMagnetSearchAtom) ],
|
|
queryFn: async ({ queryKey: [ , params ] }) => {
|
|
return await cmsServ.videoMagnet.list(params as SearchParams)
|
|
},
|
|
select: res => {
|
|
const data = res.data
|
|
data.rows = data.rows?.map(row => {
|
|
return {
|
|
...row,
|
|
//status: convertToBool(row.status)
|
|
}
|
|
})
|
|
return data
|
|
}
|
|
}
|
|
})
|
|
|
|
//saveOrUpdateAtom
|
|
export const saveOrUpdateVideoMagnetAtom = atomWithMutation<IApiResult, Cms.IVideoMagnet>((get) => {
|
|
|
|
return {
|
|
mutationKey: [ 'updateVideoMagnet' ],
|
|
mutationFn: async (data) => {
|
|
//data.status = data.status ? '1' : '0'
|
|
if (data.id === 0) {
|
|
return await cmsServ.videoMagnet.add(data)
|
|
}
|
|
return await cmsServ.videoMagnet.update(data)
|
|
},
|
|
onSuccess: (res) => {
|
|
const isAdd = !!res.data?.id
|
|
message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功'))
|
|
|
|
//更新列表
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
// @ts-ignore fix
|
|
get(queryClientAtom).invalidateQueries({ queryKey: [ 'videoMagnets', get(videoMagnetSearchAtom) ] })
|
|
|
|
return res
|
|
}
|
|
}
|
|
})
|
|
|
|
export const deleteVideoMagnetAtom = atomWithMutation((get) => {
|
|
return {
|
|
mutationKey: [ 'deleteVideoMagnet' ],
|
|
mutationFn: async (ids: number[]) => {
|
|
return await cmsServ.videoMagnet.batchDelete(ids ?? get(videoMagnetIdsAtom))
|
|
},
|
|
onSuccess: (res) => {
|
|
message.success('message.deleteSuccess')
|
|
//更新列表
|
|
get(queryClientAtom).invalidateQueries({ queryKey: [ 'videoMagnets', get(videoMagnetSearchAtom) ] })
|
|
return res
|
|
}
|
|
}
|
|
})
|