Skip to content

Commit

Permalink
Update router
Browse files Browse the repository at this point in the history
  • Loading branch information
rayc2045 committed Apr 30, 2024
1 parent 7989500 commit 0625809
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,19 @@ const utils = {
setTimeout(resolve, delay * 1000);
});
},
getParamsByUrl(url = window.location.href) {
getUrlParam(url = window.location.href) {
const urlSearch = url.split('?')[1];
const urlSearchParams = new URLSearchParams(urlSearch);
const entries = Object.fromEntries(urlSearchParams.entries());
Object.keys(entries).forEach(entry => {
const split = entries[entry].split(' ');
if (split.length === 1 && split[0] === '') return (entries[entry] = []);
entries[entry] = split;
const param = Object.fromEntries(urlSearchParams.entries());
Object.keys(param).forEach(key => {
const items = param[key].split(' ');
if (items.length === 1) {
if (!items[0].length) return (param[key] = null);
return (param[key] = items[0]);
}
param[key] = items;
});
return entries;
return param;
},
async scrollToTop() {
scrollTo({ top: 0, behavior: prefer.motion ? 'smooth' : 'auto' });
Expand Down Expand Up @@ -155,12 +158,12 @@ const router = {
routes: [
{ path: '/', component: '/home.html' },
{ path: '/posts', component: '/blog/blog.html', title: 'Recent from blog' },
{ path: '/posts/:post', component: '/blog/post.html' },
{ path: '/posts/:title', component: '/blog/post.html' },
{ path: '/about', component: '/about/about-us.html', title: 'About Us' },
{ path: '/contact', component: '/about/contact.html', title: 'Contact Us' },
{ path: '/faq', component: '/about/faq.html', title: 'Frequently asked questions' },
{ path: '/shop', component: '/shop/shop.html', title: 'Shop' },
{ path: '/shop/:product', component: '/shop/product.html' },
{ path: '/shop/:name', component: '/shop/product.html' },
{ path: '/cart', component: '/shop/cart.html', title: 'My cart' },
{ path: '/login', component: '/login.html', title: 'Log in' },
{ path: '/account', component: '/account.html', title: 'My account' },
Expand All @@ -176,9 +179,9 @@ const router = {
}) || this.routes.find(route => route.path === '/404')
);
},
get currentRouteParam() {
if (!this.currentRoute.path.includes('/:')) return null;
const param = {};
get currentParam() {
const param = utils.getUrlParam();
if (!this.currentRoute.path.includes(':')) return param;
const routePathFrags = this.currentRoute.path.split('/'),
currentPathFrags = this.currentPath.split('/');
for (let i = 0; i < routePathFrags.length; i++)
Expand Down

0 comments on commit 0625809

Please sign in to comment.