Skip to content

Commit

Permalink
fix(projects): fix menu data when role is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jun 6, 2024
1 parent 0036172 commit 7cc5f9c
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/store/modules/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
const constantRoutes = shallowRef<ElegantConstRoute[]>([]);

function addConstantRoutes(routes: ElegantConstRoute[]) {
const constantRoutesMap = new Map(constantRoutes.value.map(route => [route.name, route]));
const constantRoutesMap = new Map<string, ElegantConstRoute>([]);

routes.forEach(route => {
constantRoutesMap.set(route.name, route);
Expand All @@ -69,7 +69,7 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
const authRoutes = shallowRef<ElegantConstRoute[]>([]);

function addAuthRoutes(routes: ElegantConstRoute[]) {
const authRoutesMap = new Map(authRoutes.value.map(route => [route.name, route]));
const authRoutesMap = new Map<string, ElegantConstRoute>([]);

routes.forEach(route => {
authRoutesMap.set(route.name, route);
Expand Down Expand Up @@ -97,13 +97,19 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
/** Cache routes */
const cacheRoutes = ref<RouteKey[]>([]);

/** All cache routes */
const allCacheRoutes = shallowRef<RouteKey[]>([]);

/**
* Get cache routes
*
* @param routes Vue routes
*/
function getCacheRoutes(routes: RouteRecordRaw[]) {
cacheRoutes.value = getCacheRouteNames(routes);
const alls = getCacheRouteNames(routes);

cacheRoutes.value = alls;
allCacheRoutes.value = [...alls];
}

/**
Expand All @@ -130,12 +136,23 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
cacheRoutes.value.splice(index, 1);
}

/**
* Is cached route
*
* @param routeKey
*/
function isCachedRoute(routeKey: RouteKey) {
return allCacheRoutes.value.includes(routeKey);
}

/**
* Re cache routes by route key
*
* @param routeKey
*/
async function reCacheRoutesByKey(routeKey: RouteKey) {
if (!isCachedRoute(routeKey)) return;

removeCacheRoutes(routeKey);

await appStore.reloadPage();
Expand Down Expand Up @@ -179,15 +196,18 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
async function initConstantRoute() {
if (isInitConstantRoute.value) return;

if (authRouteMode.value === 'static') {
const staticRoute = createStaticRoutes();
const staticRoute = createStaticRoutes();

if (authRouteMode.value === 'static') {
addConstantRoutes(staticRoute.constantRoutes);
} else {
const { data, error } = await fetchGetConstantRoutes();

if (!error) {
addConstantRoutes(data);
} else {
// if fetch constant routes failed, use static constant routes
addConstantRoutes(staticRoute.constantRoutes);
}
}

Expand Down Expand Up @@ -240,6 +260,9 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
handleUpdateRootRouteRedirect(home);

setIsInitAuthRoute(true);
} else {
// if fetch user routes failed, reset store
authStore.resetStore();
}
}

Expand Down Expand Up @@ -331,18 +354,6 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
return getSelectedMenuKeyPathByKey(selectedKey, menus.value);
}

/**
* Get selected menu meta by key
*
* @param selectedKey Selected menu key
*/
function getSelectedMenuMetaByKey(selectedKey: string) {
// The routes in router.options.routes are static, you need to use router.getRoutes() to get all the routes.
const allRoutes = router.getRoutes();

return allRoutes.find(route => route.name === selectedKey)?.meta || null;
}

/**
* Get route meta by key
*
Expand Down Expand Up @@ -388,7 +399,6 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
setIsInitAuthRoute,
getIsAuthRouteExist,
getSelectedMenuKeyPath,
getSelectedMenuMetaByKey,
getRouteQueryOfMetaByKey
};
});

0 comments on commit 7cc5f9c

Please sign in to comment.