Skip to content

Commit

Permalink
chore: re-add matcher for current format to not break things
Browse files Browse the repository at this point in the history
new matchers are contained in new files next to the current ones
  • Loading branch information
fmalcher authored and Sebastian-Haehnlein committed Jan 8, 2020
1 parent 003650d commit 09cba8e
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 56 deletions.
37 changes: 37 additions & 0 deletions src/app/core/custom-routes/NEW-category.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Route, UrlSegment, UrlSegmentGroup } from '@angular/router';

import { Category } from 'ish-core/models/category/category.model';

import { CustomRoute } from './custom-route';

export function generateCategoryRoute(category: Category) {
return `/${category.uniqueId}-c`;
}

/**
* UrlMatcher for category route
* Defines a specific URL format for the category page
*/
export function categoryRouteMatcher(url: UrlSegment[], _: UrlSegmentGroup, route: Route) {
if (!route.data) {
route.data = {};
}
route.data.format = '<categoryUniqueId>-c';

// Format: /<categoryUniqueId>-c
if (url.length === 1 && url[0].path.endsWith('-c')) {
const categoryUniqueId = url[0].path.slice(0, -2);
return {
posParams: {
categoryUniqueId: new UrlSegment(categoryUniqueId, {}),
},
consumed: url,
};
}
}

export const categoryRoute: CustomRoute = {
matcher: categoryRouteMatcher,
generateUrl: generateCategoryRoute,
formats: ['<categoryUniqueId>-c'],
};
96 changes: 96 additions & 0 deletions src/app/core/custom-routes/NEW-product.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Route, UrlSegment, UrlSegmentGroup } from '@angular/router';

import { Category } from 'ish-core/models/category/category.model';
import { Product } from 'ish-core/models/product/product.model';

import { CustomRoute } from './custom-route';

export function generateProductSlug(product: Product) {
return product && product.name ? product.name.replace(/[^a-zA-Z0-9-]+/g, '-').replace(/-+$/g, '') : undefined;
}

/**
* Generate a product detail route with optional category context.
* @param product The Product to genereate the route for
* @param category The optional Category that should be used as context for the product route
* @returns Product route string
*/
export function generateProductRoute(product: Product, category?: Category): string {
if (!(product && product.sku)) {
return '/';
}
const productSlug = generateProductSlug(product);
let route = `p-${product.sku}.html`;
if (productSlug) {
route = `${productSlug}-${route}`;
}

if (category) {
route = `${category.uniqueId}-c/${route}`;
}

return '/' + route;
}

/**
* UrlMatcher for product route
* Defines a specific URL format for the product page
*/

export function productRouteMatcher(url: UrlSegment[], _: UrlSegmentGroup, route: Route) {
if (url.length && !url[url.length - 1].path.endsWith('.html')) {
return;
}

if (!route.data) {
route.data = {};
}

let productPath = '';
let categoryUniqueId;

if (url.length === 1) {
productPath = url[0].path;
} else if (url.length === 2) {
// Format: <categoryUniqueId>-c/<productSlug>-p-<sku>.html
route.data.format = '<categoryUniqueId>-c/<productSlug>-p-<sku>.html';
productPath = url[1].path;
categoryUniqueId = url[0].path.slice(0, -2);
}

const productParts = productPath.slice(0, -5).split('-'); // remove '.html'
let sku;

// Format: p-<sku>.html
if (productParts.length === 2 && productParts[0] === 'p') {
route.data.format = 'p-<sku>.html';
sku = productParts[1];
}

// Format: <productSlug>-p-<sku>.html
if (productParts.length >= 3 && [...productParts].splice(-2)[0] === 'p') {
if (!categoryUniqueId) {
route.data.format = '<productSlug>-p-<sku>.html';
}
sku = [...productParts].splice(-1)[0];
}

const posParams: { [key: string]: UrlSegment } = {
sku: new UrlSegment(sku, {}),
};

if (categoryUniqueId) {
posParams.categoryUniqueId = new UrlSegment(categoryUniqueId, {});
}

return {
posParams,
consumed: url,
};
}

export const productRoute: CustomRoute = {
matcher: productRouteMatcher,
generateUrl: generateProductRoute,
formats: ['<categoryUniqueId>-c/<productSlug>-p-<sku>.html', 'p-<sku>.html', '<productSlug>-p-<sku>.html'],
};
13 changes: 6 additions & 7 deletions src/app/core/custom-routes/category.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Category } from 'ish-core/models/category/category.model';
import { CustomRoute } from './custom-route';

export function generateCategoryRoute(category: Category) {
return `/${category.uniqueId}-c`;
return '/category/' + category.uniqueId;
}

