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.
141 lines
3.7 KiB
141 lines
3.7 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 { XForm } from '@/types/x-form/model'
|
|
import * as modelServ from '@/service/x-form/model'
|
|
import { atomWithStorage } from 'jotai/utils'
|
|
|
|
type SearchParams = IPage & {
|
|
key?: string
|
|
api: string
|
|
}
|
|
|
|
|
|
export const apiAtom = atomWithStorage('api', '')
|
|
|
|
export const modelIdAtom = atom(0)
|
|
|
|
export const modelIdsAtom = atom<number[]>([])
|
|
|
|
export const modelAtom = atom<XForm.IModel>(undefined as unknown as XForm.IModel)
|
|
|
|
export const modelSearchAtom = atom<SearchParams>({
|
|
key: '',
|
|
pageSize: 10,
|
|
page: 1,
|
|
} as SearchParams)
|
|
|
|
export const modelPageAtom = atom<IPage>({
|
|
pageSize: 10,
|
|
page: 1,
|
|
})
|
|
|
|
export const modelCURDAtom = atomWithQuery<IApiResult<XForm.IModelCURD>, any, any>((get) => {
|
|
const api = get(apiAtom)
|
|
console.log('api', api)
|
|
return {
|
|
enabled: !!api,
|
|
queryKey: [ 'modelCURD' ],
|
|
queryFn: async () => {
|
|
return await modelServ.model(api).proxy()
|
|
},
|
|
select: (res) => {
|
|
return res.data.data
|
|
}
|
|
}
|
|
})
|
|
|
|
export const modelsAtom = atomWithQuery((get) => {
|
|
const api = get(apiAtom)
|
|
const curd = get(modelCURDAtom)
|
|
|
|
return {
|
|
enabled: curd.isSuccess && !!api,
|
|
queryKey: [ 'models', get(modelSearchAtom) ],
|
|
queryFn: async ({ queryKey: [ , params ] }) => {
|
|
|
|
if (api.startsWith('http')) {
|
|
return await modelServ.model(api).proxy<XForm.IModel>({
|
|
path: '/list',
|
|
body: params,
|
|
})
|
|
}
|
|
return await modelServ.model(api).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 saveOrUpdateModelAtom = atomWithMutation<IApiResult, XForm.IModel>((get) => {
|
|
|
|
return {
|
|
mutationKey: [ 'updateModel' ],
|
|
mutationFn: async (data) => {
|
|
const api = get(apiAtom)
|
|
if (!api) {
|
|
return Promise.reject('api 不能为空')
|
|
}
|
|
if (data.id === 0) {
|
|
if (api.startsWith('http')) {
|
|
return await modelServ.model(api).proxy<XForm.IModel>({
|
|
body: data,
|
|
path: '/add',
|
|
})
|
|
}
|
|
return await modelServ.model(api).add(data)
|
|
}
|
|
if (api.startsWith('http')) {
|
|
return await modelServ.model(api).proxy<XForm.IModel>({
|
|
body: data,
|
|
path: '/edit',
|
|
})
|
|
}
|
|
return await modelServ.model(api).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: [ 'models', get(modelSearchAtom) ] })
|
|
|
|
return res
|
|
}
|
|
}
|
|
})
|
|
|
|
export const deleteModelAtom = atomWithMutation((get) => {
|
|
return {
|
|
mutationKey: [ 'deleteModel' ],
|
|
mutationFn: async (ids: number[]) => {
|
|
const api = get(apiAtom)
|
|
if (api.startsWith('http')) {
|
|
return await modelServ.model(api).proxy<XForm.IModel>({
|
|
body: ids ?? get(modelIdsAtom) ,
|
|
path: '/deletes',
|
|
})
|
|
}
|
|
return await modelServ.model(api).batchDelete(ids ?? get(modelIdsAtom))
|
|
},
|
|
onSuccess: (res) => {
|
|
message.success('message.deleteSuccess')
|
|
//更新列表
|
|
get(queryClientAtom).invalidateQueries({ queryKey: [ 'models', get(modelSearchAtom) ] })
|
|
return res
|
|
}
|
|
}
|
|
})
|