|
|
@ -1,23 +1,24 @@ |
|
|
|
import systemServ from '@/service/system.ts' |
|
|
|
import { IApiResult, IPage, IPageResult, MenuItem } from '@/types' |
|
|
|
import { IMenu } from '@/types/menus' |
|
|
|
import { AxiosResponse } from 'axios' |
|
|
|
import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query' |
|
|
|
import { atom, createStore } from 'jotai' |
|
|
|
import { message } from 'antd' |
|
|
|
import { t } from '@/i18n.ts' |
|
|
|
|
|
|
|
|
|
|
|
export const defaultMenu = { |
|
|
|
parent_id: 0, |
|
|
|
type: 'menu', |
|
|
|
name: '', |
|
|
|
title: '', |
|
|
|
icon: '', |
|
|
|
path: '', |
|
|
|
component: '', |
|
|
|
sort: 0, |
|
|
|
id: 0, |
|
|
|
button: [], |
|
|
|
} as MenuItem |
|
|
|
parent_id: 0, |
|
|
|
type: 'menu', |
|
|
|
name: '', |
|
|
|
title: '', |
|
|
|
icon: '', |
|
|
|
path: '', |
|
|
|
component: '', |
|
|
|
sort: 0, |
|
|
|
id: 0, |
|
|
|
button: [], |
|
|
|
} as unknown as MenuItem |
|
|
|
|
|
|
|
export const menuPageAtom = atom<IPage>({}) |
|
|
|
|
|
|
@ -25,68 +26,66 @@ const store = createStore() |
|
|
|
|
|
|
|
export const menuDataAtom = atomWithQuery<any, IPageResult<IMenu>>((get) => { |
|
|
|
|
|
|
|
return { |
|
|
|
queryKey: [ 'menus', get(menuPageAtom) ], |
|
|
|
queryFn: async ({ queryKey: [ , page ] }) => { |
|
|
|
return await systemServ.menus.list(page) |
|
|
|
}, |
|
|
|
select: (data) => { |
|
|
|
return data.rows ?? [] |
|
|
|
return { |
|
|
|
queryKey: [ 'menus', get(menuPageAtom) ], |
|
|
|
queryFn: async ({ queryKey: [ , page ] }) => { |
|
|
|
return await systemServ.menus.list(page) |
|
|
|
}, |
|
|
|
select: (data) => { |
|
|
|
return data.rows ?? [] |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
export const selectedMenuIdAtom = atom<number>(0) |
|
|
|
export const selectedMenuAtom = atom<MenuItem>({} as MenuItem) |
|
|
|
export const byIdMenuAtom = atomWithQuery((get) => ({ |
|
|
|
queryKey: [ 'selectedMenu', get(selectedMenuIdAtom) ], |
|
|
|
queryFn: async ({ queryKey: [ , id ] }) => { |
|
|
|
return await systemServ.menus.info(id as number) |
|
|
|
}, |
|
|
|
select: data => data.data, |
|
|
|
queryKey: [ 'selectedMenu', get(selectedMenuIdAtom) ], |
|
|
|
queryFn: async ({ queryKey: [ , id ] }) => { |
|
|
|
return await systemServ.menus.info(id as number) |
|
|
|
}, |
|
|
|
select: data => data.data, |
|
|
|
})) |
|
|
|
|
|
|
|
|
|
|
|
export const saveOrUpdateMenuAtom = atomWithMutation<IApiResult<IMenu>>((get) => { |
|
|
|
|
|
|
|
return { |
|
|
|
mutationKey: [ 'updateMenu', get(selectedMenuIdAtom) ], |
|
|
|
mutationFn: async (data: IMenu) => { |
|
|
|
if (data.id === 0) { |
|
|
|
return await systemServ.menus.add(data) |
|
|
|
} |
|
|
|
return await systemServ.menus.update(data) |
|
|
|
}, |
|
|
|
onSuccess: (res) => { |
|
|
|
const menu = get(selectedMenuAtom) |
|
|
|
console.log({ |
|
|
|
...menu, |
|
|
|
id: res.data?.id |
|
|
|
}) |
|
|
|
store.set(selectedMenuAtom, { |
|
|
|
...menu, |
|
|
|
id: res.data?.id |
|
|
|
}) |
|
|
|
//更新列表
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore fix
|
|
|
|
get(queryClientAtom).refetchQueries([ 'menus', get(menuPageAtom) ]).then() |
|
|
|
return { |
|
|
|
mutationKey: [ 'updateMenu', get(selectedMenuIdAtom) ], |
|
|
|
mutationFn: async (data: IMenu) => { |
|
|
|
if (data.id === 0) { |
|
|
|
return await systemServ.menus.add(data) |
|
|
|
} |
|
|
|
return await systemServ.menus.update(data) |
|
|
|
}, |
|
|
|
onSuccess: (res) => { |
|
|
|
message.success(t('message.saveSuccess', '保存成功')) |
|
|
|
const menu = get(selectedMenuAtom) |
|
|
|
store.set(selectedMenuAtom, { |
|
|
|
...menu, |
|
|
|
id: res.data?.id |
|
|
|
}) |
|
|
|
//更新列表
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore fix
|
|
|
|
get(queryClientAtom).refetchQueries([ 'menus', get(menuPageAtom) ]).then() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
export const batchIdsAtom = atom<number[]>([]) |
|
|
|
|
|
|
|
export const deleteMenuAtom = atomWithMutation((get) => { |
|
|
|
return { |
|
|
|
mutationKey: [ 'deleteMenu', get(batchIdsAtom) ], |
|
|
|
mutationFn: async (ids?: number[]) => { |
|
|
|
return await systemServ.menus.batchDelete(ids ?? get(batchIdsAtom)) |
|
|
|
}, |
|
|
|
onSuccess: () => { |
|
|
|
store.set(batchIdsAtom, []) |
|
|
|
get(queryClientAtom).refetchQueries([ 'menus', get(menuPageAtom) ]).then() |
|
|
|
return { |
|
|
|
mutationKey: [ 'deleteMenu', get(batchIdsAtom) ], |
|
|
|
mutationFn: async (ids?: number[]) => { |
|
|
|
return await systemServ.menus.batchDelete(ids ?? get(batchIdsAtom)) |
|
|
|
}, |
|
|
|
onSuccess: () => { |
|
|
|
message.success(t('message.deleteSuccess', '删除成功')) |
|
|
|
store.set(batchIdsAtom, []) |
|
|
|
get(queryClientAtom).refetchQueries([ 'menus', get(menuPageAtom) ]).then() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}) |