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 { DB } from '@/types/db/vod' import dBServ from '@/service/db/vod' type SearchParams = IPage & { key?: string [key: string]: any } export const vodIdAtom = atom(0) export const vodIdsAtom = atom([]) export const vodAtom = atom(undefined as unknown as DB.IVod ) export const vodSearchAtom = atom({ key: '', pageSize: 10, page: 1, } as SearchParams) export const vodPageAtom = atom({ pageSize: 10, page: 1, }) export const vodsAtom = atomWithQuery((get) => { return { queryKey: [ 'vods', get(vodSearchAtom) ], queryFn: async ({ queryKey: [ , params ] }) => { return await dBServ.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 saveOrUpdateVodAtom = atomWithMutation((get) => { return { mutationKey: [ 'updateVod' ], mutationFn: async (data) => { //data.status = data.status ? '1' : '0' if (data.id === 0) { return await dBServ.add(data) } return await dBServ.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: [ 'vods', get(vodSearchAtom) ] }) return res } } }) export const deleteVodAtom = atomWithMutation((get) => { return { mutationKey: [ 'deleteVod' ], mutationFn: async (ids: number[]) => { return await dBServ.batchDelete(ids ?? get(vodIdsAtom)) }, onSuccess: (res) => { message.success('message.deleteSuccess') //更新列表 get(queryClientAtom).invalidateQueries({ queryKey: [ 'vods', get(vodSearchAtom) ] }) return res } } })