|
@@ -2,10 +2,10 @@ import {useContextSection} from '@hooks';
|
|
|
import {context} from './context';
|
|
|
import {FormEvent, useEffect} from 'react';
|
|
|
import {useMutation, useQueryClient} from '@tanstack/react-query';
|
|
|
-import {editRoleMenu, getRoleList} from '@apis';
|
|
|
+import {editRoleMenu, getRoleList, getTreeMenu} from '@apis';
|
|
|
import {message} from 'antd';
|
|
|
-import {RoleListData} from '@models';
|
|
|
-import {trimEnd} from 'lodash-es';
|
|
|
+import {RoleListData, TreeMenuListData} from '@models';
|
|
|
+import {useMap} from 'ahooks';
|
|
|
|
|
|
function useEditMenu(onClose: () => void, onFetch: () => void) {
|
|
|
const {isLoading, mutate} = useMutation(
|
|
@@ -24,18 +24,61 @@ function useEditMenu(onClose: () => void, onFetch: () => void) {
|
|
|
return [isLoading, mutate] as const;
|
|
|
}
|
|
|
|
|
|
+function useTreeMap() {
|
|
|
+ const client = useQueryClient();
|
|
|
+ const treeData = client.getQueryData<TreeMenuListData[]>([getTreeMenu.name]);
|
|
|
+ const [map, {set}] = useMap<string, string>();
|
|
|
+
|
|
|
+ useEffect(
|
|
|
+ function() {
|
|
|
+ if (!treeData || treeData.length === 0) return;
|
|
|
+
|
|
|
+ function deepTree(tree: TreeMenuListData[]) {
|
|
|
+ tree.forEach(function({pId, key, children}) {
|
|
|
+ // 一级菜单不参与寻找父级id
|
|
|
+ pId !== '0' && set(key, pId);
|
|
|
+
|
|
|
+ children.length && deepTree(children);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ deepTree(treeData);
|
|
|
+ },
|
|
|
+ [set, treeData],
|
|
|
+ );
|
|
|
+
|
|
|
+ return map;
|
|
|
+}
|
|
|
+
|
|
|
export function useSubmit(
|
|
|
{id, onClose, onFetch}:
|
|
|
{id: string, onClose: () => void, onFetch: () => void},
|
|
|
) {
|
|
|
const list = useContextSection(context, ([list]) => list);
|
|
|
const [isLoading, mutate] = useEditMenu(onClose, onFetch);
|
|
|
+ const treeMap = useTreeMap();
|
|
|
+
|
|
|
+ function fixMenuTreeList(list: string) {
|
|
|
+ const trees = list.split(','),
|
|
|
+ // 方式有重复问题
|
|
|
+ treeSet = new Set(trees);
|
|
|
+
|
|
|
+ trees.forEach(function(val) {
|
|
|
+ const mapPId = treeMap.get(val);
|
|
|
+
|
|
|
+ mapPId && treeSet.add(mapPId);
|
|
|
+ });
|
|
|
+
|
|
|
+ return [...treeSet].join(',') + ',';
|
|
|
+ }
|
|
|
|
|
|
function onSubmit(e: FormEvent<HTMLFormElement>) {
|
|
|
e.preventDefault();
|
|
|
|
|
|
- const listStr = list.join(',') + ',';
|
|
|
- mutate({id, menu: listStr});
|
|
|
+ const listStr = list.join(',');
|
|
|
+ const treeMenu = fixMenuTreeList(listStr);
|
|
|
+
|
|
|
+ mutate({id, menu: treeMenu, menuBefore: listStr});
|
|
|
}
|
|
|
|
|
|
return [isLoading, onSubmit] as const;
|
|
@@ -52,7 +95,8 @@ export function useWatchId(id: string, visible: boolean) {
|
|
|
const res = data?.find(val => String(val.id) === id);
|
|
|
|
|
|
if (res) {
|
|
|
- const menuList = trimEnd(res?.menu ?? '', ',').split(',');
|
|
|
+ const menuList = res?.menuBefore ? res.menuBefore.split(',') : [];
|
|
|
+
|
|
|
dispatch(menuList);
|
|
|
}
|
|
|
}, [id, client, dispatch, visible]);
|