Skip to content

Commit

Permalink
feat(demo): add product external resolution (#1843)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Nov 24, 2023
1 parent 6efc2c4 commit 669fe5a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@daffodil/checkout": "0.0.0-PLACEHOLDER",
"@daffodil/core": "0.0.0-PLACEHOLDER",
"@daffodil/design": "0.0.0-PLACEHOLDER",
"@daffodil/external-router": "0.0.0-PLACEHOLDER",
"@daffodil/newsletter": "0.0.0-PLACEHOLDER",
"@daffodil/navigation": "0.0.0-PLACEHOLDER",
"@daffodil/driver": "0.0.0-PLACEHOLDER"
Expand Down
38 changes: 36 additions & 2 deletions apps/demo/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import {
RouterModule,
} from '@angular/router';

import { DaffProductPageIdResolver } from '@daffodil/product/routing';
import {
DaffExternalRouterModule,
daffDataPathUrlMatcher,
daffInsertDataPathStrategy,
} from '@daffodil/external-router';
import { DaffExternalRouterExistenceGuard } from '@daffodil/external-router/routing';
import {
DaffProductPageIdResolver,
DaffProductPageUrlResolver,
} from '@daffodil/product/routing';

import { EmptyCartResolver } from './cart/routing-resolvers/resolvers/empty-cart-resolver.service';
import { CheckoutViewComponent } from './checkout/pages/checkout-view/checkout-view.component';
Expand All @@ -31,6 +40,16 @@ export const appRoutes: Routes = [
product: DaffProductPageIdResolver,
},
},
{
matcher: daffDataPathUrlMatcher,
data: {
daffExternalRouteType: 'PRODUCT',
},
component: ProductViewComponent,
resolve: {
product: DaffProductPageUrlResolver,
},
},
{
path: 'checkout',
loadChildren: () => import('./checkout/checkout.module').then(m => m.CheckoutModule),
Expand All @@ -40,7 +59,8 @@ export const appRoutes: Routes = [
},
{
path: '**',
redirectTo: '/404',
canActivate: [DaffExternalRouterExistenceGuard],
children: [],
},
];

Expand All @@ -49,6 +69,20 @@ export const appRoutes: Routes = [
RouterModule.forRoot(appRoutes, {
scrollPositionRestoration: 'enabled',
}),
DaffExternalRouterModule.forRoot({
failedResolutionPath: '404',
notFoundResolutionPath: '404',
}, [
// {
// type: 'CATEGORY',
// route: {},
// },
{
type: 'PRODUCT',
insertionStrategy: daffInsertDataPathStrategy,
route: {},
},
]),
],
exports: [
RouterModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
inject,
InjectionToken,
} from '@angular/core';

import { DaffInMemoryBackendCategoryService } from '@daffodil/category/driver/in-memory';
import { DAFF_EXTERNAL_ROUTER_NOT_FOUND_RESOLUTION } from '@daffodil/external-router/driver';
import { DaffExternalRouterDriverInMemoryConfig } from '@daffodil/external-router/driver/in-memory';
import { DaffInMemoryBackendProductService } from '@daffodil/product/driver/in-memory';

export const DEMO_EXTERNAL_ROUTER_DRIVER_IN_MEMORY_CONFIG
= new InjectionToken<DaffExternalRouterDriverInMemoryConfig>('DEMO_EXTERNAL_ROUTER_DRIVER_IN_MEMORY_CONFIG', {
factory: () => {
const productBackend = inject(DaffInMemoryBackendProductService);
// const categoryBackend = inject(DaffInMemoryBackendCategoryService);

return {
resolver: url => {
const product = productBackend.products.find(p => p.url === url);
if (product) {
return {
id: product.id,
url,
// TODO(griest024): don't hardcode this
type: 'PRODUCT',
code: 200,
};
}

// const category = categoryBackend.categories.find(c => c.url === url);
// if (category) {
// return {
// id: category.id,
// url,
// // TODO(griest024): don't hardcode this
// type: 'CATEGORY',
// code: 200,
// };
// }

return DAFF_EXTERNAL_ROUTER_NOT_FOUND_RESOLUTION;
},
};
},
})
;
3 changes: 3 additions & 0 deletions apps/demo/src/app/drivers/in-memory/in-memory.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
import { DaffAuthInMemoryDriverModule } from '@daffodil/auth/driver/in-memory';
import { DaffCartInMemoryDriverModule } from '@daffodil/cart/driver/in-memory';
import { DaffCheckoutInMemoryDriverModule } from '@daffodil/checkout/testing';
import { DaffExternalRouterDriverInMemoryModule } from '@daffodil/external-router/driver/in-memory';
import { DaffGeographyInMemoryDriverModule } from '@daffodil/geography/driver/in-memory';
import { DaffNavigationInMemoryDriverModule } from '@daffodil/navigation/driver/in-memory';
import { DaffNewsletterInMemoryDriverModule } from '@daffodil/newsletter/driver/in-memory';
import { DaffProductInMemoryDriverModule } from '@daffodil/product/driver/in-memory';

import { DemoInMemoryBackendService } from './backend/backend.service';
import { DEMO_EXTERNAL_ROUTER_DRIVER_IN_MEMORY_CONFIG } from './external-router.config.token';


@NgModule({
Expand All @@ -22,6 +24,7 @@ import { DemoInMemoryBackendService } from './backend/backend.service';
DaffNavigationInMemoryDriverModule.forRoot(),
DaffNewsletterInMemoryDriverModule.forRoot(),
DaffGeographyInMemoryDriverModule.forRoot(),
DaffExternalRouterDriverInMemoryModule.forRoot(DEMO_EXTERNAL_ROUTER_DRIVER_IN_MEMORY_CONFIG),
],
})
export class DemoInMemoryDriverModule { }

0 comments on commit 669fe5a

Please sign in to comment.