├── .browserslistrc ├── .editorconfig ├── .gitignore ├── README.md ├── angular.json ├── e2e ├── protractor.conf.js ├── src │ ├── app.e2e-spec.ts │ └── app.po.ts └── tsconfig.json ├── karma.conf.js ├── package-lock.json ├── package.json ├── src ├── app │ ├── Pipes │ │ ├── filter.pipe.ts │ │ └── shorten.pipe.ts │ ├── app-routing.module.ts │ ├── app.component.css │ ├── app.component.html │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── app.module.ts │ ├── auth.module.ts │ ├── auth │ │ ├── auth.component.html │ │ ├── auth.component.ts │ │ └── user.model.ts │ ├── categories │ │ ├── categories.component.css │ │ ├── categories.component.html │ │ ├── categories.component.spec.ts │ │ └── categories.component.ts │ ├── core.module.ts │ ├── edit-user │ │ ├── edit-user.component.css │ │ ├── edit-user.component.html │ │ ├── edit-user.component.spec.ts │ │ └── edit-user.component.ts │ ├── filter-pipes │ │ ├── filter-pipes.component.css │ │ ├── filter-pipes.component.html │ │ ├── filter-pipes.component.spec.ts │ │ └── filter-pipes.component.ts │ ├── filter.module.ts │ ├── home │ │ ├── home.component.css │ │ ├── home.component.html │ │ ├── home.component.spec.ts │ │ └── home.component.ts │ ├── navigation │ │ ├── navigation.component.html │ │ └── navigation.component.ts │ ├── page-not-found │ │ ├── page-not-found.component.css │ │ ├── page-not-found.component.html │ │ ├── page-not-found.component.spec.ts │ │ └── page-not-found.component.ts │ ├── post.module.ts │ ├── posts │ │ ├── Post.model.ts │ │ ├── posts.component.css │ │ ├── posts.component.html │ │ ├── posts.component.spec.ts │ │ └── posts.component.ts │ ├── reactive-forms │ │ ├── reactive-forms.component.css │ │ ├── reactive-forms.component.html │ │ ├── reactive-forms.component.spec.ts │ │ └── reactive-forms.component.ts │ ├── services │ │ ├── auth-interceptor.service.ts │ │ ├── auth-token-interceptor.service.ts │ │ ├── auth.service.ts │ │ ├── dummy.service.ts │ │ ├── guards │ │ │ ├── auth-guard.service.ts │ │ │ ├── auth.guard.ts │ │ │ └── deactivate-guard.service.ts │ │ ├── logging-interceptor.service.ts │ │ ├── post.service.ts │ │ ├── resolvers │ │ │ └── user-resolve.service.ts │ │ └── user.service.ts │ ├── shared.module.ts │ ├── shared │ │ ├── Placeholder.directive.ts │ │ ├── alert-modal │ │ │ ├── alert-modal.component.css │ │ │ ├── alert-modal.component.html │ │ │ └── alert-modal.component.ts │ │ └── loading-spinner │ │ │ ├── loading-spinner.component.css │ │ │ └── loading-spinner.component.ts │ ├── template-form │ │ ├── template-form.component.css │ │ ├── template-form.component.html │ │ ├── template-form.component.spec.ts │ │ └── template-form.component.ts │ ├── user-routing.module.ts │ ├── user.module.ts │ ├── user │ │ ├── user.component.css │ │ ├── user.component.html │ │ ├── user.component.spec.ts │ │ └── user.component.ts │ └── users │ │ ├── users.component.css │ │ ├── users.component.html │ │ ├── users.component.spec.ts │ │ └── users.component.ts ├── assets │ └── .gitkeep ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── main.ts ├── polyfills.ts ├── styles.css └── test.ts ├── tsconfig.app.json ├── tsconfig.base.json ├── tsconfig.json ├── tsconfig.spec.json └── tslint.json /.browserslistrc: -------------------------------------------------------------------------------- 1 | # This file is used by the build system to adjust CSS and JS output to support the specified browsers below. 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | 5 | # For the full list of supported browsers by the Angular framework, please see: 6 | # https://angular.io/guide/browser-support 7 | 8 | # You can see what browsers were selected by your queries by running: 9 | # npx browserslist 10 | 11 | last 1 Chrome version 12 | last 1 Firefox version 13 | last 2 Edge major versions 14 | last 2 Safari major versions 15 | last 2 iOS major versions 16 | Firefox ESR 17 | not IE 9-10 # Angular support for IE 9-10 has been deprecated and will be removed as of Angular v11. To opt-in, remove the 'not' prefix on this line. 18 | not IE 11 # Angular supports IE 11 only as an opt-in. To opt-in, remove the 'not' prefix on this line. 19 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.ts] 12 | quote_type = single 13 | 14 | [*.md] 15 | max_line_length = off 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # dependencies 11 | /node_modules 12 | 13 | # profiling files 14 | chrome-profiler-events*.json 15 | speed-measure-plugin*.json 16 | 17 | # IDEs and editors 18 | /.idea 19 | .project 20 | .classpath 21 | .c9/ 22 | *.launch 23 | .settings/ 24 | *.sublime-workspace 25 | 26 | # IDE - VSCode 27 | .vscode/* 28 | !.vscode/settings.json 29 | !.vscode/tasks.json 30 | !.vscode/launch.json 31 | !.vscode/extensions.json 32 | .history/* 33 | 34 | # misc 35 | /.sass-cache 36 | /connect.lock 37 | /coverage 38 | /libpeerconnection.log 39 | npm-debug.log 40 | yarn-error.log 41 | testem.log 42 | /typings 43 | 44 | # System Files 45 | .DS_Store 46 | Thumbs.db 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angularrouting 2 | 3 | This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 10.0.6. 4 | 5 | ## Development server 6 | 7 | Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. 8 | 9 | ## Code scaffolding 10 | 11 | Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. 12 | 13 | ## Build 14 | 15 | Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. 16 | 17 | ## Running unit tests 18 | 19 | Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). 20 | 21 | ## Running end-to-end tests 22 | 23 | Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/). 24 | 25 | ## Further help 26 | 27 | To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). 28 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "angularrouting": { 7 | "projectType": "application", 8 | "schematics": {}, 9 | "root": "", 10 | "sourceRoot": "src", 11 | "prefix": "app", 12 | "architect": { 13 | "build": { 14 | "builder": "@angular-devkit/build-angular:browser", 15 | "options": { 16 | "outputPath": "dist/angularrouting", 17 | "index": "src/index.html", 18 | "main": "src/main.ts", 19 | "polyfills": "src/polyfills.ts", 20 | "tsConfig": "tsconfig.app.json", 21 | "aot": true, 22 | "assets": ["src/favicon.ico", "src/assets"], 23 | "styles": [ 24 | "node_modules/bootstrap/dist/css/bootstrap.min.css", 25 | "src/styles.css" 26 | ], 27 | "scripts": [] 28 | }, 29 | "configurations": { 30 | "production": { 31 | "fileReplacements": [ 32 | { 33 | "replace": "src/environments/environment.ts", 34 | "with": "src/environments/environment.prod.ts" 35 | } 36 | ], 37 | "optimization": true, 38 | "outputHashing": "all", 39 | "sourceMap": false, 40 | "extractCss": true, 41 | "namedChunks": false, 42 | "extractLicenses": true, 43 | "vendorChunk": false, 44 | "buildOptimizer": true, 45 | "budgets": [ 46 | { 47 | "type": "initial", 48 | "maximumWarning": "2mb", 49 | "maximumError": "5mb" 50 | }, 51 | { 52 | "type": "anyComponentStyle", 53 | "maximumWarning": "6kb", 54 | "maximumError": "10kb" 55 | } 56 | ] 57 | } 58 | } 59 | }, 60 | "serve": { 61 | "builder": "@angular-devkit/build-angular:dev-server", 62 | "options": { 63 | "browserTarget": "angularrouting:build" 64 | }, 65 | "configurations": { 66 | "production": { 67 | "browserTarget": "angularrouting:build:production" 68 | } 69 | } 70 | }, 71 | "extract-i18n": { 72 | "builder": "@angular-devkit/build-angular:extract-i18n", 73 | "options": { 74 | "browserTarget": "angularrouting:build" 75 | } 76 | }, 77 | "test": { 78 | "builder": "@angular-devkit/build-angular:karma", 79 | "options": { 80 | "main": "src/test.ts", 81 | "polyfills": "src/polyfills.ts", 82 | "tsConfig": "tsconfig.spec.json", 83 | "karmaConfig": "karma.conf.js", 84 | "assets": ["src/favicon.ico", "src/assets"], 85 | "styles": ["src/styles.css"], 86 | "scripts": [] 87 | } 88 | }, 89 | "lint": { 90 | "builder": "@angular-devkit/build-angular:tslint", 91 | "options": { 92 | "tsConfig": [ 93 | "tsconfig.app.json", 94 | "tsconfig.spec.json", 95 | "e2e/tsconfig.json" 96 | ], 97 | "exclude": ["**/node_modules/**"] 98 | } 99 | }, 100 | "e2e": { 101 | "builder": "@angular-devkit/build-angular:protractor", 102 | "options": { 103 | "protractorConfig": "e2e/protractor.conf.js", 104 | "devServerTarget": "angularrouting:serve" 105 | }, 106 | "configurations": { 107 | "production": { 108 | "devServerTarget": "angularrouting:serve:production" 109 | } 110 | } 111 | } 112 | } 113 | } 114 | }, 115 | "defaultProject": "angularrouting", 116 | "cli": { 117 | "analytics": false 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | // Protractor configuration file, see link for more information 3 | // https://github.com/angular/protractor/blob/master/lib/config.ts 4 | 5 | const { SpecReporter, StacktraceOption } = require('jasmine-spec-reporter'); 6 | 7 | /** 8 | * @type { import("protractor").Config } 9 | */ 10 | exports.config = { 11 | allScriptsTimeout: 11000, 12 | specs: [ 13 | './src/**/*.e2e-spec.ts' 14 | ], 15 | capabilities: { 16 | browserName: 'chrome' 17 | }, 18 | directConnect: true, 19 | baseUrl: 'http://localhost:4200/', 20 | framework: 'jasmine', 21 | jasmineNodeOpts: { 22 | showColors: true, 23 | defaultTimeoutInterval: 30000, 24 | print: function() {} 25 | }, 26 | onPrepare() { 27 | require('ts-node').register({ 28 | project: require('path').join(__dirname, './tsconfig.json') 29 | }); 30 | jasmine.getEnv().addReporter(new SpecReporter({ 31 | spec: { 32 | displayStacktrace: StacktraceOption.PRETTY 33 | } 34 | })); 35 | } 36 | }; -------------------------------------------------------------------------------- /e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | import { browser, logging } from 'protractor'; 3 | 4 | describe('workspace-project App', () => { 5 | let page: AppPage; 6 | 7 | beforeEach(() => { 8 | page = new AppPage(); 9 | }); 10 | 11 | it('should display welcome message', () => { 12 | page.navigateTo(); 13 | expect(page.getTitleText()).toEqual('angularrouting app is running!'); 14 | }); 15 | 16 | afterEach(async () => { 17 | // Assert that there are no errors emitted from the browser 18 | const logs = await browser.manage().logs().get(logging.Type.BROWSER); 19 | expect(logs).not.toContain(jasmine.objectContaining({ 20 | level: logging.Level.SEVERE, 21 | } as logging.Entry)); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo(): Promise { 5 | return browser.get(browser.baseUrl) as Promise; 6 | } 7 | 8 | getTitleText(): Promise { 9 | return element(by.css('app-root .content span')).getText() as Promise; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /e2e/tsconfig.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "../tsconfig.base.json", 4 | "compilerOptions": { 5 | "outDir": "../out-tsc/e2e", 6 | "module": "commonjs", 7 | "target": "es2018", 8 | "types": [ 9 | "jasmine", 10 | "jasminewd2", 11 | "node" 12 | ] 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, './coverage/angularrouting'), 20 | reports: ['html', 'lcovonly', 'text-summary'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false, 30 | restartOnFileChange: true 31 | }); 32 | }; 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angularrouting", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e" 11 | }, 12 | "private": true, 13 | "dependencies": { 14 | "@angular/animations": "~10.0.9", 15 | "@angular/common": "~10.0.9", 16 | "@angular/compiler": "~10.0.9", 17 | "@angular/core": "~10.0.9", 18 | "@angular/forms": "~10.0.9", 19 | "@angular/platform-browser": "~10.0.9", 20 | "@angular/platform-browser-dynamic": "~10.0.9", 21 | "@angular/router": "~10.0.9", 22 | "bootstrap": "^4.5.2", 23 | "rxjs": "~6.5.5", 24 | "tslib": "^2.0.0", 25 | "zone.js": "~0.10.3" 26 | }, 27 | "devDependencies": { 28 | "@angular-devkit/build-angular": "~0.1000.6", 29 | "@angular/cli": "~10.0.6", 30 | "@angular/compiler-cli": "~10.0.9", 31 | "@types/node": "^12.11.1", 32 | "@types/jasmine": "~3.5.0", 33 | "@types/jasminewd2": "~2.0.3", 34 | "codelyzer": "^6.0.0", 35 | "jasmine-core": "~3.5.0", 36 | "jasmine-spec-reporter": "~5.0.0", 37 | "karma": "~5.0.0", 38 | "karma-chrome-launcher": "~3.1.0", 39 | "karma-coverage-istanbul-reporter": "~3.0.2", 40 | "karma-jasmine": "~3.3.0", 41 | "karma-jasmine-html-reporter": "^1.5.0", 42 | "protractor": "~7.0.0", 43 | "ts-node": "~8.3.0", 44 | "tslint": "~6.1.0", 45 | "typescript": "~3.9.5" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/app/Pipes/filter.pipe.ts: -------------------------------------------------------------------------------- 1 | import { Pipe, PipeTransform } from '@angular/core'; 2 | 3 | @Pipe({ 4 | name: 'filter', 5 | pure: false 6 | }) 7 | export class FilterPipe implements PipeTransform { 8 | 9 | transform(value: any, filterString: string) { 10 | if (value.length === 0 || filterString === '') { 11 | return value; 12 | } 13 | 14 | const users = []; 15 | for (const user of value) { 16 | if (user['name'] === filterString) { 17 | users.push(user); 18 | } 19 | } 20 | return users; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/app/Pipes/shorten.pipe.ts: -------------------------------------------------------------------------------- 1 | import { Pipe, PipeTransform } from '@angular/core'; 2 | 3 | @Pipe({ 4 | name: 'shorten' 5 | }) 6 | export class ShortenPipe implements PipeTransform { 7 | transform(value: any, limit: number) { 8 | if (value.length > limit) { 9 | return value.substr(0, limit) + ' ...'; 10 | } 11 | return value; 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /src/app/app-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { PreloadAllModules, RouterModule, Routes } from '@angular/router'; 3 | 4 | import { HomeComponent } from './home/home.component'; 5 | import { CategoriesComponent } from './categories/categories.component'; 6 | import { PageNotFoundComponent } from './page-not-found/page-not-found.component'; 7 | import { TemplateFormComponent } from './template-form/template-form.component'; 8 | import { ReactiveFormsComponent } from './reactive-forms/reactive-forms.component'; 9 | 10 | const appRoutes: Routes = [ 11 | { path: '', component: HomeComponent, data: { page: 1, search: 'Leela' } }, 12 | {path: 'users', loadChildren: () => import ('./user.module').then(m => m.UserModule)}, 13 | {path: 'posts', loadChildren: () => import ('./post.module').then(m => m.PostModule)}, 14 | { path: 'categories', component: CategoriesComponent }, 15 | { path: 'templateform', component: TemplateFormComponent }, 16 | { path: 'reactiveform', component: ReactiveFormsComponent }, 17 | { path: 'not-found', component: PageNotFoundComponent }, 18 | { path: '**', redirectTo: 'not-found' }, 19 | ]; 20 | 21 | @NgModule({ 22 | imports: [RouterModule.forRoot(appRoutes, {preloadingStrategy: PreloadAllModules})], 23 | exports: [RouterModule], 24 | }) 25 | export class AppRoutingModule {} 26 | -------------------------------------------------------------------------------- /src/app/app.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leelanarasimha/angularcourse/54d65e7dc8ba5fe38706c13e9d9d0f2678e1354b/src/app/app.component.css -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 | 5 |
6 | 7 |
8 |
9 |
10 |
User added button clicked
11 | 12 |
13 |
14 |
15 |
16 |
17 | -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, async } from '@angular/core/testing'; 2 | import { AppComponent } from './app.component'; 3 | 4 | describe('AppComponent', () => { 5 | beforeEach(async(() => { 6 | TestBed.configureTestingModule({ 7 | declarations: [ 8 | AppComponent 9 | ], 10 | }).compileComponents(); 11 | })); 12 | 13 | it('should create the app', () => { 14 | const fixture = TestBed.createComponent(AppComponent); 15 | const app = fixture.componentInstance; 16 | expect(app).toBeTruthy(); 17 | }); 18 | 19 | it(`should have as title 'angularrouting'`, () => { 20 | const fixture = TestBed.createComponent(AppComponent); 21 | const app = fixture.componentInstance; 22 | expect(app.title).toEqual('angularrouting'); 23 | }); 24 | 25 | it('should render title', () => { 26 | const fixture = TestBed.createComponent(AppComponent); 27 | fixture.detectChanges(); 28 | const compiled = fixture.nativeElement; 29 | expect(compiled.querySelector('.content span').textContent).toContain('angularrouting app is running!'); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { DummyService } from './services/dummy.service'; 2 | import { Component, OnDestroy, OnInit } from '@angular/core'; 3 | import { Subscription } from 'rxjs'; 4 | import { AuthService } from './services/auth.service'; 5 | import { UserService } from './services/user.service'; 6 | 7 | @Component({ 8 | selector: 'app-root', 9 | templateUrl: './app.component.html', 10 | styleUrls: ['./app.component.css'], 11 | }) 12 | export class AppComponent implements OnInit, OnDestroy { 13 | title = 'angularrouting'; 14 | userAdded = false; 15 | userAddedSubscription: Subscription; 16 | constructor( 17 | private authService: AuthService, 18 | private userService: UserService, 19 | private DummyService: DummyService 20 | ) {} 21 | 22 | ngOnInit() { 23 | this.authService.autoLogin(); 24 | this.DummyService.printLog('Hello from App Component'); 25 | } 26 | 27 | onLoginClick() {} 28 | 29 | onLogoutClick() { 30 | this.authService.logout(); 31 | } 32 | 33 | ngOnDestroy() { 34 | this.userAddedSubscription.unsubscribe(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { DummyService } from './services/dummy.service'; 2 | import { SharedModule } from './shared.module'; 3 | import { CoreModule } from './core.module'; 4 | import { FilterModule } from './filter.module'; 5 | import { AuthModule } from './auth.module'; 6 | import { PostModule } from './post.module'; 7 | import { UserModule } from './user.module'; 8 | import { PlaceholderDirective } from './shared/Placeholder.directive'; 9 | import { NavigationComponent } from './navigation/navigation.component'; 10 | import { LoadingSpinnerComponent } from './shared/loading-spinner/loading-spinner.component'; 11 | 12 | import { BrowserModule } from '@angular/platform-browser'; 13 | import { NgModule } from '@angular/core'; 14 | import { AppRoutingModule } from './app-routing.module'; 15 | import { HttpClientModule } from '@angular/common/http'; 16 | 17 | import { AppComponent } from './app.component'; 18 | import { HomeComponent } from './home/home.component'; 19 | import { CategoriesComponent } from './categories/categories.component'; 20 | import { PageNotFoundComponent } from './page-not-found/page-not-found.component'; 21 | import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 22 | import { TemplateFormComponent } from './template-form/template-form.component'; 23 | import { ReactiveFormsComponent } from './reactive-forms/reactive-forms.component'; 24 | 25 | @NgModule({ 26 | declarations: [ 27 | AppComponent, 28 | HomeComponent, 29 | CategoriesComponent, 30 | PageNotFoundComponent, 31 | TemplateFormComponent, 32 | ReactiveFormsComponent, 33 | LoadingSpinnerComponent, 34 | NavigationComponent, 35 | PlaceholderDirective 36 | ], 37 | imports: [ 38 | BrowserModule, 39 | CoreModule, 40 | AuthModule, 41 | FilterModule, 42 | AppRoutingModule, 43 | FormsModule, 44 | ReactiveFormsModule, 45 | HttpClientModule, 46 | SharedModule 47 | ], 48 | 49 | 50 | bootstrap: [AppComponent], 51 | }) 52 | export class AppModule {} 53 | -------------------------------------------------------------------------------- /src/app/auth.module.ts: -------------------------------------------------------------------------------- 1 | import { RouterModule } from '@angular/router'; 2 | import { AuthComponent } from './auth/auth.component'; 3 | import { NgModule } from '@angular/core'; 4 | import { CommonModule } from '@angular/common'; 5 | import { FormsModule } from '@angular/forms'; 6 | 7 | 8 | @NgModule({ 9 | declarations: [ 10 | AuthComponent, 11 | ], 12 | imports: [ 13 | CommonModule, 14 | FormsModule, 15 | RouterModule.forChild([ 16 | { path: 'auth', component: AuthComponent }, 17 | ]) 18 | ] 19 | }) 20 | export class AuthModule { 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/app/auth/auth.component.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |
5 | 8 | 9 | 10 |
11 | 12 |
13 |
18 |
19 | 20 | 29 |
30 |
Email is required
31 |
Invalid Email
32 |
33 |
34 |
35 | 36 | 45 |
46 |
{{ getPasswordErrors(password) }}
47 |
48 |
49 | 50 |
51 | 58 | | 59 | 62 |
63 |
64 |
65 |
66 | -------------------------------------------------------------------------------- /src/app/auth/auth.component.ts: -------------------------------------------------------------------------------- 1 | import { PlaceholderDirective } from './../shared/Placeholder.directive'; 2 | import { AlertModalComponent } from './../shared/alert-modal/alert-modal.component'; 3 | import { AuthResponseData } from './../services/auth.service'; 4 | import { Observable, Subscription } from 'rxjs'; 5 | import { FormControl, NgForm } from '@angular/forms'; 6 | import { Component, ComponentFactoryResolver, ViewChild, OnDestroy } from '@angular/core'; 7 | import { AuthService } from '../services/auth.service'; 8 | import { Router } from '@angular/router'; 9 | 10 | @Component({ 11 | selector: 'app-auth', 12 | templateUrl: './auth.component.html', 13 | }) 14 | export class AuthComponent implements OnDestroy { 15 | isLoginMode = true; 16 | isLoading = false; 17 | error: string = null; 18 | closeSub: Subscription; 19 | @ViewChild(PlaceholderDirective) alertHost: PlaceholderDirective; 20 | 21 | constructor(private authService: AuthService, private router: Router, 22 | private ComponentFactoryResolver: ComponentFactoryResolver) {} 23 | 24 | onSwitchMode() { 25 | this.isLoginMode = !this.isLoginMode; 26 | } 27 | 28 | onFormSubmit(authForm: NgForm) { 29 | if (!authForm.valid) { 30 | return; 31 | } 32 | 33 | this.isLoading = true; 34 | this.error = null; 35 | 36 | let authObs: Observable; 37 | 38 | if (this.isLoginMode) { 39 | authObs = this.authService.login( 40 | authForm.value.email, 41 | authForm.value.password 42 | ); 43 | } else { 44 | authObs = this.authService.signUp( 45 | authForm.value.email, 46 | authForm.value.password 47 | ); 48 | } 49 | 50 | authObs.subscribe( 51 | (response) => { 52 | console.log(response); 53 | this.isLoading = false; 54 | this.router.navigate(['/']); 55 | }, 56 | (errorMessage) => { 57 | this.error = errorMessage; 58 | this.showErrorAlert(errorMessage); 59 | this.isLoading = false; 60 | } 61 | ); 62 | } 63 | 64 | showErrorAlert(message: string) { 65 | const componentFactory = this.ComponentFactoryResolver.resolveComponentFactory(AlertModalComponent); 66 | this.alertHost.ViewContainerRef.clear(); 67 | const componentRef = this.alertHost.ViewContainerRef.createComponent(componentFactory); 68 | componentRef.instance.error = message; 69 | this.closeSub = componentRef.instance.close.subscribe(() => { 70 | this.closeSub.unsubscribe(); 71 | this.alertHost.ViewContainerRef.clear(); 72 | 73 | }) 74 | } 75 | 76 | ngOnDestroy() { 77 | if (this.closeSub) { 78 | this.closeSub.unsubscribe(); 79 | } 80 | } 81 | 82 | getPasswordErrors(password: FormControl) { 83 | if (password.errors.required) { 84 | return 'Password Required'; 85 | } 86 | if (password.errors.minlength) { 87 | return 'password is of 6 characters'; 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/app/auth/user.model.ts: -------------------------------------------------------------------------------- 1 | export class User { 2 | constructor( 3 | public email: string, 4 | public localId: string, 5 | private _token: string, 6 | private expirationDate: Date 7 | ) {} 8 | 9 | get token() { 10 | if (new Date() > this.expirationDate) { 11 | return null; 12 | } 13 | return this._token; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/app/categories/categories.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leelanarasimha/angularcourse/54d65e7dc8ba5fe38706c13e9d9d0f2678e1354b/src/app/categories/categories.component.css -------------------------------------------------------------------------------- /src/app/categories/categories.component.html: -------------------------------------------------------------------------------- 1 |

categories works!

2 | -------------------------------------------------------------------------------- /src/app/categories/categories.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { CategoriesComponent } from './categories.component'; 4 | 5 | describe('CategoriesComponent', () => { 6 | let component: CategoriesComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ CategoriesComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(CategoriesComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/categories/categories.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-categories', 5 | templateUrl: './categories.component.html', 6 | styleUrls: ['./categories.component.css'] 7 | }) 8 | export class CategoriesComponent implements OnInit { 9 | 10 | constructor() { } 11 | 12 | ngOnInit(): void { 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/app/core.module.ts: -------------------------------------------------------------------------------- 1 | import { DummyService } from './services/dummy.service'; 2 | import { UserResolveService } from './services/resolvers/user-resolve.service'; 3 | import { UserService } from './services/user.service'; 4 | import { DeactivateGuardService } from './services/guards/deactivate-guard.service'; 5 | import { AuthGuardService } from './services/guards/auth-guard.service'; 6 | import { AuthTokenInterceptorService } from './services/auth-token-interceptor.service'; 7 | import { LoggingInterceptorService } from './services/logging-interceptor.service'; 8 | import { AuthInterceptorService } from './services/auth-interceptor.service'; 9 | import { HTTP_INTERCEPTORS } from '@angular/common/http'; 10 | import { NgModule } from '@angular/core'; 11 | 12 | @NgModule({ 13 | providers: [{ 14 | provide: HTTP_INTERCEPTORS, 15 | useClass: AuthInterceptorService, 16 | multi: true, 17 | }, 18 | { 19 | provide: HTTP_INTERCEPTORS, 20 | useClass: LoggingInterceptorService, 21 | multi: true, 22 | }, 23 | { 24 | provide: HTTP_INTERCEPTORS, 25 | useClass: AuthTokenInterceptorService, 26 | multi: true, 27 | }, 28 | AuthGuardService, 29 | DeactivateGuardService, 30 | UserService, 31 | UserResolveService] 32 | }) 33 | export class CoreModule { 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/app/edit-user/edit-user.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leelanarasimha/angularcourse/54d65e7dc8ba5fe38706c13e9d9d0f2678e1354b/src/app/edit-user/edit-user.component.css -------------------------------------------------------------------------------- /src/app/edit-user/edit-user.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 | 5 |
6 |
7 |
8 | 9 | 14 |
15 |
16 |
17 | -------------------------------------------------------------------------------- /src/app/edit-user/edit-user.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { EditUserComponent } from './edit-user.component'; 4 | 5 | describe('EditUserComponent', () => { 6 | let component: EditUserComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ EditUserComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(EditUserComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/edit-user/edit-user.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { ActivatedRoute, Params } from '@angular/router'; 3 | 4 | import { IDeactivateGuard } from './../services/guards/deactivate-guard.service'; 5 | 6 | @Component({ 7 | selector: 'app-edit-user', 8 | templateUrl: './edit-user.component.html', 9 | styleUrls: ['./edit-user.component.css'], 10 | }) 11 | export class EditUserComponent implements OnInit, IDeactivateGuard { 12 | user: { id: string; name: string }; 13 | editUserDetails: { id: string; name: string }; 14 | constructor(private route: ActivatedRoute) { } 15 | 16 | ngOnInit(): void { 17 | 18 | this.route.data.subscribe(data => { 19 | console.log(data); 20 | this.user = { 21 | id: data['user']['id'], 22 | name: data['user']['name'], 23 | }; 24 | this.editUserDetails = { ...this.user }; 25 | }) 26 | 27 | } 28 | 29 | canExit() { 30 | console.log(this.user); 31 | console.log(this.editUserDetails); 32 | 33 | if ( 34 | this.editUserDetails.id !== this.user.id || 35 | this.editUserDetails.name !== this.user.name 36 | ) { 37 | if (confirm('All changes will be lost if you move to another page')) { 38 | return true; 39 | } else { 40 | return false; 41 | } 42 | } 43 | 44 | return true; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/app/filter-pipes/filter-pipes.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leelanarasimha/angularcourse/54d65e7dc8ba5fe38706c13e9d9d0f2678e1354b/src/app/filter-pipes/filter-pipes.component.css -------------------------------------------------------------------------------- /src/app/filter-pipes/filter-pipes.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 | 5 |
6 |
7 | 8 |
9 | {{appStatus | async}} 10 |
11 |
12 |
13 | 14 |
15 |
16 |
17 |
18 |
19 |
Name: {{user.name | shorten:10}}
20 |
Joined Date: {{user.joinedDate | date:'fullDate' | uppercase}}
21 |
22 |
23 |
24 |
-------------------------------------------------------------------------------- /src/app/filter-pipes/filter-pipes.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { FilterPipesComponent } from './filter-pipes.component'; 4 | 5 | describe('FilterPipesComponent', () => { 6 | let component: FilterPipesComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ FilterPipesComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(FilterPipesComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/filter-pipes/filter-pipes.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-filter-pipes', 5 | templateUrl: './filter-pipes.component.html', 6 | styleUrls: ['./filter-pipes.component.css'] 7 | }) 8 | export class FilterPipesComponent implements OnInit { 9 | 10 | appStatus = new Promise((resolve, reject) => { 11 | setTimeout(() => { 12 | resolve('Users Data Received'); 13 | }, 3000); 14 | }) 15 | filteredString: string = ''; 16 | users = [{ 17 | name: 'Leela', 18 | joinedDate: new Date(15, 2, 2016) 19 | }, 20 | { 21 | name: 'Rama', 22 | joinedDate: new Date(17, 3, 2019) 23 | }, 24 | { 25 | name: 'Krishna', 26 | joinedDate: new Date(20, 4, 2019) 27 | }, 28 | ]; 29 | 30 | constructor() { } 31 | 32 | ngOnInit(): void { 33 | } 34 | 35 | onAddUser() { 36 | this.users.push({ 37 | name: 'Sample', 38 | joinedDate: new Date(12, 2, 2009) 39 | }) 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/app/filter.module.ts: -------------------------------------------------------------------------------- 1 | import { SharedModule } from './shared.module'; 2 | import { RouterModule } from '@angular/router'; 3 | import { FilterPipesComponent } from './filter-pipes/filter-pipes.component'; 4 | import { NgModule } from '@angular/core'; 5 | 6 | @NgModule({ 7 | declarations: [ 8 | FilterPipesComponent, 9 | ], 10 | imports : [ 11 | SharedModule, 12 | RouterModule.forChild([ 13 | { path: 'filterpipes', component: FilterPipesComponent }, 14 | ]) 15 | ] 16 | }) 17 | export class FilterModule { 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/app/home/home.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leelanarasimha/angularcourse/54d65e7dc8ba5fe38706c13e9d9d0f2678e1354b/src/app/home/home.component.css -------------------------------------------------------------------------------- /src/app/home/home.component.html: -------------------------------------------------------------------------------- 1 |

home works!

2 | -------------------------------------------------------------------------------- /src/app/home/home.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { HomeComponent } from './home.component'; 4 | 5 | describe('HomeComponent', () => { 6 | let component: HomeComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ HomeComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(HomeComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/home/home.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnDestroy, OnInit } from '@angular/core'; 2 | import { ActivatedRoute, Data } from '@angular/router'; 3 | import { interval, Observable, Subscription } from 'rxjs'; 4 | import { map } from 'rxjs/operators'; 5 | 6 | @Component({ 7 | selector: 'app-home', 8 | templateUrl: './home.component.html', 9 | styleUrls: ['./home.component.css'], 10 | }) 11 | export class HomeComponent implements OnInit, OnDestroy { 12 | intervalSubscription: Subscription; 13 | routeSubscription: Subscription; 14 | constructor(private route: ActivatedRoute) { } 15 | 16 | ngOnInit(): void { 17 | this.routeSubscription = this.route.data.subscribe((data: Data) => { 18 | console.log(data); 19 | }, error => { 20 | console.log(error); 21 | }); 22 | 23 | let customObservable = Observable.create(observer => { 24 | let count = 0; 25 | setInterval(() => { 26 | observer.next(count); 27 | if (count > 3) { 28 | observer.error('count is greater than 3') 29 | } 30 | 31 | if (count > 2) { 32 | observer.complete(); 33 | } 34 | count++; 35 | }, 1000); 36 | }); 37 | 38 | this.intervalSubscription = customObservable.pipe(map((data: number) => { 39 | 40 | return 'count is ' + (data + 1); 41 | })).subscribe(data => { 42 | console.log(data); 43 | }, error => { 44 | console.log(error); 45 | }, () => { 46 | console.log('complete'); 47 | }) 48 | } 49 | 50 | ngOnDestroy() { 51 | this.intervalSubscription.unsubscribe(); 52 | this.routeSubscription.unsubscribe(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/app/navigation/navigation.component.html: -------------------------------------------------------------------------------- 1 | 67 | -------------------------------------------------------------------------------- /src/app/navigation/navigation.component.ts: -------------------------------------------------------------------------------- 1 | import { AuthService } from './../services/auth.service'; 2 | import { Component, OnInit } from '@angular/core'; 3 | 4 | @Component({ 5 | selector: 'app-navigation', 6 | templateUrl: './navigation.component.html', 7 | }) 8 | export class NavigationComponent implements OnInit { 9 | isAuthenticated = false; 10 | constructor(private authService: AuthService) {} 11 | 12 | ngOnInit() { 13 | this.authService.userSub.subscribe((user) => { 14 | this.isAuthenticated = user ? true : false; 15 | }); 16 | } 17 | 18 | onLogout(event: Event) { 19 | event.preventDefault(); 20 | this.authService.logout(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/app/page-not-found/page-not-found.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leelanarasimha/angularcourse/54d65e7dc8ba5fe38706c13e9d9d0f2678e1354b/src/app/page-not-found/page-not-found.component.css -------------------------------------------------------------------------------- /src/app/page-not-found/page-not-found.component.html: -------------------------------------------------------------------------------- 1 |
Page Not Found
2 | -------------------------------------------------------------------------------- /src/app/page-not-found/page-not-found.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { PageNotFoundComponent } from './page-not-found.component'; 4 | 5 | describe('PageNotFoundComponent', () => { 6 | let component: PageNotFoundComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ PageNotFoundComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(PageNotFoundComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/page-not-found/page-not-found.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-page-not-found', 5 | templateUrl: './page-not-found.component.html', 6 | styleUrls: ['./page-not-found.component.css'] 7 | }) 8 | export class PageNotFoundComponent implements OnInit { 9 | 10 | constructor() { } 11 | 12 | ngOnInit(): void { 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/app/post.module.ts: -------------------------------------------------------------------------------- 1 | import { ReactiveFormsModule } from '@angular/forms'; 2 | import { AuthGuard } from './services/guards/auth.guard'; 3 | import { RouterModule } from '@angular/router'; 4 | import { PostsComponent } from './posts/posts.component'; 5 | import { NgModule } from '@angular/core'; 6 | import { CommonModule } from '@angular/common'; 7 | 8 | @NgModule({ 9 | declarations: [ 10 | PostsComponent, 11 | ], 12 | imports: [ 13 | CommonModule, 14 | ReactiveFormsModule, 15 | RouterModule.forChild([ 16 | { path: '', component: PostsComponent, canActivate: [AuthGuard] }, 17 | ]) 18 | ] 19 | }) 20 | export class PostModule { 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/app/posts/Post.model.ts: -------------------------------------------------------------------------------- 1 | export interface Post { 2 | title: string; 3 | content: string; 4 | key?: string; 5 | } 6 | -------------------------------------------------------------------------------- /src/app/posts/posts.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leelanarasimha/angularcourse/54d65e7dc8ba5fe38706c13e9d9d0f2678e1354b/src/app/posts/posts.component.css -------------------------------------------------------------------------------- /src/app/posts/posts.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |
5 | 6 | 7 | 8 |
12 | Title is Required 13 |
14 |
15 |
16 | 17 | 18 |
24 | Content is Required 25 |
26 |
27 | 28 |
29 | 30 |
31 |
32 |
33 | 34 |
35 | Clear Posts 36 |
37 | 38 |
39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 |
TitleContent
{{ post.title }}{{ post.content }}
51 |
52 |
{{ error }}
53 |
54 |
55 |
56 | -------------------------------------------------------------------------------- /src/app/posts/posts.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { PostsComponent } from './posts.component'; 4 | 5 | describe('PostsComponent', () => { 6 | let component: PostsComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ PostsComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(PostsComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/posts/posts.component.ts: -------------------------------------------------------------------------------- 1 | import { HttpClient } from '@angular/common/http'; 2 | import { Component, OnInit } from '@angular/core'; 3 | import { FormControl, FormGroup, Validators } from '@angular/forms'; 4 | import { PostService } from '../services/post.service'; 5 | import { Post } from './Post.model'; 6 | @Component({ 7 | selector: 'app-posts', 8 | templateUrl: './posts.component.html', 9 | styleUrls: ['./posts.component.css'], 10 | }) 11 | export class PostsComponent implements OnInit { 12 | postForm: FormGroup; 13 | posts: Post[]; 14 | error = null; 15 | 16 | constructor(private postService: PostService) {} 17 | 18 | ngOnInit(): void { 19 | this.postForm = new FormGroup({ 20 | title: new FormControl(null, Validators.required), 21 | content: new FormControl(null, Validators.required), 22 | }); 23 | this.getPosts(); 24 | } 25 | 26 | getPosts() { 27 | this.postService.fetchPosts().subscribe( 28 | (response) => { 29 | this.posts = response; 30 | }, 31 | (error) => { 32 | console.log(error); 33 | this.error = error.message; 34 | } 35 | ); 36 | } 37 | 38 | onCreatePost() { 39 | const postData: Post = this.postForm.value; 40 | this.postService.createPost(postData).subscribe((response) => { 41 | console.log(response); 42 | this.getPosts(); 43 | }); 44 | } 45 | 46 | onClearPosts(event: Event) { 47 | event.preventDefault(); 48 | this.postService.clearPosts(); 49 | this.posts = []; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/app/reactive-forms/reactive-forms.component.css: -------------------------------------------------------------------------------- 1 | input.ng-invalid.ng-touched { 2 | border: 1px solid red; 3 | } -------------------------------------------------------------------------------- /src/app/reactive-forms/reactive-forms.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |
5 |
6 |
7 | 8 | 9 | 11 | Username is 12 | required 13 | Username is not 14 | valid 15 | 16 |
17 |
18 | 19 | 20 | Please 22 | enter valid 23 | Email 24 |
25 |
26 | 27 |
28 | 31 |
32 | 33 |
34 |
35 | 36 |
37 | 38 |
39 | 40 |
41 |
42 | 43 |
44 | 45 |
46 | 47 | 48 |
49 |
50 |
51 |
-------------------------------------------------------------------------------- /src/app/reactive-forms/reactive-forms.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { ReactiveFormsComponent } from './reactive-forms.component'; 4 | 5 | describe('ReactiveFormsComponent', () => { 6 | let component: ReactiveFormsComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ ReactiveFormsComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(ReactiveFormsComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/reactive-forms/reactive-forms.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { FormArray, FormControl, FormGroup, Validators } from '@angular/forms'; 3 | import { Observable } from 'rxjs'; 4 | 5 | @Component({ 6 | selector: 'app-reactive-forms', 7 | 8 | templateUrl: './reactive-forms.component.html', 9 | styleUrls: ['./reactive-forms.component.css'] 10 | }) 11 | export class ReactiveFormsComponent implements OnInit { 12 | genders = ['male', 'female']; 13 | restrictedNames = ['Leela']; 14 | signUpForm: FormGroup; 15 | constructor() { } 16 | 17 | get hobbyControls() { 18 | return (this.signUpForm.get('hobbies')).controls; 19 | } 20 | 21 | ngOnInit(): void { 22 | this.signUpForm = new FormGroup({ 23 | 'userData': new FormGroup({ 24 | 'username': new FormControl(null, [Validators.required, this.isRestrictedNames.bind(this)]), 25 | 'email': new FormControl(null, [Validators.required, Validators.email], [this.isRestrictedEmails]), 26 | }), 27 | 28 | 'gender': new FormControl('female'), 29 | 'hobbies': new FormArray([]) 30 | }); 31 | 32 | this.signUpForm.statusChanges.subscribe(value => { 33 | console.log(value); 34 | }); 35 | 36 | this.signUpForm.patchValue({ 37 | userData: { 38 | username: 'Hai Leela', 39 | }, 40 | gender: 'male', 41 | hobbies: [] 42 | }) 43 | } 44 | 45 | onSubmit() { 46 | console.log(this.signUpForm); 47 | this.signUpForm.reset(); 48 | } 49 | 50 | isRestrictedNames(control: FormControl): { [s: string]: boolean } { 51 | if (this.restrictedNames.includes(control.value)) { 52 | return { nameIsRestricted: true }; 53 | } 54 | return null; 55 | } 56 | 57 | isRestrictedEmails(control: FormControl): Promise | Observable { 58 | let promise = new Promise((resolve, reject) => { 59 | setTimeout(() => { 60 | if (control.value === 'test@test.com') { 61 | resolve({ emailIsRestricted: true }); 62 | } else { 63 | resolve(null); 64 | } 65 | }, 2000) 66 | }); 67 | return promise; 68 | 69 | } 70 | 71 | 72 | 73 | onAddHobby() { 74 | const control = new FormControl(null, [Validators.required]); 75 | (this.signUpForm.get('hobbies')).push(control); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/app/services/auth-interceptor.service.ts: -------------------------------------------------------------------------------- 1 | import { 2 | HttpEventType, 3 | HttpHandler, 4 | HttpInterceptor, 5 | HttpRequest, 6 | } from '@angular/common/http'; 7 | 8 | export class AuthInterceptorService implements HttpInterceptor { 9 | intercept(req: HttpRequest, next: HttpHandler) { 10 | console.log('Request INterceptor'); 11 | let modifiedRequest = req.clone({ 12 | //headers: req.headers.append('auth', 'abc'), 13 | //params: req.params.append('hai', 'hello world'), 14 | }); 15 | return next.handle(modifiedRequest); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/app/services/auth-token-interceptor.service.ts: -------------------------------------------------------------------------------- 1 | import { Params } from '@angular/router'; 2 | import { exhaustMap, take } from 'rxjs/operators'; 3 | import { AuthService } from './auth.service'; 4 | import { 5 | HttpHandler, 6 | HttpInterceptor, 7 | HttpRequest, 8 | } from '@angular/common/http'; 9 | import { Injectable } from '@angular/core'; 10 | 11 | @Injectable() 12 | export class AuthTokenInterceptorService implements HttpInterceptor { 13 | constructor(private authService: AuthService) {} 14 | intercept(req: HttpRequest, next: HttpHandler) { 15 | return this.authService.userSub.pipe( 16 | take(1), 17 | exhaustMap((user) => { 18 | if (!user) { 19 | return next.handle(req); 20 | } 21 | let modifiedReq = req.clone({ 22 | params: req.params.append('auth', user.token), 23 | }); 24 | return next.handle(modifiedReq); 25 | }) 26 | ); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/app/services/auth.service.ts: -------------------------------------------------------------------------------- 1 | import { User } from './../auth/user.model'; 2 | import { HttpClient, HttpErrorResponse } from '@angular/common/http'; 3 | import { Injectable } from '@angular/core'; 4 | import { BehaviorSubject, Subject, throwError } from 'rxjs'; 5 | import { catchError, tap } from 'rxjs/operators'; 6 | import { Router } from '@angular/router'; 7 | 8 | export interface AuthResponseData { 9 | idToken: string; 10 | email: string; 11 | refreshToken: string; 12 | expiresIn: string; 13 | localId: string; 14 | registered?: boolean; 15 | } 16 | 17 | @Injectable({ providedIn: 'root' }) 18 | export class AuthService { 19 | isLoggedIn = false; 20 | userSub = new BehaviorSubject(null); 21 | clearTimeout: any; 22 | 23 | constructor(private http: HttpClient, private router: Router) {} 24 | 25 | signUp(email: string, password: string) { 26 | return this.http 27 | .post( 28 | `https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyASi_-Ea6ygtX9MSBhvqYMIS6MUyy3F3s0 29 | `, 30 | { email, password, returnSecureToken: true } 31 | ) 32 | .pipe(catchError(this.getErrorHandler), tap(this.handleUser.bind(this))); 33 | } 34 | 35 | login(email: string, password: string) { 36 | return this.http 37 | .post( 38 | `https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyASi_-Ea6ygtX9MSBhvqYMIS6MUyy3F3s0 39 | `, 40 | { email, password, returnSecureToken: true } 41 | ) 42 | .pipe(catchError(this.getErrorHandler), tap(this.handleUser.bind(this))); 43 | } 44 | 45 | private handleUser(response: AuthResponseData) { 46 | const expireDate = new Date( 47 | new Date().getTime() + +response.expiresIn * 1000 48 | ); 49 | const user = new User( 50 | response.email, 51 | response.localId, 52 | response.idToken, 53 | expireDate 54 | ); 55 | this.userSub.next(user); 56 | localStorage.setItem('userData', JSON.stringify(user)); 57 | this.autoLogout(+response.expiresIn * 1000); 58 | } 59 | 60 | getErrorHandler(errorRes: HttpErrorResponse) { 61 | let errorMessage = 'An Error Occurred'; 62 | if (!errorRes.error || !errorRes.error.error) { 63 | return throwError(errorMessage); 64 | } 65 | switch (errorRes.error.error.message) { 66 | case 'EMAIL_EXISTS': 67 | errorMessage = 'Email Already Exists'; 68 | break; 69 | case 'EMAIL_NOT_FOUND': 70 | errorMessage = 'Email Not Found'; 71 | break; 72 | case 'INVALID_PASSWORD': 73 | errorMessage = 'Invalid Password'; 74 | break; 75 | } 76 | return throwError(errorMessage); 77 | } 78 | 79 | autoLogin() { 80 | let userData: { 81 | email: string; 82 | _token: string; 83 | expirationDate: string; 84 | localId: string; 85 | } = JSON.parse(localStorage.getItem('userData')); 86 | if (!userData) { 87 | return; 88 | } 89 | 90 | let user = new User( 91 | userData.email, 92 | userData.localId, 93 | userData._token, 94 | new Date(userData.expirationDate) 95 | ); 96 | 97 | if (user.token) { 98 | this.userSub.next(user); 99 | } 100 | 101 | let date = new Date().getTime(); 102 | let expirationDate = new Date(userData.expirationDate).getTime(); 103 | 104 | this.autoLogout(expirationDate - date); 105 | } 106 | 107 | autoLogout(expirationDate: number) { 108 | console.log(expirationDate); 109 | this.clearTimeout = setTimeout(() => { 110 | this.logout(); 111 | }, expirationDate); 112 | } 113 | 114 | logout() { 115 | this.userSub.next(null); 116 | this.router.navigate(['/auth']); 117 | localStorage.removeItem('userData'); 118 | if (this.clearTimeout) { 119 | clearTimeout(this.clearTimeout); 120 | } 121 | } 122 | 123 | isAuthenticated() { 124 | return new Promise((resolve, reject) => { 125 | setTimeout(() => { 126 | resolve(this.isLoggedIn); 127 | }, 1000); 128 | }); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/app/services/dummy.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | 3 | 4 | export class DummyService { 5 | logMessage: string; 6 | 7 | printLog(message: string) { 8 | console.log(message); 9 | console.log(this.logMessage); 10 | this.logMessage = message; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/app/services/guards/auth-guard.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { 3 | ActivatedRouteSnapshot, 4 | CanActivate, 5 | CanActivateChild, 6 | Router, 7 | RouterStateSnapshot, 8 | } from '@angular/router'; 9 | import { Observable } from 'rxjs'; 10 | import { AuthService } from '../auth.service'; 11 | 12 | @Injectable() 13 | export class AuthGuardService implements CanActivate, CanActivateChild { 14 | constructor(private authService: AuthService, private router: Router) {} 15 | canActivate( 16 | route: ActivatedRouteSnapshot, 17 | state: RouterStateSnapshot 18 | ): boolean | Promise | Observable { 19 | return this.authService.isAuthenticated().then((data) => { 20 | if (data) { 21 | return true; 22 | } else { 23 | this.router.navigate(['/']); 24 | } 25 | }); 26 | } 27 | 28 | canActivateChild( 29 | route: ActivatedRouteSnapshot, 30 | state: RouterStateSnapshot 31 | ): boolean | Promise | Observable { 32 | return this.canActivate(route, state); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/app/services/guards/auth.guard.ts: -------------------------------------------------------------------------------- 1 | import { AuthService } from './../auth.service'; 2 | import { Observable } from 'rxjs'; 3 | import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, UrlTree } from '@angular/router'; 4 | import { Injectable } from '@angular/core'; 5 | import { map, take, tap } from 'rxjs/operators'; 6 | 7 | @Injectable({providedIn: 'root'}) 8 | export class AuthGuard implements CanActivate { 9 | 10 | constructor(private authService: AuthService, private router: Router) {} 11 | canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): 12 | boolean | UrlTree | Promise | Observable { 13 | return this.authService.userSub.pipe(take(1), map(user => { 14 | if (!user) { 15 | return this.router.createUrlTree(['/auth']); 16 | } 17 | return true; 18 | })); 19 | 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/app/services/guards/deactivate-guard.service.ts: -------------------------------------------------------------------------------- 1 | import { 2 | ActivatedRouteSnapshot, 3 | CanDeactivate, 4 | RouterStateSnapshot, 5 | } from '@angular/router'; 6 | import { Observable } from 'rxjs'; 7 | 8 | export interface IDeactivateGuard { 9 | canExit: () => boolean | Promise | Observable; 10 | } 11 | 12 | export class DeactivateGuardService implements CanDeactivate { 13 | canDeactivate( 14 | component: IDeactivateGuard, 15 | route: ActivatedRouteSnapshot, 16 | currentState: RouterStateSnapshot, 17 | nextState: RouterStateSnapshot 18 | ): boolean | Promise | Observable { 19 | return component.canExit(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/app/services/logging-interceptor.service.ts: -------------------------------------------------------------------------------- 1 | import { 2 | HttpEventType, 3 | HttpHandler, 4 | HttpInterceptor, 5 | HttpRequest, 6 | } from '@angular/common/http'; 7 | 8 | import { tap } from 'rxjs/operators'; 9 | 10 | export class LoggingInterceptorService implements HttpInterceptor { 11 | intercept(req: HttpRequest, next: HttpHandler) { 12 | console.log(req.headers); 13 | return next.handle(req).pipe( 14 | tap((event) => { 15 | console.log(event); 16 | console.log('logging Response from interceptor'); 17 | if (event.type === HttpEventType.Response) { 18 | console.log(event.body); 19 | } 20 | }) 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/app/services/post.service.ts: -------------------------------------------------------------------------------- 1 | import { AuthService } from './auth.service'; 2 | import { Injectable } from '@angular/core'; 3 | import { 4 | HttpClient, 5 | HttpEventType, 6 | HttpHeaders, 7 | HttpParams, 8 | } from '@angular/common/http'; 9 | import { Post } from '../posts/Post.model'; 10 | import { map, take, tap, switchMap } from 'rxjs/operators'; 11 | 12 | @Injectable({ providedIn: 'root' }) 13 | export class PostService { 14 | constructor(private http: HttpClient, private authService: AuthService) {} 15 | 16 | fetchPosts() { 17 | return this.http 18 | .get<{ [key: string]: Post }>( 19 | `https://ng-complete-guide-aad09.firebaseio.com/posts.json` 20 | ) 21 | .pipe( 22 | map((response) => { 23 | let posts: Post[] = []; 24 | for (let key in response) { 25 | posts.push({ ...response[key], key }); 26 | } 27 | return posts; 28 | }) 29 | ); 30 | } 31 | 32 | createPost(postData: Post) { 33 | return this.http.post<{ name: string }>( 34 | 'https://ng-complete-guide-aad09.firebaseio.com/posts.json', 35 | postData, 36 | { 37 | headers: new HttpHeaders({ 38 | 'custom-header': 'post Leela', 39 | }), 40 | observe: 'body', 41 | } 42 | ); 43 | } 44 | clearPosts() { 45 | this.http 46 | .delete('https://ng-complete-guide-aad09.firebaseio.com/posts.json', { 47 | observe: 'events', 48 | responseType: 'text', 49 | }) 50 | .pipe( 51 | tap((response) => { 52 | if (response.type === HttpEventType.Sent) { 53 | console.log('request sent'); 54 | } 55 | 56 | if (response.type === HttpEventType.Response) { 57 | console.log(response); 58 | } 59 | }) 60 | ) 61 | .subscribe((response) => { 62 | //console.log(response); 63 | }); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/app/services/resolvers/user-resolve.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { ActivatedRouteSnapshot, Resolve, RouterStateSnapshot } from '@angular/router'; 3 | import { Observable } from 'rxjs'; 4 | import { UserService } from '../user.service'; 5 | 6 | interface User { 7 | id: string; 8 | name: string; 9 | } 10 | 11 | @Injectable() 12 | export class UserResolveService implements Resolve { 13 | 14 | constructor(private userService: UserService) { 15 | 16 | } 17 | 18 | resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): User | Observable | Promise { 19 | let id = route.params['id']; 20 | 21 | let details = this.userService.getUser(id); 22 | return details; 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /src/app/services/user.service.ts: -------------------------------------------------------------------------------- 1 | import { EventEmitter } from '@angular/core' 2 | import { Subject } from 'rxjs'; 3 | 4 | export class UserService { 5 | 6 | userAddedEvent = new Subject(); 7 | 8 | getUser(id: string) { 9 | if (id === '1') { 10 | return { 11 | id: '1', 12 | name: 'Leela' 13 | } 14 | } else { 15 | return { 16 | id: '1', 17 | name: 'Krishna' 18 | } 19 | } 20 | } 21 | 22 | addUser() { 23 | this.userAddedEvent.next(true); 24 | } 25 | } -------------------------------------------------------------------------------- /src/app/shared.module.ts: -------------------------------------------------------------------------------- 1 | import { DummyService } from './services/dummy.service'; 2 | import { CommonModule } from '@angular/common'; 3 | import { FormsModule } from '@angular/forms'; 4 | import { NgModule } from '@angular/core'; 5 | import { FilterPipe } from './Pipes/filter.pipe'; 6 | import { ShortenPipe } from './Pipes/shorten.pipe'; 7 | 8 | 9 | 10 | @NgModule({ 11 | declarations: [ 12 | ShortenPipe, 13 | FilterPipe 14 | ], 15 | imports: [FormsModule, 16 | CommonModule], 17 | exports: [ShortenPipe, 18 | FilterPipe,FormsModule, 19 | CommonModule], 20 | providers: [DummyService] 21 | }) 22 | export class SharedModule { 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/app/shared/Placeholder.directive.ts: -------------------------------------------------------------------------------- 1 | import { Directive, ViewContainerRef } from '@angular/core'; 2 | 3 | @Directive({ 4 | selector: '[appPlaceholder]' 5 | }) 6 | export class PlaceholderDirective { 7 | constructor(public ViewContainerRef: ViewContainerRef) { 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/app/shared/alert-modal/alert-modal.component.css: -------------------------------------------------------------------------------- 1 | .backdrop { 2 | position: fixed; 3 | width: 100vw; 4 | height: 100vh; 5 | top: 0px; 6 | left: 0px; 7 | z-index: 50; 8 | background: rgba(0, 0, 0, 0.75); 9 | } 10 | 11 | .alert-box { 12 | position: fixed; 13 | width: 60vw; 14 | left: 20vw; 15 | padding: 20px; 16 | top: 20vh; 17 | z-index: 100; 18 | background: white; 19 | box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.5); 20 | } 21 | -------------------------------------------------------------------------------- /src/app/shared/alert-modal/alert-modal.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 | {{error}} 4 |
5 | 6 |
7 |
8 | -------------------------------------------------------------------------------- /src/app/shared/alert-modal/alert-modal.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, Input, Output, EventEmitter } from '@angular/core'; 2 | @Component({ 3 | selector: 'app-alert-modal', 4 | templateUrl: './alert-modal.component.html', 5 | styleUrls: ['./alert-modal.component.css'] 6 | }) 7 | export class AlertModalComponent { 8 | @Input() error; 9 | @Output() close = new EventEmitter(); 10 | 11 | onCloseClick() { 12 | this.close.emit(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/app/shared/loading-spinner/loading-spinner.component.css: -------------------------------------------------------------------------------- 1 | .lds-ring { 2 | display: inline-block; 3 | position: relative; 4 | width: 80px; 5 | height: 80px; 6 | } 7 | .lds-ring div { 8 | box-sizing: border-box; 9 | display: block; 10 | position: absolute; 11 | width: 64px; 12 | height: 64px; 13 | margin: 8px; 14 | border: 8px solid #fff; 15 | border-radius: 50%; 16 | animation: lds-ring 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite; 17 | border-color: blue transparent transparent transparent; 18 | } 19 | .lds-ring div:nth-child(1) { 20 | animation-delay: -0.45s; 21 | } 22 | .lds-ring div:nth-child(2) { 23 | animation-delay: -0.3s; 24 | } 25 | .lds-ring div:nth-child(3) { 26 | animation-delay: -0.15s; 27 | } 28 | @keyframes lds-ring { 29 | 0% { 30 | transform: rotate(0deg); 31 | } 32 | 100% { 33 | transform: rotate(360deg); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/app/shared/loading-spinner/loading-spinner.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-loading-spinner', 5 | template: `
6 |
7 |
8 |
9 |
10 |
`, 11 | styleUrls: ['./loading-spinner.component.css'], 12 | }) 13 | export class LoadingSpinnerComponent {} 14 | -------------------------------------------------------------------------------- /src/app/template-form/template-form.component.css: -------------------------------------------------------------------------------- 1 | input.ng-invalid.ng-touched { 2 | border: 1px solid red; 3 | } -------------------------------------------------------------------------------- /src/app/template-form/template-form.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |
5 | 6 | 7 |
User Data is Invalid
8 |
9 |
10 | 11 | 12 |
13 | 14 |
15 | 16 | 18 | Please enter 19 | valid 20 | email 21 |
22 |
23 | 24 | 25 | 26 |
27 | 28 | 32 |
33 | 34 |
35 | 36 | 37 |
38 | 39 |
{{about}}
40 |
41 | 42 |
43 |
44 |
45 | 46 |
47 |

Your Data

48 |
User: {{user.username}}
49 |
Email: {{user.email}}
50 |
Gender: {{user.gender}}
51 |
About: {{user.about}}
52 |
53 |
54 |
-------------------------------------------------------------------------------- /src/app/template-form/template-form.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { TemplateFormComponent } from './template-form.component'; 4 | 5 | describe('TemplateFormComponent', () => { 6 | let component: TemplateFormComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ TemplateFormComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(TemplateFormComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/template-form/template-form.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit, ViewChild } from '@angular/core'; 2 | import { NgForm } from '@angular/forms'; 3 | 4 | @Component({ 5 | selector: 'app-template-form', 6 | templateUrl: './template-form.component.html', 7 | styleUrls: ['./template-form.component.css'] 8 | }) 9 | export class TemplateFormComponent implements OnInit { 10 | gender = 'female'; 11 | about = ''; 12 | submitted = false; 13 | user = { 14 | username: '', 15 | email: '', 16 | gender: '', 17 | about: '' 18 | } 19 | 20 | @ViewChild('f') signUpForm: NgForm; 21 | 22 | constructor() { } 23 | 24 | ngOnInit(): void { 25 | } 26 | 27 | checkData() { 28 | console.log(this.signUpForm); 29 | } 30 | 31 | onFormSubmit() { 32 | this.submitted = true; 33 | this.user.username = this.signUpForm.value.userData.username; 34 | this.user.email = this.signUpForm.value.userData.email; 35 | this.user.gender = this.signUpForm.value.gender; 36 | this.user.about = this.signUpForm.value.about; 37 | this.signUpForm.reset(); 38 | } 39 | 40 | fillValues() { 41 | this.signUpForm.form.patchValue({ 42 | userData: { 43 | email: 'leela@leela.com', 44 | username: 'Leela' 45 | }, 46 | 47 | }) 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/app/user-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { UserResolveService } from './services/resolvers/user-resolve.service'; 2 | import { DeactivateGuardService } from './services/guards/deactivate-guard.service'; 3 | import { EditUserComponent } from './edit-user/edit-user.component'; 4 | import { UserComponent } from './user/user.component'; 5 | import { AuthGuard } from './services/guards/auth.guard'; 6 | import { UsersComponent } from './users/users.component'; 7 | import { Routes, RouterModule } from '@angular/router'; 8 | import { NgModule } from '@angular/core'; 9 | 10 | const routes: Routes = [ 11 | { 12 | path: '', 13 | component: UsersComponent, 14 | canActivate: [AuthGuard], 15 | children: [ 16 | { path: ':id/:name', component: UserComponent }, 17 | { 18 | path: ':id/:name/edit', 19 | component: EditUserComponent, 20 | canDeactivate: [DeactivateGuardService], 21 | resolve: { user: UserResolveService }, 22 | }, 23 | ], 24 | }, 25 | ]; 26 | @NgModule({ 27 | imports: [ 28 | RouterModule.forChild(routes) 29 | ], 30 | exports: [RouterModule] 31 | }) 32 | export class UserRoutingModule { 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/app/user.module.ts: -------------------------------------------------------------------------------- 1 | import { DummyService } from './services/dummy.service'; 2 | import { SharedModule } from './shared.module'; 3 | import { UserRoutingModule } from './user-routing.module'; 4 | import { UsersComponent } from './users/users.component'; 5 | import { EditUserComponent } from './edit-user/edit-user.component'; 6 | import { UserComponent } from './user/user.component'; 7 | import { NgModule } from '@angular/core'; 8 | 9 | @NgModule({ 10 | declarations: [ 11 | UserComponent, 12 | EditUserComponent, 13 | UsersComponent, 14 | ], 15 | imports: [ 16 | UserRoutingModule, 17 | SharedModule 18 | ], 19 | 20 | }) 21 | export class UserModule { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/app/user/user.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leelanarasimha/angularcourse/54d65e7dc8ba5fe38706c13e9d9d0f2678e1354b/src/app/user/user.component.css -------------------------------------------------------------------------------- /src/app/user/user.component.html: -------------------------------------------------------------------------------- 1 |
2 |
User with id is {{ user.id }}
3 |
User with name is {{ user.name }}
4 |
5 | 6 |
7 | 8 |
9 | -------------------------------------------------------------------------------- /src/app/user/user.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { UserComponent } from './user.component'; 4 | 5 | describe('UserComponent', () => { 6 | let component: UserComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ UserComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(UserComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/user/user.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { ActivatedRoute, Params, Router } from '@angular/router'; 3 | 4 | @Component({ 5 | selector: 'app-user', 6 | templateUrl: './user.component.html', 7 | styleUrls: ['./user.component.css'], 8 | }) 9 | export class UserComponent implements OnInit { 10 | user: { id: string; name: string }; 11 | constructor(private route: ActivatedRoute, private router: Router) {} 12 | 13 | ngOnInit(): void { 14 | this.user = { 15 | id: this.route.snapshot.params['id'], 16 | name: this.route.snapshot.params['name'], 17 | }; 18 | this.route.params.subscribe((data: Params) => { 19 | this.user = { 20 | id: data['id'], 21 | name: data['name'], 22 | }; 23 | }); 24 | 25 | this.route.queryParams.subscribe((data) => { 26 | console.log(data); 27 | }); 28 | 29 | this.route.fragment.subscribe((data) => { 30 | console.log(data); 31 | }); 32 | } 33 | 34 | getRamaDetails() { 35 | this.router.navigate(['/users', 2, 'Rama'], { 36 | queryParams: { page: 1, search: 'Leela' }, 37 | fragment: 'loading', 38 | }); 39 | } 40 | 41 | onUserEdit() { 42 | this.router.navigate(['/users', this.user.id, this.user.name, 'edit'], { 43 | queryParamsHandling: 'preserve', 44 | }); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/app/users/users.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leelanarasimha/angularcourse/54d65e7dc8ba5fe38706c13e9d9d0f2678e1354b/src/app/users/users.component.css -------------------------------------------------------------------------------- /src/app/users/users.component.html: -------------------------------------------------------------------------------- 1 | 9 | 10 |
11 | 12 |
13 |
14 | 15 |
16 | -------------------------------------------------------------------------------- /src/app/users/users.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { UsersComponent } from './users.component'; 4 | 5 | describe('UsersComponent', () => { 6 | let component: UsersComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ UsersComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(UsersComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/users/users.component.ts: -------------------------------------------------------------------------------- 1 | import { DummyService } from './../services/dummy.service'; 2 | import { Component, OnInit } from '@angular/core'; 3 | import { Router } from '@angular/router'; 4 | import { UserService } from '../services/user.service'; 5 | 6 | @Component({ 7 | selector: 'app-users', 8 | templateUrl: './users.component.html', 9 | styleUrls: ['./users.component.css'], 10 | }) 11 | export class UsersComponent implements OnInit { 12 | usersData = ['Rama', 'Krishna', 'Leela']; 13 | constructor(private router: Router, private userService: UserService, private dummyService: DummyService) { } 14 | 15 | ngOnInit(): void { 16 | this.dummyService.printLog('Hello from Users COmponent'); 17 | } 18 | 19 | onCategoriesClick() { 20 | //perform some logic 21 | //navigate to page 22 | 23 | this.router.navigate(['/categories']); 24 | } 25 | 26 | onUserAddedClick() { 27 | this.userService.addUser(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leelanarasimha/angularcourse/54d65e7dc8ba5fe38706c13e9d9d0f2678e1354b/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build --prod` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/dist/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leelanarasimha/angularcourse/54d65e7dc8ba5fe38706c13e9d9d0f2678e1354b/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Angularrouting 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.error(err)); 13 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 22 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 23 | 24 | /** 25 | * Web Animations `@angular/platform-browser/animations` 26 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 27 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 28 | */ 29 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 30 | 31 | /** 32 | * By default, zone.js will patch all possible macroTask and DomEvents 33 | * user can disable parts of macroTask/DomEvents patch by setting following flags 34 | * because those flags need to be set before `zone.js` being loaded, and webpack 35 | * will put import in the top of bundle, so user need to create a separate file 36 | * in this directory (for example: zone-flags.ts), and put the following flags 37 | * into that file, and then add the following code before importing zone.js. 38 | * import './zone-flags'; 39 | * 40 | * The flags allowed in zone-flags.ts are listed here. 41 | * 42 | * The following flags will work for all browsers. 43 | * 44 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 45 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 46 | * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 47 | * 48 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 49 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 50 | * 51 | * (window as any).__Zone_enable_cross_context_check = true; 52 | * 53 | */ 54 | 55 | /*************************************************************************************************** 56 | * Zone JS is required by default for Angular itself. 57 | */ 58 | import 'zone.js/dist/zone'; // Included with Angular CLI. 59 | 60 | 61 | /*************************************************************************************************** 62 | * APPLICATION IMPORTS 63 | */ 64 | -------------------------------------------------------------------------------- /src/styles.css: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/dist/zone-testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: { 11 | context(path: string, deep?: boolean, filter?: RegExp): { 12 | keys(): string[]; 13 | (id: string): T; 14 | }; 15 | }; 16 | 17 | // First, initialize the Angular testing environment. 18 | getTestBed().initTestEnvironment( 19 | BrowserDynamicTestingModule, 20 | platformBrowserDynamicTesting() 21 | ); 22 | // Then we find all the tests. 23 | const context = require.context('./', true, /\.spec\.ts$/); 24 | // And load the modules. 25 | context.keys().map(context); 26 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.base.json", 4 | "compilerOptions": { 5 | "outDir": "./out-tsc/app", 6 | "types": [] 7 | }, 8 | "files": [ 9 | "src/main.ts", 10 | "src/polyfills.ts" 11 | ], 12 | "include": [ 13 | "src/**/*.d.ts" 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /tsconfig.base.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "compileOnSave": false, 4 | "compilerOptions": { 5 | "baseUrl": "./", 6 | "outDir": "./dist/out-tsc", 7 | "sourceMap": true, 8 | "declaration": false, 9 | "downlevelIteration": true, 10 | "experimentalDecorators": true, 11 | "moduleResolution": "node", 12 | "importHelpers": true, 13 | "target": "es2015", 14 | "module": "es2020", 15 | "lib": [ 16 | "es2018", 17 | "dom" 18 | ] 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | /* 2 | This is a "Solution Style" tsconfig.json file, and is used by editors and TypeScript’s language server to improve development experience. 3 | It is not intended to be used to perform a compilation. 4 | 5 | To learn more about this file see: https://angular.io/config/solution-tsconfig. 6 | */ 7 | { 8 | "files": [], 9 | "references": [ 10 | { 11 | "path": "./tsconfig.app.json" 12 | }, 13 | { 14 | "path": "./tsconfig.spec.json" 15 | } 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.base.json", 4 | "compilerOptions": { 5 | "outDir": "./out-tsc/spec", 6 | "types": [ 7 | "jasmine" 8 | ] 9 | }, 10 | "files": [ 11 | "src/test.ts", 12 | "src/polyfills.ts" 13 | ], 14 | "include": [ 15 | "src/**/*.spec.ts", 16 | "src/**/*.d.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint:recommended", 3 | "rules": { 4 | "align": { 5 | "options": [ 6 | "parameters", 7 | "statements" 8 | ] 9 | }, 10 | "array-type": false, 11 | "arrow-return-shorthand": true, 12 | "curly": true, 13 | "deprecation": { 14 | "severity": "warning" 15 | }, 16 | "component-class-suffix": true, 17 | "contextual-lifecycle": true, 18 | "directive-class-suffix": true, 19 | "directive-selector": [ 20 | true, 21 | "attribute", 22 | "app", 23 | "camelCase" 24 | ], 25 | "component-selector": [ 26 | true, 27 | "element", 28 | "app", 29 | "kebab-case" 30 | ], 31 | "eofline": true, 32 | "import-blacklist": [ 33 | true, 34 | "rxjs/Rx" 35 | ], 36 | "import-spacing": true, 37 | "indent": { 38 | "options": [ 39 | "spaces" 40 | ] 41 | }, 42 | "max-classes-per-file": false, 43 | "max-line-length": [ 44 | true, 45 | 140 46 | ], 47 | "member-ordering": [ 48 | true, 49 | { 50 | "order": [ 51 | "static-field", 52 | "instance-field", 53 | "static-method", 54 | "instance-method" 55 | ] 56 | } 57 | ], 58 | "no-console": [ 59 | true, 60 | "debug", 61 | "info", 62 | "time", 63 | "timeEnd", 64 | "trace" 65 | ], 66 | "no-empty": false, 67 | "no-inferrable-types": [ 68 | true, 69 | "ignore-params" 70 | ], 71 | "no-non-null-assertion": true, 72 | "no-redundant-jsdoc": true, 73 | "no-switch-case-fall-through": true, 74 | "no-var-requires": false, 75 | "object-literal-key-quotes": [ 76 | true, 77 | "as-needed" 78 | ], 79 | "quotemark": [ 80 | true, 81 | "single" 82 | ], 83 | "semicolon": { 84 | "options": [ 85 | "always" 86 | ] 87 | }, 88 | "space-before-function-paren": { 89 | "options": { 90 | "anonymous": "never", 91 | "asyncArrow": "always", 92 | "constructor": "never", 93 | "method": "never", 94 | "named": "never" 95 | } 96 | }, 97 | "typedef": [ 98 | true, 99 | "call-signature" 100 | ], 101 | "typedef-whitespace": { 102 | "options": [ 103 | { 104 | "call-signature": "nospace", 105 | "index-signature": "nospace", 106 | "parameter": "nospace", 107 | "property-declaration": "nospace", 108 | "variable-declaration": "nospace" 109 | }, 110 | { 111 | "call-signature": "onespace", 112 | "index-signature": "onespace", 113 | "parameter": "onespace", 114 | "property-declaration": "onespace", 115 | "variable-declaration": "onespace" 116 | } 117 | ] 118 | }, 119 | "variable-name": { 120 | "options": [ 121 | "ban-keywords", 122 | "check-format", 123 | "allow-pascal-case" 124 | ] 125 | }, 126 | "whitespace": { 127 | "options": [ 128 | "check-branch", 129 | "check-decl", 130 | "check-operator", 131 | "check-separator", 132 | "check-type", 133 | "check-typecast" 134 | ] 135 | }, 136 | "no-conflicting-lifecycle": true, 137 | "no-host-metadata-property": true, 138 | "no-input-rename": true, 139 | "no-inputs-metadata-property": true, 140 | "no-output-native": true, 141 | "no-output-on-prefix": true, 142 | "no-output-rename": true, 143 | "no-outputs-metadata-property": true, 144 | "template-banana-in-box": true, 145 | "template-no-negated-async": true, 146 | "use-lifecycle-interface": true, 147 | "use-pipe-transform-interface": true 148 | }, 149 | "rulesDirectory": [ 150 | "codelyzer" 151 | ] 152 | } --------------------------------------------------------------------------------