From 92706caf90d6acbeacf751773dee944dc4295cb3 Mon Sep 17 00:00:00 2001 From: Dede Fuji Abdul Date: Thu, 26 Oct 2023 16:52:25 +0700 Subject: [PATCH] add route page to recent when menu load --- src/components/CommandPalettes.vue | 6 +----- .../Pages/Gangguan/Daftar/Daftar_1.vue | 2 +- src/stores/command.ts | 15 ++++++++++++- src/stores/menu.ts | 21 +++++++++++++++++-- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/components/CommandPalettes.vue b/src/components/CommandPalettes.vue index 8b1ebfa..b75a07e 100644 --- a/src/components/CommandPalettes.vue +++ b/src/components/CommandPalettes.vue @@ -116,10 +116,6 @@ import { useCommandPalattesStore } from '@/stores/command' import { navigationIcon } from '@/utils/route' const command = useCommandPalattesStore() -const searchRoutesByName = (routes: RouteRecordRaw[], query: string): RouteRecordRaw[] => { - const matchingRoutes = extractLeafRoutes(routes, '').filter((item: RouteRecordRaw) => item.path.includes('home/') && item.name?.toString().toLocaleLowerCase().includes(query.toLocaleLowerCase())) - return matchingRoutes -} const route = useRouter() const recent = computed(() => query.value === '' ? command.readRecent() : []) @@ -127,7 +123,7 @@ const query = ref('') const filteredMenus = computed(() => query.value === '' ? [] - : searchRoutesByName(routes, query.value) + : command.searchRoutesByName(routes, query.value) ) const onClose = () => command.open = false diff --git a/src/components/Pages/Gangguan/Daftar/Daftar_1.vue b/src/components/Pages/Gangguan/Daftar/Daftar_1.vue index e6135d5..b6b4d64 100644 --- a/src/components/Pages/Gangguan/Daftar/Daftar_1.vue +++ b/src/components/Pages/Gangguan/Daftar/Daftar_1.vue @@ -112,7 +112,7 @@ const onSelectionChanged = ({ selectedRowsData }: any) => { onMounted(() => { createDummy() - console.log(readDataJson('data-daftar-1')); + // console.log(readDataJson('data-daftar-1')); }) diff --git a/src/stores/command.ts b/src/stores/command.ts index 743df78..86ad874 100644 --- a/src/stores/command.ts +++ b/src/stores/command.ts @@ -2,6 +2,7 @@ import { ref } from 'vue' import { defineStore } from 'pinia' import type { RouteRecordRaw } from 'vue-router' import { readDataJson, writeDataJson } from '@/utils/storage' +import { extractLeafRoutes } from '@/router' export const useCommandPalattesStore = defineStore('command_palettes', () => { const open = ref(false) @@ -71,6 +72,16 @@ export const useCommandPalattesStore = defineStore('command_palettes', () => { writeDataJson('recentmenu', lastRecent) } + const searchRoutesByName = (routes: RouteRecordRaw[], query: string): RouteRecordRaw[] => { + const matchingRoutes = extractLeafRoutes(routes, '').filter((item: RouteRecordRaw) => item.path.includes('home/') && item.name?.toString().toLocaleLowerCase().includes(query.toLocaleLowerCase())) + return matchingRoutes + } + + const searchRoutesPath = (routes: RouteRecordRaw[], query: string): RouteRecordRaw[] => { + const matchingRoutes = extractLeafRoutes(routes, '').filter((item: RouteRecordRaw) => item.path.includes('home/') && item.path === query) + return matchingRoutes + } + return { open, showCommandPalettes, @@ -78,6 +89,8 @@ export const useCommandPalattesStore = defineStore('command_palettes', () => { onKeyPressed, onKeyUp, addRecent, - readRecent + readRecent, + searchRoutesByName, + searchRoutesPath } }) \ No newline at end of file diff --git a/src/stores/menu.ts b/src/stores/menu.ts index 5d81338..5ea0647 100644 --- a/src/stores/menu.ts +++ b/src/stores/menu.ts @@ -5,6 +5,7 @@ import { convertRouteToMenu } from '@/utils/route' import { routes } from '@/router' import type { MenuItemModel } from '@/types/menu' import { splitRoutePath } from '@/utils/texts' +import { useCommandPalattesStore } from '@/stores/command' export const useMenuStore = defineStore('menu', () => { const route = useRoute() @@ -12,6 +13,7 @@ export const useMenuStore = defineStore('menu', () => { const sidebarOpen = ref(false) const sidebarShowed = ref(true) const router = useRouter() + const command = useCommandPalattesStore() const menuSelected = ref(route.fullPath) const toggleSidebar = () => (sidebarOpen.value = !sidebarOpen.value) const toggleSidebarMenu = (path: string, newExpanded: boolean): void => { @@ -54,14 +56,29 @@ export const useMenuStore = defineStore('menu', () => { } }) - watch(router, () => { - if (router.currentRoute.value.fullPath === '/' || router.currentRoute.value.fullPath === '/home') { + watch(router, (value) => { + if (value.currentRoute.value.fullPath === '/' || value.currentRoute.value.fullPath === '/home') { for (const item of navigation.value) { item.expanded = false } } }) + watch(menuSelected, (value) => { + if (value !== '/login' && value !== '/404' && value !== '/home' && value !== '/') { + console.log('current route', value); + + const result = command.searchRoutesPath(routes, value) + if (result.length > 0) { + const route = result.find((item) => item.path === value) + if (route) { + command.addRecent(route) + } + } + + } + }) + return { navigation, toggleSidebar,