-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom url matchers for product and category route
- Loading branch information
Showing
10 changed files
with
116 additions
and
87 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
import { Category } from 'ish-core/models/category/category.model'; | ||
import { generateCategoryRoute } from 'ish-core/route-formats/category.route'; | ||
|
||
@Pipe({ name: 'ishCategoryRoute', pure: true }) | ||
export class CategoryRoutePipe implements PipeTransform { | ||
transform(category: Category): string { | ||
return '/category/' + category.uniqueId; | ||
return generateCategoryRoute(category); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Category } from 'ish-core/models/category/category.model'; | ||
|
||
import { generateCategoryRoute } from './category.route'; | ||
|
||
describe('Category Route', () => { | ||
let cat: Category; | ||
|
||
beforeEach(() => { | ||
cat = { uniqueId: 'cate' } as Category; | ||
}); | ||
|
||
it('should generate category route for category', () => { | ||
expect(generateCategoryRoute(cat)).toEqual('/category/cate'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { UrlSegment } from '@angular/router'; | ||
|
||
import { Category } from 'ish-core/models/category/category.model'; | ||
|
||
export function generateCategoryRoute(category: Category) { | ||
return '/category/' + category.uniqueId; | ||
} | ||
|
||
/** | ||
* UrlMatcher for product route | ||
* Defines a specific URL format for the product page | ||
*/ | ||
export function categoryRouteMatcher(url: UrlSegment[]) { | ||
// Format: category/:categoryUniqueId | ||
if (url[0].path === 'category') { | ||
return { | ||
posParams: { | ||
categoryUniqueId: url[1], | ||
}, | ||
consumed: url, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { UrlSegment } from '@angular/router'; | ||
|
||
import { Category } from 'ish-core/models/category/category.model'; | ||
import { Product } from 'ish-core/models/product/product.model'; | ||
|
||
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 '/'; | ||
} | ||
let productRoute = '/product/' + product.sku; | ||
const productSlug = generateProductSlug(product); | ||
if (productSlug) { | ||
productRoute += '/' + productSlug; | ||
} | ||
|
||
if (category) { | ||
productRoute = `/category/${category.uniqueId}${productRoute}`; | ||
} else { | ||
// TODO: add defaultCategory to route once this information is available with the products REST call | ||
} | ||
return productRoute; | ||
} | ||
|
||
/** | ||
* UrlMatcher for product route | ||
* Defines a specific URL format for the product page | ||
*/ | ||
export function productRouteMatcher(url: UrlSegment[]) { | ||
// Format: product/:sku/:productSlug | ||
if (url[0].path === 'product') { | ||
return { | ||
posParams: { | ||
sku: url[1], | ||
}, | ||
consumed: url, | ||
}; | ||
} | ||
|
||
// Format: category/:categoryUniqueId/product/:sku/:productSlug | ||
if (url.length >= 4 && url[0].path === 'category' && url[2].path === 'product') { | ||
return { | ||
posParams: { | ||
categoryUniqueId: url[1], | ||
sku: url[3], | ||
}, | ||
consumed: url, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters