From 48f4772d804aa463a9df1815a3464be3917304a4 Mon Sep 17 00:00:00 2001 From: Uday Vunnam <20707504+udayvunnam@users.noreply.github.com> Date: Mon, 3 Jan 2022 10:30:37 +0530 Subject: [PATCH] fix: respect angular guard checks and do not navigate if authGuard check returns false (#114) --- .gitignore | 2 + angular.json | 2 + .../breadcrumb-demo/src/app/app.component.css | 17 ++-- .../app/connect/connect/connect.component.css | 7 +- apps/breadcrumb-demo/src/styles.css | 18 ++-- apps/with-gaurd-check-e2e/.eslintrc.json | 10 ++ apps/with-gaurd-check-e2e/cypress.json | 12 +++ apps/with-gaurd-check-e2e/project.json | 28 ++++++ .../src/fixtures/example.json | 4 + .../src/integration/app.spec.ts | 36 +++++++ .../src/support/app.po.ts | 1 + .../src/support/commands.ts | 33 +++++++ .../with-gaurd-check-e2e/src/support/index.ts | 17 ++++ apps/with-gaurd-check-e2e/tsconfig.json | 19 ++++ apps/with-gaurd-check/.browserslistrc | 16 +++ apps/with-gaurd-check/.eslintrc.json | 36 +++++++ apps/with-gaurd-check/jest.config.js | 21 ++++ apps/with-gaurd-check/project.json | 93 ++++++++++++++++++ .../src/app/app-routing.module.ts | 44 +++++++++ .../src/app/app.component.css | 0 .../src/app/app.component.html | 9 ++ .../with-gaurd-check/src/app/app.component.ts | 10 ++ apps/with-gaurd-check/src/app/app.module.ts | 20 ++++ .../src/app/page-guard.service.ts | 35 +++++++ .../src/app/page1.component.ts | 7 ++ .../src/app/page2-child.component.ts | 7 ++ .../src/app/page2.component.ts | 7 ++ apps/with-gaurd-check/src/assets/.gitkeep | 0 .../src/environments/environment.prod.ts | 3 + .../src/environments/environment.ts | 16 +++ apps/with-gaurd-check/src/favicon.ico | Bin 0 -> 15086 bytes apps/with-gaurd-check/src/index.html | 13 +++ apps/with-gaurd-check/src/main.ts | 13 +++ apps/with-gaurd-check/src/polyfills.ts | 52 ++++++++++ apps/with-gaurd-check/src/styles.css | 1 + apps/with-gaurd-check/src/test-setup.ts | 1 + apps/with-gaurd-check/tsconfig.app.json | 10 ++ apps/with-gaurd-check/tsconfig.editor.json | 7 ++ apps/with-gaurd-check/tsconfig.json | 27 +++++ apps/with-gaurd-check/tsconfig.spec.json | 10 ++ .../src/lib/breadcrumb.component.ts | 4 +- .../src/lib/breadcrumb.service.ts | 12 +-- 42 files changed, 653 insertions(+), 27 deletions(-) create mode 100644 apps/with-gaurd-check-e2e/.eslintrc.json create mode 100644 apps/with-gaurd-check-e2e/cypress.json create mode 100644 apps/with-gaurd-check-e2e/project.json create mode 100644 apps/with-gaurd-check-e2e/src/fixtures/example.json create mode 100644 apps/with-gaurd-check-e2e/src/integration/app.spec.ts create mode 100644 apps/with-gaurd-check-e2e/src/support/app.po.ts create mode 100644 apps/with-gaurd-check-e2e/src/support/commands.ts create mode 100644 apps/with-gaurd-check-e2e/src/support/index.ts create mode 100644 apps/with-gaurd-check-e2e/tsconfig.json create mode 100644 apps/with-gaurd-check/.browserslistrc create mode 100644 apps/with-gaurd-check/.eslintrc.json create mode 100644 apps/with-gaurd-check/jest.config.js create mode 100644 apps/with-gaurd-check/project.json create mode 100644 apps/with-gaurd-check/src/app/app-routing.module.ts create mode 100644 apps/with-gaurd-check/src/app/app.component.css create mode 100644 apps/with-gaurd-check/src/app/app.component.html create mode 100644 apps/with-gaurd-check/src/app/app.component.ts create mode 100644 apps/with-gaurd-check/src/app/app.module.ts create mode 100644 apps/with-gaurd-check/src/app/page-guard.service.ts create mode 100644 apps/with-gaurd-check/src/app/page1.component.ts create mode 100644 apps/with-gaurd-check/src/app/page2-child.component.ts create mode 100644 apps/with-gaurd-check/src/app/page2.component.ts create mode 100644 apps/with-gaurd-check/src/assets/.gitkeep create mode 100644 apps/with-gaurd-check/src/environments/environment.prod.ts create mode 100644 apps/with-gaurd-check/src/environments/environment.ts create mode 100644 apps/with-gaurd-check/src/favicon.ico create mode 100644 apps/with-gaurd-check/src/index.html create mode 100644 apps/with-gaurd-check/src/main.ts create mode 100644 apps/with-gaurd-check/src/polyfills.ts create mode 100644 apps/with-gaurd-check/src/styles.css create mode 100644 apps/with-gaurd-check/src/test-setup.ts create mode 100644 apps/with-gaurd-check/tsconfig.app.json create mode 100644 apps/with-gaurd-check/tsconfig.editor.json create mode 100644 apps/with-gaurd-check/tsconfig.json create mode 100644 apps/with-gaurd-check/tsconfig.spec.json diff --git a/.gitignore b/.gitignore index 84c8d4f..b54b45a 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,5 @@ testem.log # System Files .DS_Store Thumbs.db + +.angular diff --git a/angular.json b/angular.json index 588e25b..4e924c4 100644 --- a/angular.json +++ b/angular.json @@ -8,6 +8,8 @@ "got-demo-e2e": "apps/got-demo-e2e", "simple-demo": "apps/simple-demo", "simple-demo-e2e": "apps/simple-demo-e2e", + "with-gaurd-check": "apps/with-gaurd-check", + "with-gaurd-check-e2e": "apps/with-gaurd-check-e2e", "xng-breadcrumb": "libs/xng-breadcrumb" } } diff --git a/apps/breadcrumb-demo/src/app/app.component.css b/apps/breadcrumb-demo/src/app/app.component.css index 0cc00a3..3575586 100644 --- a/apps/breadcrumb-demo/src/app/app.component.css +++ b/apps/breadcrumb-demo/src/app/app.component.css @@ -5,15 +5,16 @@ .route-container { padding: 16px; +} - div { - display: flex; - flex-direction: row; - align-items: center; - label { - min-width: 240px; - } - } +.route-container div { + display: flex; + flex-direction: row; + align-items: center; +} + +.route-container div label { + min-width: 240px; } @media (max-width: 960px) { diff --git a/apps/breadcrumb-demo/src/app/connect/connect/connect.component.css b/apps/breadcrumb-demo/src/app/connect/connect/connect.component.css index a563ed3..29ba0d4 100644 --- a/apps/breadcrumb-demo/src/app/connect/connect/connect.component.css +++ b/apps/breadcrumb-demo/src/app/connect/connect/connect.component.css @@ -1,7 +1,8 @@ .connect { display: flex; justify-content: center; - button { - width: 200px; - } +} + +.connect button { + width: 200px; } diff --git a/apps/breadcrumb-demo/src/styles.css b/apps/breadcrumb-demo/src/styles.css index e33c902..062cedd 100644 --- a/apps/breadcrumb-demo/src/styles.css +++ b/apps/breadcrumb-demo/src/styles.css @@ -11,9 +11,10 @@ body { margin-top: 16px; justify-content: flex-end; display: flex; - > *:not(:last-child) { - margin-right: 16px; - } +} + +.button-row > *:not(:last-child) { + margin-right: 16px; } .label { @@ -64,9 +65,10 @@ body { .custom-breadcrumb.xng-breadcrumb-root { background-color: #fff; - .xng-breadcrumb-item { - border-radius: 16px; - background-color: #f5f5f5; - padding: 4px 8px; - } +} + +.custom-breadcrumb.xng-breadcrumb-root .xng-breadcrumb-item { + border-radius: 16px; + background-color: #f5f5f5; + padding: 4px 8px; } diff --git a/apps/with-gaurd-check-e2e/.eslintrc.json b/apps/with-gaurd-check-e2e/.eslintrc.json new file mode 100644 index 0000000..696cb8b --- /dev/null +++ b/apps/with-gaurd-check-e2e/.eslintrc.json @@ -0,0 +1,10 @@ +{ + "extends": ["plugin:cypress/recommended", "../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], + "rules": {} + } + ] +} diff --git a/apps/with-gaurd-check-e2e/cypress.json b/apps/with-gaurd-check-e2e/cypress.json new file mode 100644 index 0000000..5bb54e1 --- /dev/null +++ b/apps/with-gaurd-check-e2e/cypress.json @@ -0,0 +1,12 @@ +{ + "fileServerFolder": ".", + "fixturesFolder": "./src/fixtures", + "integrationFolder": "./src/integration", + "modifyObstructiveCode": false, + "supportFile": "./src/support/index.ts", + "pluginsFile": false, + "video": false, + "videosFolder": "../../dist/cypress/apps/with-gaurd-check-e2e/videos", + "screenshotsFolder": "../../dist/cypress/apps/with-gaurd-check-e2e/screenshots", + "chromeWebSecurity": false +} diff --git a/apps/with-gaurd-check-e2e/project.json b/apps/with-gaurd-check-e2e/project.json new file mode 100644 index 0000000..a16df73 --- /dev/null +++ b/apps/with-gaurd-check-e2e/project.json @@ -0,0 +1,28 @@ +{ + "root": "apps/with-gaurd-check-e2e", + "sourceRoot": "apps/with-gaurd-check-e2e/src", + "projectType": "application", + "targets": { + "e2e": { + "executor": "@nrwl/cypress:cypress", + "options": { + "cypressConfig": "apps/with-gaurd-check-e2e/cypress.json", + "devServerTarget": "with-gaurd-check:serve:development" + }, + "configurations": { + "production": { + "devServerTarget": "with-gaurd-check:serve:production" + } + } + }, + "lint": { + "executor": "@nrwl/linter:eslint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": ["apps/with-gaurd-check-e2e/**/*.{js,ts}"] + } + } + }, + "tags": [], + "implicitDependencies": ["with-gaurd-check"] +} diff --git a/apps/with-gaurd-check-e2e/src/fixtures/example.json b/apps/with-gaurd-check-e2e/src/fixtures/example.json new file mode 100644 index 0000000..294cbed --- /dev/null +++ b/apps/with-gaurd-check-e2e/src/fixtures/example.json @@ -0,0 +1,4 @@ +{ + "name": "Using fixtures to represent data", + "email": "hello@cypress.io" +} diff --git a/apps/with-gaurd-check-e2e/src/integration/app.spec.ts b/apps/with-gaurd-check-e2e/src/integration/app.spec.ts new file mode 100644 index 0000000..cf266da --- /dev/null +++ b/apps/with-gaurd-check-e2e/src/integration/app.spec.ts @@ -0,0 +1,36 @@ +describe('with-gaurd-check', () => { + it('should contain breadcrumbs for page1', () => { + cy.visit('/'); + cy.get('.xng-breadcrumb-list').contains('Page 1'); + cy.get('button').contains('To Page 2 Child').click(); + cy.get('.xng-breadcrumb-list').contains('Page2 child'); + }); + + it('Auth guard false should block navigation from breadcrumb', () => { + cy.get('button').contains('Back to Page 1').click(); + cy.on('window:confirm', (text) => { + expect(text).to.contains( + 'Are you sure you want to navigate away before saving changes?' + ); + return false; + }); + cy.get('.xng-breadcrumb-list').contains('Page2 child'); // navigation didn't happen + cy.location().should((loc) => { + expect(loc.pathname).to.eq('/page2/page2-child'); + }); + }); + + it('Auth guard true should allow navigation from breadcrumb', () => { + cy.get('button').contains('Back to Page 1').click(); + cy.on('window:confirm', (text) => { + expect(text).to.contains( + 'Are you sure you want to navigate away before saving changes?' + ); + return true; + }); + cy.get('.xng-breadcrumb-list').contains('Page2 child').should('not.exist'); // navigation happen + cy.location().should((loc) => { + expect(loc.pathname).to.eq('/page1'); + }); + }); +}); diff --git a/apps/with-gaurd-check-e2e/src/support/app.po.ts b/apps/with-gaurd-check-e2e/src/support/app.po.ts new file mode 100644 index 0000000..3293424 --- /dev/null +++ b/apps/with-gaurd-check-e2e/src/support/app.po.ts @@ -0,0 +1 @@ +export const getGreeting = () => cy.get('h1'); diff --git a/apps/with-gaurd-check-e2e/src/support/commands.ts b/apps/with-gaurd-check-e2e/src/support/commands.ts new file mode 100644 index 0000000..310f1fa --- /dev/null +++ b/apps/with-gaurd-check-e2e/src/support/commands.ts @@ -0,0 +1,33 @@ +// *********************************************** +// This example commands.js shows you how to +// create various custom commands and overwrite +// existing commands. +// +// For more comprehensive examples of custom +// commands please read more here: +// https://on.cypress.io/custom-commands +// *********************************************** + +// eslint-disable-next-line @typescript-eslint/no-namespace +declare namespace Cypress { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + interface Chainable { + login(email: string, password: string): void; + } +} +// +// -- This is a parent command -- +Cypress.Commands.add('login', (email, password) => { + console.log('Custom command example: Login', email, password); +}); +// +// -- This is a child command -- +// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... }) +// +// +// -- This is a dual command -- +// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... }) +// +// +// -- This will overwrite an existing command -- +// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) diff --git a/apps/with-gaurd-check-e2e/src/support/index.ts b/apps/with-gaurd-check-e2e/src/support/index.ts new file mode 100644 index 0000000..3d469a6 --- /dev/null +++ b/apps/with-gaurd-check-e2e/src/support/index.ts @@ -0,0 +1,17 @@ +// *********************************************************** +// This example support/index.js is processed and +// loaded automatically before your test files. +// +// This is a great place to put global configuration and +// behavior that modifies Cypress. +// +// You can change the location of this file or turn off +// automatically serving support files with the +// 'supportFile' configuration option. +// +// You can read more here: +// https://on.cypress.io/configuration +// *********************************************************** + +// Import commands.js using ES2015 syntax: +import './commands'; diff --git a/apps/with-gaurd-check-e2e/tsconfig.json b/apps/with-gaurd-check-e2e/tsconfig.json new file mode 100644 index 0000000..8ff0a82 --- /dev/null +++ b/apps/with-gaurd-check-e2e/tsconfig.json @@ -0,0 +1,19 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "sourceMap": false, + "outDir": "../../dist/out-tsc", + "allowJs": true, + "types": ["cypress", "node"], + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "include": ["src/**/*.ts", "src/**/*.js"], + "angularCompilerOptions": { + "strictInjectionParameters": true, + "strictInputAccessModifiers": true, + "strictTemplates": true + } +} diff --git a/apps/with-gaurd-check/.browserslistrc b/apps/with-gaurd-check/.browserslistrc new file mode 100644 index 0000000..4f9ac26 --- /dev/null +++ b/apps/with-gaurd-check/.browserslistrc @@ -0,0 +1,16 @@ +# This file is used by the build system to adjust CSS and JS output to support the specified browsers below. +# For additional information regarding the format and rule options, please see: +# https://github.com/browserslist/browserslist#queries + +# For the full list of supported browsers by the Angular framework, please see: +# https://angular.io/guide/browser-support + +# You can see what browsers were selected by your queries by running: +# npx browserslist + +last 1 Chrome version +last 1 Firefox version +last 2 Edge major versions +last 2 Safari major versions +last 2 iOS major versions +Firefox ESR diff --git a/apps/with-gaurd-check/.eslintrc.json b/apps/with-gaurd-check/.eslintrc.json new file mode 100644 index 0000000..a4ae7f7 --- /dev/null +++ b/apps/with-gaurd-check/.eslintrc.json @@ -0,0 +1,36 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts"], + "extends": [ + "plugin:@nrwl/nx/angular", + "plugin:@angular-eslint/template/process-inline-templates" + ], + "rules": { + "@angular-eslint/directive-selector": [ + "error", + { + "type": "attribute", + "prefix": "wgc", + "style": "camelCase" + } + ], + "@angular-eslint/component-selector": [ + "error", + { + "type": "element", + "prefix": "wgc", + "style": "kebab-case" + } + ] + } + }, + { + "files": ["*.html"], + "extends": ["plugin:@nrwl/nx/angular-template"], + "rules": {} + } + ] +} diff --git a/apps/with-gaurd-check/jest.config.js b/apps/with-gaurd-check/jest.config.js new file mode 100644 index 0000000..2a72c2f --- /dev/null +++ b/apps/with-gaurd-check/jest.config.js @@ -0,0 +1,21 @@ +module.exports = { + displayName: 'with-gaurd-check', + preset: '../../jest.preset.js', + setupFilesAfterEnv: ['/src/test-setup.ts'], + globals: { + 'ts-jest': { + tsconfig: '/tsconfig.spec.json', + stringifyContentPathRegex: '\\.(html|svg)$', + }, + }, + coverageDirectory: '../../coverage/apps/with-gaurd-check', + transform: { + '^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular', + }, + transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'], + snapshotSerializers: [ + 'jest-preset-angular/build/serializers/no-ng-attributes', + 'jest-preset-angular/build/serializers/ng-snapshot', + 'jest-preset-angular/build/serializers/html-comment', + ], +}; diff --git a/apps/with-gaurd-check/project.json b/apps/with-gaurd-check/project.json new file mode 100644 index 0000000..85b0ad3 --- /dev/null +++ b/apps/with-gaurd-check/project.json @@ -0,0 +1,93 @@ +{ + "projectType": "application", + "root": "apps/with-gaurd-check", + "sourceRoot": "apps/with-gaurd-check/src", + "prefix": "wgc", + "targets": { + "build": { + "executor": "@angular-devkit/build-angular:browser", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/apps/with-gaurd-check", + "index": "apps/with-gaurd-check/src/index.html", + "main": "apps/with-gaurd-check/src/main.ts", + "polyfills": "apps/with-gaurd-check/src/polyfills.ts", + "tsConfig": "apps/with-gaurd-check/tsconfig.app.json", + "assets": [ + "apps/with-gaurd-check/src/favicon.ico", + "apps/with-gaurd-check/src/assets" + ], + "styles": ["apps/with-gaurd-check/src/styles.css"], + "scripts": [] + }, + "configurations": { + "production": { + "budgets": [ + { + "type": "initial", + "maximumWarning": "500kb", + "maximumError": "1mb" + }, + { + "type": "anyComponentStyle", + "maximumWarning": "2kb", + "maximumError": "4kb" + } + ], + "fileReplacements": [ + { + "replace": "apps/with-gaurd-check/src/environments/environment.ts", + "with": "apps/with-gaurd-check/src/environments/environment.prod.ts" + } + ], + "outputHashing": "all" + }, + "development": { + "buildOptimizer": false, + "optimization": false, + "vendorChunk": true, + "extractLicenses": false, + "sourceMap": true, + "namedChunks": true + } + }, + "defaultConfiguration": "production" + }, + "serve": { + "executor": "@angular-devkit/build-angular:dev-server", + "configurations": { + "production": { + "browserTarget": "with-gaurd-check:build:production" + }, + "development": { + "browserTarget": "with-gaurd-check:build:development" + } + }, + "defaultConfiguration": "development" + }, + "extract-i18n": { + "executor": "@angular-devkit/build-angular:extract-i18n", + "options": { + "browserTarget": "with-gaurd-check:build" + } + }, + "lint": { + "executor": "@nrwl/linter:eslint", + "options": { + "lintFilePatterns": [ + "apps/with-gaurd-check/src/**/*.ts", + "apps/with-gaurd-check/src/**/*.html" + ] + } + }, + "test": { + "executor": "@nrwl/jest:jest", + "outputs": ["coverage/apps/with-gaurd-check"], + "options": { + "jestConfig": "apps/with-gaurd-check/jest.config.js", + "passWithNoTests": true + } + } + }, + "tags": [] +} diff --git a/apps/with-gaurd-check/src/app/app-routing.module.ts b/apps/with-gaurd-check/src/app/app-routing.module.ts new file mode 100644 index 0000000..ec03e04 --- /dev/null +++ b/apps/with-gaurd-check/src/app/app-routing.module.ts @@ -0,0 +1,44 @@ +import { NgModule } from '@angular/core'; +import { Routes, RouterModule } from '@angular/router'; +import { PageGuard } from './page-guard.service'; +import { Page1Component } from './page1.component'; +import { Page2ChildComponent } from './page2-child.component'; +import { Page2Component } from './page2.component'; + +export const appRoutes: Routes = [ + { + path: '', + redirectTo: 'page1', + pathMatch: 'full', + }, + { + path: 'page1', + component: Page1Component, + data: { + breadcrumb: 'Page 1', + }, + }, + { + path: 'page2', + component: Page2Component, + data: { + breadcrumb: 'Page 2', + }, + children: [ + { + path: 'page2-child', + component: Page2ChildComponent, + data: { + breadcrumb: 'Page2 child', + }, + canDeactivate: [PageGuard], + }, + ], + }, +]; + +@NgModule({ + imports: [RouterModule.forRoot(appRoutes)], + exports: [RouterModule], +}) +export class AppRoutingModule {} diff --git a/apps/with-gaurd-check/src/app/app.component.css b/apps/with-gaurd-check/src/app/app.component.css new file mode 100644 index 0000000..e69de29 diff --git a/apps/with-gaurd-check/src/app/app.component.html b/apps/with-gaurd-check/src/app/app.component.html new file mode 100644 index 0000000..1920ef0 --- /dev/null +++ b/apps/with-gaurd-check/src/app/app.component.html @@ -0,0 +1,9 @@ +

xng-breadcrumb

+ + + +
+ + + + diff --git a/apps/with-gaurd-check/src/app/app.component.ts b/apps/with-gaurd-check/src/app/app.component.ts new file mode 100644 index 0000000..bb7be29 --- /dev/null +++ b/apps/with-gaurd-check/src/app/app.component.ts @@ -0,0 +1,10 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'wgc-root', + templateUrl: './app.component.html', + styleUrls: ['./app.component.css'], +}) +export class AppComponent { + title = 'with-gaurd-check'; +} diff --git a/apps/with-gaurd-check/src/app/app.module.ts b/apps/with-gaurd-check/src/app/app.module.ts new file mode 100644 index 0000000..88ed299 --- /dev/null +++ b/apps/with-gaurd-check/src/app/app.module.ts @@ -0,0 +1,20 @@ +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +import { AppComponent } from './app.component'; +import { RouterModule } from '@angular/router'; +import { BreadcrumbModule } from 'xng-breadcrumb'; +import { AppRoutingModule } from './app-routing.module'; + +@NgModule({ + declarations: [AppComponent], + imports: [ + BrowserModule, + BreadcrumbModule, + RouterModule.forRoot([]), + AppRoutingModule, + ], + providers: [], + bootstrap: [AppComponent], +}) +export class AppModule {} diff --git a/apps/with-gaurd-check/src/app/page-guard.service.ts b/apps/with-gaurd-check/src/app/page-guard.service.ts new file mode 100644 index 0000000..743aa90 --- /dev/null +++ b/apps/with-gaurd-check/src/app/page-guard.service.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@angular/core'; +import { + ActivatedRouteSnapshot, + CanDeactivate, + RouterStateSnapshot, + UrlTree, +} from '@angular/router'; +import { Observable } from 'rxjs'; +import { BreadcrumbService } from 'xng-breadcrumb'; + +interface BaseComponent { + isFormValid: () => boolean; +} + +@Injectable({ + providedIn: 'root', +}) +export class PageGuard implements CanDeactivate { + constructor(private breadcrumbService: BreadcrumbService) {} + + canDeactivate( + component: BaseComponent, + currentRoute: ActivatedRouteSnapshot, + currentState: RouterStateSnapshot, + nextState?: RouterStateSnapshot + ): + | boolean + | UrlTree + | Observable + | Promise { + return window.confirm( + 'Are you sure you want to navigate away before saving changes?' + ); + } +} diff --git a/apps/with-gaurd-check/src/app/page1.component.ts b/apps/with-gaurd-check/src/app/page1.component.ts new file mode 100644 index 0000000..c087aeb --- /dev/null +++ b/apps/with-gaurd-check/src/app/page1.component.ts @@ -0,0 +1,7 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'wgc-page1', + template: `