/**
Expand All @@ -16,14 +16,13 @@ export function categoryRouteMatcher(url: UrlSegment[], _: UrlSegmentGroup, rout
if (!route.data) {
route.data = {};
}
route.data.format = '<categoryUniqueId>-c';
route.data.format = 'category/:categoryUniqueId';

// Format: /<categoryUniqueId>-c
if (url.length === 1 && url[0].path.endsWith('-c')) {
const categoryUniqueId = url[0].path.slice(0, -2);
// Format: category/:categoryUniqueId
if (url[0].path === 'category') {
return {
posParams: {
categoryUniqueId: new UrlSegment(categoryUniqueId, {}),
categoryUniqueId: url[1],
},
consumed: url,
};
Expand All @@ -33,5 +32,5 @@ export function categoryRouteMatcher(url: UrlSegment[], _: UrlSegmentGroup, rout
export const categoryRoute: CustomRoute = {
matcher: categoryRouteMatcher,
generateUrl: generateCategoryRoute,
formats: ['<categoryUniqueId>-c'],
formats: ['category/:categoryUniqueId'],
};
74 changes: 26 additions & 48 deletions src/app/core/custom-routes/product.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ export function generateProductRoute(product: Product, category?: Category): str
if (!(product && product.sku)) {
return '/';
}
let route = '/product/' + product.sku;
const productSlug = generateProductSlug(product);
let route = `p-${product.sku}.html`;
if (productSlug) {
route = `${productSlug}-${route}`;
route += '/' + productSlug;
}

if (category) {
route = `${category.uniqueId}-c/${route}`;
route = `/category/${category.uniqueId}${route}`;
} else {
// TODO: add defaultCategory to route once this information is available with the products REST call
}

return '/' + route;
return route;
}

/**
Expand All @@ -38,59 +39,36 @@ export function generateProductRoute(product: Product, category?: Category): str
*/

export function productRouteMatcher(url: UrlSegment[], _: UrlSegmentGroup, route: Route) {
if (url.length && !url[url.length - 1].path.endsWith('.html')) {
return;
}

if (!route.data) {
route.data = {};
}

let productPath = '';
let categoryUniqueId;

if (url.length === 1) {
productPath = url[0].path;
} else if (url.length === 2) {
// Format: <categoryUniqueId>-c/<productSlug>-p-<sku>.html
route.data.format = '<categoryUniqueId>-c/<productSlug>-p-<sku>.html';
productPath = url[1].path;
categoryUniqueId = url[0].path.slice(0, -2);
// Format: product/:sku/:productSlug
if (url[0].path === 'product') {
route.data.format = 'product/:sku/:productSlug';
return {
posParams: {
sku: url[1],
},
consumed: url,
};
}

const productParts = productPath.slice(0, -5).split('-'); // remove '.html'
let sku;

// Format: p-<sku>.html
if (productParts.length === 2 && productParts[0] === 'p') {
route.data.format = 'p-<sku>.html';
sku = productParts[1];
// Format: category/:categoryUniqueId/product/:sku/:productSlug
if (url.length >= 4 && url[0].path === 'category' && url[2].path === 'product') {
route.data.format = 'category/:categoryUniqueId/product/:sku/:productSlug';
return {
posParams: {
categoryUniqueId: url[1],
sku: url[3],
},
consumed: url,
};
}

// Format: <productSlug>-p-<sku>.html
if (productParts.length >= 3 && [...productParts].splice(-2)[0] === 'p') {
if (!categoryUniqueId) {
route.data.format = '<productSlug>-p-<sku>.html';
}
sku = [...productParts].splice(-1)[0];
}

const posParams: { [key: string]: UrlSegment } = {
sku: new UrlSegment(sku, {}),
};

if (categoryUniqueId) {
posParams.categoryUniqueId = new UrlSegment(categoryUniqueId, {});
}

return {
posParams,
consumed: url,
};
}

export const productRoute: CustomRoute = {
matcher: productRouteMatcher,
generateUrl: generateProductRoute,
formats: ['<categoryUniqueId>-c/<productSlug>-p-<sku>.html', 'p-<sku>.html', '<productSlug>-p-<sku>.html'],
formats: ['product/:sku/:productSlug', 'category/:categoryUniqueId/product/:sku/:productSlug'],
};
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export class CategoriesEffects {
@Effect()
productOrCategoryChanged$ = this.actions$.pipe(
ofRoute(categoryRoute.formats),
log('CAT EFF'),
mapToParam<string>('categoryUniqueId'),
switchMap(() => this.store.pipe(select(selectors.getSelectedCategory))),
whenTruthy(),
Expand Down

0 comments on commit 09cba8e

Please sign in to comment.