diff --git a/src/components/ui/VnSearchbar.vue b/src/components/ui/VnSearchbar.vue index 344267ef7..ae8ac05dd 100644 --- a/src/components/ui/VnSearchbar.vue +++ b/src/components/ui/VnSearchbar.vue @@ -1,9 +1,9 @@ diff --git a/src/composables/useRedirect.js b/src/composables/useRedirect.js new file mode 100644 index 000000000..786e1378b --- /dev/null +++ b/src/composables/useRedirect.js @@ -0,0 +1,28 @@ +import { useRouter } from 'vue-router'; + +export default function useRedirect() { + const router = useRouter(); + + const navigate = (data, { customRouteRedirectName, searchText }) => { + if (customRouteRedirectName) + return router.push({ + name: customRouteRedirectName, + params: { id: searchText }, + }); + + const { matched: matches } = router.currentRoute.value; + const { path } = matches.at(-1); + const [, moduleName] = path.split('/'); + + if (!data.length || data.length > 1) + return router.push({ path: `/${moduleName}/list` }); + + const targetId = data[0].id; + + if (/\/list|-list/.test(path)) + router.push({ path: `/${moduleName}/${targetId}/summary` }); + else router.push({ path: path.replace(/:id/, targetId) }); + }; + + return { navigate }; +}