In Page 1

`, +}) +export class Page1Component {} diff --git a/apps/with-gaurd-check/src/app/page2-child.component.ts b/apps/with-gaurd-check/src/app/page2-child.component.ts new file mode 100644 index 0000000..4ab2e9e --- /dev/null +++ b/apps/with-gaurd-check/src/app/page2-child.component.ts @@ -0,0 +1,7 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'wgc-page2-child', + template: `

In Page 2 - Child

`, +}) +export class Page2ChildComponent {} diff --git a/apps/with-gaurd-check/src/app/page2.component.ts b/apps/with-gaurd-check/src/app/page2.component.ts new file mode 100644 index 0000000..b18984f --- /dev/null +++ b/apps/with-gaurd-check/src/app/page2.component.ts @@ -0,0 +1,7 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'wgc-page2', + template: `

In Page 2

`, +}) +export class Page2Component {} diff --git a/apps/with-gaurd-check/src/assets/.gitkeep b/apps/with-gaurd-check/src/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/apps/with-gaurd-check/src/environments/environment.prod.ts b/apps/with-gaurd-check/src/environments/environment.prod.ts new file mode 100644 index 0000000..c966979 --- /dev/null +++ b/apps/with-gaurd-check/src/environments/environment.prod.ts @@ -0,0 +1,3 @@ +export const environment = { + production: true, +}; diff --git a/apps/with-gaurd-check/src/environments/environment.ts b/apps/with-gaurd-check/src/environments/environment.ts new file mode 100644 index 0000000..66998ae --- /dev/null +++ b/apps/with-gaurd-check/src/environments/environment.ts @@ -0,0 +1,16 @@ +// This file can be replaced during build by using the `fileReplacements` array. +// `ng build` replaces `environment.ts` with `environment.prod.ts`. +// The list of file replacements can be found in `angular.json`. + +export const environment = { + production: false, +}; + +/* + * For easier debugging in development mode, you can import the following file + * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. + * + * This import should be commented out in production mode because it will have a negative impact + * on performance if an error is thrown. + */ +// import 'zone.js/plugins/zone-error'; // Included with Angular CLI. diff --git a/apps/with-gaurd-check/src/favicon.ico b/apps/with-gaurd-check/src/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..317ebcb2336e0833a22dddf0ab287849f26fda57 GIT binary patch literal 15086 zcmeI332;U^%p|z7g|#(P)qFEA@4f!_@qOK2 z_lJl}!lhL!VT_U|uN7%8B2iKH??xhDa;*`g{yjTFWHvXn;2s{4R7kH|pKGdy(7z!K zgftM+Ku7~24TLlh(!g)gz|foI94G^t2^IO$uvX$3(OR0<_5L2sB)lMAMy|+`xodJ{ z_Uh_1m)~h?a;2W{dmhM;u!YGo=)OdmId_B<%^V^{ovI@y`7^g1_V9G}*f# zNzAtvou}I!W1#{M^@ROc(BZ! z+F!!_aR&Px3_reO(EW+TwlW~tv*2zr?iP7(d~a~yA|@*a89IUke+c472NXM0wiX{- zl`UrZC^1XYyf%1u)-Y)jj9;MZ!SLfd2Hl?o|80Su%Z?To_=^g_Jt0oa#CT*tjx>BI z16wec&AOWNK<#i0Qd=1O$fymLRoUR*%;h@*@v7}wApDl^w*h}!sYq%kw+DKDY)@&A z@9$ULEB3qkR#85`lb8#WZw=@})#kQig9oqy^I$dj&k4jU&^2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B-1di>cj(x3Bav`7v z3G7EIbU$z>`Nad7Rk_&OT-W{;qg)-GXV-aJT#(ozdmnA~Rq3GQ_3mby(>q6Ocb-RgTUhTN)))x>m&eD;$J5Bg zo&DhY36Yg=J=$Z>t}RJ>o|@hAcwWzN#r(WJ52^g$lh^!63@hh+dR$&_dEGu&^CR*< z!oFqSqO@>xZ*nC2oiOd0eS*F^IL~W-rsrO`J`ej{=ou_q^_(<$&-3f^J z&L^MSYWIe{&pYq&9eGaArA~*kA + + + + WithGaurdCheck + + + + + + loading... + + diff --git a/apps/with-gaurd-check/src/main.ts b/apps/with-gaurd-check/src/main.ts new file mode 100644 index 0000000..d9a2e7e --- /dev/null +++ b/apps/with-gaurd-check/src/main.ts @@ -0,0 +1,13 @@ +import { enableProdMode } from '@angular/core'; +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; + +import { AppModule } from './app/app.module'; +import { environment } from './environments/environment'; + +if (environment.production) { + enableProdMode(); +} + +platformBrowserDynamic() + .bootstrapModule(AppModule) + .catch((err) => console.error(err)); diff --git a/apps/with-gaurd-check/src/polyfills.ts b/apps/with-gaurd-check/src/polyfills.ts new file mode 100644 index 0000000..e4555ed --- /dev/null +++ b/apps/with-gaurd-check/src/polyfills.ts @@ -0,0 +1,52 @@ +/** + * This file includes polyfills needed by Angular and is loaded before the app. + * You can add your own extra polyfills to this file. + * + * This file is divided into 2 sections: + * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. + * 2. Application imports. Files imported after ZoneJS that should be loaded before your main + * file. + * + * The current setup is for so-called "evergreen" browsers; the last versions of browsers that + * automatically update themselves. This includes recent versions of Safari, Chrome (including + * Opera), Edge on the desktop, and iOS and Chrome on mobile. + * + * Learn more in https://angular.io/guide/browser-support + */ + +/*************************************************************************************************** + * BROWSER POLYFILLS + */ + +/** + * By default, zone.js will patch all possible macroTask and DomEvents + * user can disable parts of macroTask/DomEvents patch by setting following flags + * because those flags need to be set before `zone.js` being loaded, and webpack + * will put import in the top of bundle, so user need to create a separate file + * in this directory (for example: zone-flags.ts), and put the following flags + * into that file, and then add the following code before importing zone.js. + * import './zone-flags'; + * + * The flags allowed in zone-flags.ts are listed here. + * + * The following flags will work for all browsers. + * + * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame + * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick + * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames + * + * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js + * with the following flag, it will bypass `zone.js` patch for IE/Edge + * + * (window as any).__Zone_enable_cross_context_check = true; + * + */ + +/*************************************************************************************************** + * Zone JS is required by default for Angular itself. + */ +import 'zone.js'; // Included with Angular CLI. + +/*************************************************************************************************** + * APPLICATION IMPORTS + */ diff --git a/apps/with-gaurd-check/src/styles.css b/apps/with-gaurd-check/src/styles.css new file mode 100644 index 0000000..90d4ee0 --- /dev/null +++ b/apps/with-gaurd-check/src/styles.css @@ -0,0 +1 @@ +/* You can add global styles to this file, and also import other style files */ diff --git a/apps/with-gaurd-check/src/test-setup.ts b/apps/with-gaurd-check/src/test-setup.ts new file mode 100644 index 0000000..1100b3e --- /dev/null +++ b/apps/with-gaurd-check/src/test-setup.ts @@ -0,0 +1 @@ +import 'jest-preset-angular/setup-jest'; diff --git a/apps/with-gaurd-check/tsconfig.app.json b/apps/with-gaurd-check/tsconfig.app.json new file mode 100644 index 0000000..323b7c4 --- /dev/null +++ b/apps/with-gaurd-check/tsconfig.app.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "types": [] + }, + "files": ["src/main.ts", "src/polyfills.ts"], + "include": ["src/**/*.d.ts"], + "exclude": ["**/*.test.ts", "**/*.spec.ts"] +} diff --git a/apps/with-gaurd-check/tsconfig.editor.json b/apps/with-gaurd-check/tsconfig.editor.json new file mode 100644 index 0000000..20c4afd --- /dev/null +++ b/apps/with-gaurd-check/tsconfig.editor.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "include": ["**/*.ts"], + "compilerOptions": { + "types": ["jest", "node"] + } +} diff --git a/apps/with-gaurd-check/tsconfig.json b/apps/with-gaurd-check/tsconfig.json new file mode 100644 index 0000000..7808c0b --- /dev/null +++ b/apps/with-gaurd-check/tsconfig.json @@ -0,0 +1,27 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.app.json" + }, + { + "path": "./tsconfig.spec.json" + }, + { + "path": "./tsconfig.editor.json" + } + ], + "compilerOptions": { + "forceConsistentCasingInFileNames": true, + "strict": false, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "angularCompilerOptions": { + "strictInjectionParameters": true, + "strictInputAccessModifiers": true, + "strictTemplates": true + } +} diff --git a/apps/with-gaurd-check/tsconfig.spec.json b/apps/with-gaurd-check/tsconfig.spec.json new file mode 100644 index 0000000..e70b1ea --- /dev/null +++ b/apps/with-gaurd-check/tsconfig.spec.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "files": ["src/test-setup.ts"], + "include": ["**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"] +} diff --git a/libs/xng-breadcrumb/src/lib/breadcrumb.component.ts b/libs/xng-breadcrumb/src/lib/breadcrumb.component.ts index 272d349..de29470 100644 --- a/libs/xng-breadcrumb/src/lib/breadcrumb.component.ts +++ b/libs/xng-breadcrumb/src/lib/breadcrumb.component.ts @@ -7,7 +7,7 @@ import { ViewEncapsulation, } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; -import { Observable, Subscription } from 'rxjs'; +import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { BreadcrumbItemDirective } from './breadcrumb-item.directive'; import { BreadcrumbDefinition, BreadcrumbService } from './breadcrumb.service'; @@ -19,7 +19,6 @@ import { BreadcrumbDefinition, BreadcrumbService } from './breadcrumb.service'; encapsulation: ViewEncapsulation.None, }) export class BreadcrumbComponent implements OnInit { - subscription: Subscription; breadcrumbs$: Observable; separatorTemplate: TemplateRef; private _separator = '/'; @@ -92,6 +91,7 @@ export class BreadcrumbComponent implements OnInit { private breadcrumbService: BreadcrumbService, activateRoute: ActivatedRoute ) { + // breadcrumb inside ngIf works only this way activateRoute.params.subscribe((params) => { this.setupComponent(params['someParam']); }); diff --git a/libs/xng-breadcrumb/src/lib/breadcrumb.service.ts b/libs/xng-breadcrumb/src/lib/breadcrumb.service.ts index cf9570b..37d6e64 100644 --- a/libs/xng-breadcrumb/src/lib/breadcrumb.service.ts +++ b/libs/xng-breadcrumb/src/lib/breadcrumb.service.ts @@ -2,15 +2,15 @@ import { Injectable } from '@angular/core'; import { ActivatedRoute, ActivatedRouteSnapshot, + GuardsCheckEnd, Router, - RoutesRecognized, } from '@angular/router'; import { BehaviorSubject } from 'rxjs'; import { filter } from 'rxjs/operators'; import { Breadcrumb } from './types/breadcrumb'; import { - BreadcrumbObject, BreadcrumbFunction, + BreadcrumbObject, } from './types/breadcrumb.config'; type BreadcrumbConfig = BreadcrumbObject | BreadcrumbFunction | string; @@ -69,11 +69,11 @@ export class BreadcrumbService { } this.router.events - .pipe(filter((event) => event instanceof RoutesRecognized)) + .pipe(filter((event) => event instanceof GuardsCheckEnd)) .subscribe((event) => { // activatedRoute doesn't carry data when shouldReuseRoute returns false // use the event data with RoutesRecognized as workaround - if (event instanceof RoutesRecognized) { + if (event instanceof GuardsCheckEnd && event.shouldActivate) { this.setupBreadcrumbs(event.state.root); } }); @@ -87,7 +87,7 @@ export class BreadcrumbService { this.prepareBreadcrumbList(activatedRouteSnapshot, this.baseHref); } - private getRootBreadcrumb() { + private getRootBreadcrumb(): Breadcrumb | void { const rootConfig = this.router.config.find((config) => config.path === ''); const rootBreadcrumb = this.extractObject(rootConfig?.data?.breadcrumb); const storeItem = this.getFromStore(rootBreadcrumb.alias, '/'); @@ -140,7 +140,7 @@ export class BreadcrumbService { private prepareBreadcrumbList( activatedRouteSnapshot: ActivatedRouteSnapshot, routeLinkPrefix: string - ): Breadcrumb[] { + ): Breadcrumb[] | void { if (activatedRouteSnapshot.routeConfig?.path) { const breadcrumbItem = this.prepareBreadcrumbItem( activatedRouteSnapshot,