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
94 lines
2.5 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'
|
|
|
|
const i18nPrefix = 'cms.video'
|
|
|
|
type SearchParams = IPage & {
|
|
key?: string
|
|
}
|
|
|
|
export const videoTypes = [
|
|
{ label: t(`${i18nPrefix}.type_id.0`), value: 0 },
|
|
{ label: t(`${i18nPrefix}.type_id.1`), value: 1 },
|
|
{ label: t(`${i18nPrefix}.type_id.2`), value: 2 },
|
|
]
|
|
|
|
export const videoIdAtom = atom(0)
|
|
|
|
export const videoIdsAtom = atom<number[]>([])
|
|
|
|
export const videoAtom = atom<Cms.IVideo>(undefined as unknown as Cms.IVideo)
|
|
|
|
export const videoSearchAtom = atom<SearchParams>({
|
|
key: ''
|
|
} as SearchParams)
|
|
|
|
export const videoPageAtom = atom<IPage>({
|
|
pageSize: 10,
|
|
page: 1,
|
|
})
|
|
|
|
export const videosAtom = atomWithQuery((get) => {
|
|
return {
|
|
queryKey: [ 'videos', get(videoSearchAtom) ],
|
|
queryFn: async ({ queryKey: [ , params ] }) => {
|
|
return await cmsServ.video.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 saveOrUpdateVideoAtom = atomWithMutation<IApiResult, Cms.IVideo>((get) => {
|
|
|
|
return {
|
|
mutationKey: [ 'updateVideo' ],
|
|
mutationFn: async (data) => {
|
|
//data.status = data.status ? '1' : '0'
|
|
if (data.id === 0) {
|
|
return await cmsServ.video.add(data)
|
|
}
|
|
return await cmsServ.video.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: [ 'videos', get(videoSearchAtom) ] })
|
|
|
|
return res
|
|
}
|
|
}
|
|
})
|
|
|
|
export const deleteVideoAtom = atomWithMutation((get) => {
|
|
return {
|
|
mutationKey: [ 'deleteVideo' ],
|
|
mutationFn: async (ids: number[]) => {
|
|
return await cmsServ.video.batchDelete(ids ?? get(videoIdsAtom))
|
|
},
|
|
onSuccess: (res) => {
|
|
message.success('message.deleteSuccess')
|
|
//更新列表
|
|
get(queryClientAtom).invalidateQueries({ queryKey: [ 'videos', get(videoSearchAtom) ] })
|
|
return res
|
|
}
|
|
}
|
|
})
|