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.
108 lines
2.9 KiB
108 lines
2.9 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 categoryIdAtom = atom(0)
|
|
|
|
export const categoryIdsAtom = atom<number[]>([])
|
|
|
|
export const categoryAtom = atom<Cms.ICategory>(undefined as unknown as Cms.ICategory)
|
|
|
|
export const categorySearchAtom = atom<SearchParams>({
|
|
key: ''
|
|
} as SearchParams)
|
|
|
|
export const categoryPageAtom = atom<IPage>({
|
|
pageSize: 10,
|
|
page: 1,
|
|
})
|
|
|
|
export const categoriesAtom = atomWithQuery((get) => {
|
|
return {
|
|
queryKey: [ 'categories', get(categorySearchAtom) ],
|
|
queryFn: async ({ queryKey: [ , params ] }) => {
|
|
return await cmsServ.category.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 saveOrUpdateCategoryAtom = atomWithMutation<IApiResult, Cms.ICategory>((get) => {
|
|
|
|
return {
|
|
mutationKey: [ 'updateCategory' ],
|
|
mutationFn: async (data) => {
|
|
//data.status = data.status ? '1' : '0'
|
|
if (data.id === 0) {
|
|
return await cmsServ.category.add(data)
|
|
}
|
|
return await cmsServ.category.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: [ 'categories', get(categorySearchAtom) ] })
|
|
|
|
return res
|
|
}
|
|
}
|
|
})
|
|
|
|
export const deleteCategoryAtom = atomWithMutation((get) => {
|
|
return {
|
|
mutationKey: [ 'deleteCategory' ],
|
|
mutationFn: async (ids: number[]) => {
|
|
return await cmsServ.category.batchDelete(ids ?? get(categoryIdsAtom))
|
|
},
|
|
onSuccess: (res) => {
|
|
message.success('message.deleteSuccess')
|
|
//更新列表
|
|
get(queryClientAtom).invalidateQueries({ queryKey: [ 'categories', get(categorySearchAtom) ] })
|
|
return res
|
|
}
|
|
}
|
|
})
|
|
|
|
|
|
//getById
|
|
export const categoryByIdAtom = atomWithQuery((get) => {
|
|
return {
|
|
enabled: !!get(categoryIdAtom),
|
|
queryKey: [ 'category', get(categoryIdAtom) ],
|
|
queryFn: async ({ queryKey: [ , id ] }) => {
|
|
const res = await cmsServ.category.info(id as number)
|
|
//res.data.status = convertToBool(res.data.status)
|
|
return res
|
|
},
|
|
select: (res) => {
|
|
const data = res.data
|
|
if (data.extend) {
|
|
data.extend = JSON.parse(data.extend)
|
|
}
|
|
return data
|
|
}
|
|
}
|
|
})
|