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.
86 lines
2.3 KiB
86 lines
2.3 KiB
import { convertToBool } from '@/utils'
|
|
import { atom } from 'jotai/index'
|
|
import { IRole } from '@/types/roles'
|
|
import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query'
|
|
import { IApiResult, IPage } from '@/types'
|
|
import systemServ from '@/service/system.ts'
|
|
import { message } from 'antd'
|
|
import { t } from '@/i18n.ts'
|
|
|
|
type SearchParams = IPage & {
|
|
key?: string
|
|
}
|
|
|
|
export const idAtom = atom(0)
|
|
|
|
export const roleIdsAtom = atom<number[]>([])
|
|
|
|
export const roleAtom = atom<IRole>(undefined as unknown as IRole)
|
|
|
|
export const searchAtom = atom<SearchParams>({} as SearchParams)
|
|
|
|
export const pageAtom = atom<IPage>({
|
|
pageSize: 10,
|
|
page: 1,
|
|
})
|
|
|
|
export const rolesAtom = atomWithQuery((get) => {
|
|
return {
|
|
queryKey: [ 'roles', get(searchAtom) ],
|
|
queryFn: async ({ queryKey: [ , params ] }) => {
|
|
return await systemServ.role.list(params as SearchParams)
|
|
},
|
|
select: res => {
|
|
const data = res.data
|
|
data.rows = data.rows?.map(row => {
|
|
return {
|
|
...row,
|
|
status: convertToBool(row.status)
|
|
}
|
|
})
|
|
return data
|
|
}
|
|
}
|
|
})
|
|
|
|
//saveOrUpdateRoleAtom
|
|
|
|
export const saveOrUpdateRoleAtom = atomWithMutation<IApiResult, IRole>((get) => {
|
|
|
|
return {
|
|
mutationKey: [ 'updateMenu' ],
|
|
mutationFn: async (data) => {
|
|
data.status = data.status ? '1' : '0'
|
|
if (data.id === 0) {
|
|
return await systemServ.role.add(data)
|
|
}
|
|
return await systemServ.role.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: [ 'roles', get(searchAtom) ] })
|
|
|
|
return res
|
|
}
|
|
}
|
|
})
|
|
|
|
export const deleteRoleAtom = atomWithMutation((get) => {
|
|
return {
|
|
mutationKey: [ 'deleteMenu' ],
|
|
mutationFn: async (ids: number[]) => {
|
|
return await systemServ.role.batchDelete(ids ?? get(roleIdsAtom))
|
|
},
|
|
onSuccess: (res) => {
|
|
message.success('message.deleteSuccess')
|
|
//更新列表
|
|
get(queryClientAtom).invalidateQueries({ queryKey: [ 'roles', get(searchAtom) ] })
|
|
return res
|
|
}
|
|
}
|
|
})
|