refactor: expose replaceRoute directly

This commit is contained in:
Shivam Mishra
2024-08-21 18:28:57 +05:30
parent c697964110
commit b605362f09
9 changed files with 44 additions and 74 deletions
@@ -0,0 +1,21 @@
import { useRouter, useRoute } from 'dashboard/composables/route';
/**
* Composable for replacing the current route with a new one if it's different.
* @param {string} name - The name of the route to replace with.
* @param {Object} params - The params to pass to the new route.
* @returns {Promise|undefined} A promise that resolves when the navigation is complete, or undefined if no navigation occurs.
*/
export const useReplaceRoute = async (name, params = {}) => {
const router = useRouter();
const route = useRoute();
return new Promise((resolve, reject) => {
if (route.name !== name) {
router.replace({ name, params }).then(resolve).catch(reject);
}
resolve(undefined);
});
};
export default useReplaceRoute;
@@ -1,26 +0,0 @@
import { useRouter, useRoute } from 'dashboard/composables/route';
/**
* Composable for handling router-related operations.
* @returns {Object} An object containing methods for router manipulation.
*/
export function useRouterHelper() {
const router = useRouter();
const route = useRoute();
/**
* Replaces the current route with a new one if it's different.
* @param {string} name - The name of the route to replace with.
* @param {Object} params - The params to pass to the new route.
* @returns {Promise|undefined} A promise that resolves when the navigation is complete, or undefined if no navigation occurs.
*/
const replaceRoute = async (name, params = {}) => {
if (route.name !== name) {
return router.replace({ name, params });
}
return undefined;
};
return {
replaceRoute,
};
}