Quantcast
Channel: Christos S. – chsakell's Blog
Viewing all articles
Browse latest Browse all 43

Angular 2 CRUD, modals, animations, pagination, datetimepicker and much more

$
0
0

Angular 2 and TypeScript have fetched client-side development to the next level but till recently most of web developers hesitated to start a production SPA with those two. The first reason was that Angular was still in development and the second one is that components that are commonly required in a production SPA were not yet available. Which are those components? DateTime pickers, custom Modal popups, animations and much more.. Components and plugins that make websites fluid and user-friendly. But this is old news now, Angular is very close to release its final version and community been familiarized with the new framework has produced a great ammount of such components.

What this post is all about

This post is another step by step walkthrough to build Angular 2 SPAs using TypeScript. I said another one, cause we have already seen such a post before. The difference though is that now we have all the knowledge and tools to create more structured, feature-enhanced and production level SPAs and this is what we will do on this post. The Schedule.SPA application that we are going to build will make use of all the previously mentioned components following the recommended Angular style guide as much as possible. As far as the back-end infrastructure (REST API) that our application will make use of, we have already built it in the previous post Building REST APIs using ASP.NET Core and Entity Framework Core. The source code for the API which was built using .NET Core can be found here where you will also find instructions how to run it. The SPA will display schedules and their related information (who created it, attendees, etc..). It will allow the user to manimulate many aspects of each schedule which means that we are going to see CRUD operations in action. Let’s see the in detail all the features that this SPA will incorporate.

  • HTTP CRUD operations
  • Routing and Navigation using the new Component Router
  • Custom Modal popup windows
  • Angular 2 animations
  • DateTime pickers
  • Notifications
  • Pagination through Request/Response headers
  • Angular Forms validation
  • Angular Directives
  • Angular Pipes

Not bad right..? Before start building it let us see the final product with a .gif (click to view in better quality).
angular-scheduler-spa

Start coding

One decision I ‘ve made for this app is to use Visual Studio Code rich text editor for development. While I used VS 2015 for developing the API, I still find it useless when it comes to TypeScript development. Lots of compile and build errors may make your life misserable. One the other hand, VS Code has a great intellisense features and a great integrated command line which allows you to run commands directly from the IDE. You can use though your text editor of your preference. First thing we need to do is configure the Angular – TypeScript application. Create a folder named Scheduler.SPA and open it in your favorite editor. Add the package.json file where we define all the packages we are going to use in our application.

{
  "version": "1.0.0",
  "name": "scheduler",
  "author": "Chris Sakellarios",
  "license": "MIT",
  "repository": "https://github.com/chsakell/angular2-features",
  "private": true,
  "dependencies": {
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/core": "2.0.0-rc.4",
    "@angular/forms": "0.2.0",
    "@angular/http": "2.0.0-rc.4",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angular/router": "3.0.0-beta.1",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.4",
    "angular2-in-memory-web-api": "0.0.14",
    "bootstrap": "^3.3.6",
    "bootstrap-datepicker": "^1.6.1",
    "bootstrap-timepicker": "^0.5.2",
    "core-js": "^2.4.0",
    "jquery": "^3.0.0",
    "lodash": "^4.13.1",
    "moment": "^2.13.0",
    "ng2-bootstrap": "^1.0.17",
    "ng2-bs3-modal": "^0.6.1",
    "ng2-datetime": "^1.1.0",
    "ng2-slim-loading-bar": "^1.2.3",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "systemjs": "0.19.27",
    "zone.js": "^0.6.12"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "del": "^2.2.0",
    "gulp": "^3.9.1",
    "gulp-tslint": "^5.0.0",
    "jquery": "^3.0.0",
    "lite-server": "^2.2.0",
    "typescript": "^1.8.10",
    "typings": "^1.0.4",
    "tslint": "^3.10.2"
  },
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite --baseDir ./app --port 8000\" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  }
}

We declared all the Angular required packages (Github repo will always be updated when new version releases) and some others such as the bootstrap-datepicker and ng2-bootstrap which will help us incorporate some cool features in our SPA. When some part of our application make use of that type of packages I will let you know. Next add the systemjs.config.js SystemJS configuration file.

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
    // map tells the System loader where to look for things
    var map = {
        'app': 'app', // 'dist',
        '@angular': 'node_modules/@angular',
        'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
        'jquery': 'node_modules/jquery/',
        'lodash': 'node_modules/lodash/lodash.js',
        'moment': 'node_modules/moment/',
        'ng2-bootstrap': 'node_modules/ng2-bootstrap',
        'ng2-datetime': 'node_modules/ng2-datetime/',
        'ng2-slim-loading-bar': 'node_modules/ng2-slim-loading-bar',
        'ng2-bs3-modal': 'node_modules/ng2-bs3-modal',
        'rxjs': 'node_modules/rxjs',
        'symbol-observable': 'node_modules/symbol-observable'
    };
    // packages tells the System loader how to load when no filename and/or no extension
    var packages = {
        'app': { main: 'main.js', defaultExtension: 'js' },
        'rxjs': { defaultExtension: 'js' },
        'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
        'moment': { main: 'moment.js', defaultExtension: 'js' },
        'ng2-bootstrap': { main: 'ng2-bootstrap.js', defaultExtension: 'js' },
        'ng2-datetime': { main: 'index.js', defaultExtension: 'js' },
        'ng2-slim-loading-bar': { defaultExtension: 'js' },
        'ng2-bs3-modal': { defaultExtension: 'js' },
        'symbol-observable': { main: 'index.js', defaultExtension: 'js' }
    };
    var ngPackageNames = [
        'common',
        'compiler',
        'core',
        'forms',
        'http',
        'platform-browser',
        'platform-browser-dynamic',
        'router',
        'router-deprecated',
        'upgrade',
    ];
    // Individual files (~300 requests):
    function packIndex(pkgName) {
        packages['@angular/' + pkgName] = { main: 'index.js', defaultExtension: 'js' };
    }
    // Bundled (~40 requests):
    function packUmd(pkgName) {
        packages['@angular/' + pkgName] = { main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
    }
    // Most environments should use UMD; some (Karma) need the individual index files
    var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
    // Add package entries for angular packages
    ngPackageNames.forEach(setPackageConfig);
    var config = {
        map: map,
        packages: packages
    };

    System.config(config);
})(this);

I ‘ll make a pause here just to ensure that you understand how SystemJS and the previous two files work together. Suppose that you want to use DateTime picker in your app. Searching the internet you find an NPM package saying that you need to run the following command to install it.

npm install ng2-datetime --save

What this command will do is download the package inside the node_modules folder and add it as a dependency in the package.json. To use that package in your application you need to import it in the component that needs its functionality like this.

import { NKDatetime } from 'ng2-datetime/ng2-datetime';

In most cases you will find the import statement on the package documentation. Is that all you need to use the package? No, cause SystemJS will make a request to http://localhost:your_port/ng2-datetime/ng2-datetime which of course doesn’t exist.
angular-crud-modal-animation-01
Modules are dynamically loaded using SystemJS and the first thing to do is to inform SystemJS where to look when a request to ng2-datetime dispatches the server. This is done through the map object in the systemjs.config.js as follow.

// map tells the System loader where to look for things
var map = {
    'app': 'app', // 'dist',
    '@angular': 'node_modules/@angular',
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
    'jquery': 'node_modules/jquery/',
    'lodash': 'node_modules/lodash/lodash.js',
    'moment': 'node_modules/moment/',
    'ng2-bootstrap': 'node_modules/ng2-bootstrap',
    'ng2-datetime': 'node_modules/ng2-datetime/',
    'ng2-slim-loading-bar': 'node_modules/ng2-slim-loading-bar',
    'ng2-bs3-modal': 'node_modules/ng2-bs3-modal',
    'rxjs': 'node_modules/rxjs',
    'symbol-observable': 'node_modules/symbol-observable'
};

From now on each time a request to ng2-datetime reaches the server, SystemJS will map the request to node_modules/ng2-datetime which actually exists since we have installed the package. Are we ready yet? No, we still need to inform SystemJS what file name to load and the default extension. This is done using the packages object in the systemjs.config.js.

// packages tells the System loader how to load when no filename and/or no extension
var packages = {
    'app': { main: 'main.js', defaultExtension: 'js' },
    'rxjs': { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
    'moment': { main: 'moment.js', defaultExtension: 'js' },
    'ng2-bootstrap': { main: 'ng2-bootstrap.js', defaultExtension: 'js' },
    'ng2-datetime': { main: 'index.js', defaultExtension: 'js' },
    'ng2-slim-loading-bar': { defaultExtension: 'js' },
    'ng2-bs3-modal': { defaultExtension: 'js' },
    'symbol-observable': { main: 'index.js', defaultExtension: 'js' }
    };

Our SPA is a TypeScript application so go ahead and add a tsconfig.json file.

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  }
}

This file will be used by the tsc command when transpiling TypeScript to pure ES5 JavaScript. We also need a typings.json file.

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160602141332",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "jquery": "registry:dt/jquery#1.10.0+20160417213236",
    "node": "registry:dt/node#6.0.0+20160621231320"
  },
  "dependencies": {
    "lodash": "registry:npm/lodash#4.0.0+20160416211519"
  }
}

Those are mostly Angular dependencies plus jquery and lodash that our SPA will make use of. We are going to use some client-side external libraries such as alertify.js and font-awesome. Add a bower.json file and set its contents as follow.

{
  "name": "scheduler.spa",
  "private": true,
  "dependencies": {
    "alertify.js" : "0.3.11",
    "bootstrap": "^3.3.6",
    "font-awesome": "latest"
  }
}

At this point we are all set configuring the SPA so go ahead and run the following commands:

npm install
bower install

npm install will also run typings install as a postinstall event. Before start typing the TypeScript code add the index.html page as well.

<!DOCTYPE html>
<html>
<head>
    <base href="/">
    <meta charset="utf-8" />
    <title>Scheduler</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="node_modules/bootstrap-timepicker/css/bootstrap-timepicker.min.css">
    <link rel="stylesheet" href="node_modules/bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css">
    <link href="bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
    <link href="bower_components/alertify.js/themes/alertify.core.css" rel="stylesheet" />
    <link href="bower_components/alertify.js/themes/alertify.bootstrap.css" rel="stylesheet" />
    <link rel="stylesheet" type="text/css" href="node_modules/ng2-slim-loading-bar/ng2-slim-loading-bar.css">
    <link rel="stylesheet" href="../assets/css/styles.css" />

    <script src="bower_components/jquery/dist/jquery.min.js"></script>
    <script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="bower_components/alertify.js/lib/alertify.min.js"></script>

    <script src="node_modules/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js"></script>
    <script src="node_modules/bootstrap-timepicker/js/bootstrap-timepicker.min.js "></script>

    <!-- 1. Load libraries -->
     <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
</head>
<body>
    <scheduler>
        <div class="loader"></div>
    </scheduler>
</body>
</html>

Angular & TypeScript in action

Add a folder named app at the root of the application and create four subfolders named home, schedules, users and shared. The home folder is responsible to display a landing page, the schedules and users are the basic features in the SPA and the latter shared will contain any component that will be used across the entire app, such as data service or utility services. I will start pasting the code from bottom to top, in other words from the files that bootstrap the application to those that implement certain features. Don’t worry if we haven’t implement all the required components while showing the code, we will during the process. I will however been giving you information regarding any component that haven’t implemented yet.

Bootstrapping the app

Add the main.ts file under app.

import { bootstrap } from '@angular/platform-browser-dynamic';

import { AppComponent } from './app.component';
import { APP_ROUTER_PROVIDERS } from './app.routes';

bootstrap(AppComponent, [APP_ROUTER_PROVIDERS]).then(
    success => console.log('AppComponent bootstrapped!'),
    error => console.log(error)
);

I am sure you are familiar with the above code. The app will be bootstrapped with an AppComponent but the most important part of the code is the APP_ROUTER_PROVIDERS. Those are all the routes for the SPA defined in an app.routes.ts file under app folder. Go ahead and create that file.

import { provideRouter  } from '@angular/router';

import { HomeComponent } from './home/home.component';
import { UserListComponent } from './users/user-list.component';
import { ScheduleRoutes } from './schedules/schedule.routes';

export const routes = [
    ...ScheduleRoutes,
    { path: 'users', component: UserListComponent },
    { path: '', component: HomeComponent }
];

export const APP_ROUTER_PROVIDERS = [
  provideRouter(routes)
];

This is how we use the new Component Router. At the moment you can understand that http://localhost:your_port/ will activate the HomeComponent and http://localhost:your_port/users the UserListComponent which display all users. If you wonder what ScheduleRoutes is, it’s nothing more than same type of routes but defined in the appropriate folder/feature (schedules). We continue with the AppComponent in a app.component.ts file under app folder.

import { Component, OnInit, ViewContainerRef } from '@angular/core';
import { ROUTER_DIRECTIVES, Router } from '@angular/router';

// Add the RxJS Observable operators we need in this app.
import './rxjs-operators';

import { APP_PROVIDERS } from './app.providers';

import { SlimLoadingBar } from 'ng2-slim-loading-bar/ng2-slim-loading-bar';

@Component({
    selector: 'scheduler',
    templateUrl: 'app/app.component.html',
    directives: [ROUTER_DIRECTIVES, SlimLoadingBar],
    providers: [APP_PROVIDERS]
})
export class AppComponent {

    constructor() { }
}

This is the very first component that actually activated through the bootstrap process we defined before using the routes we configured. There are three key points for this component: The first one has to do with the rxjs operators. RxJS is a huge library and it’ s good practice to import only those modules that you actually need, not all the library cause otherwise you will pay a too slow application startup penalty. We will define any operators that we need in a rxjs-operators.ts file under app folder. In this SPA we will use several services (custom or Angular’s) and it’s good pattern to define them an a single file, in our case in an app.providers.ts file. Adding them in the providers property of the root component AppComponent will make those services available to all of its children. Same applies for the SlimLoadingBar directive which is a directive installed using npm. You can search the ng2-slim-loading-bar keyword and you will find that we have followed the process described before regarding NPM packages. Add the following two files under app folder.

// Statics
import 'rxjs/add/observable/throw';

// Operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/toPromise';
import { bind } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http';
import { FORM_PROVIDERS, LocationStrategy, HashLocationStrategy } from '@angular/common';

import { DataService } from './shared/services/data.service';
import { ConfigService } from './shared/utils/config.service';
import { ItemsService } from './shared/utils/items.service';
import { NotificationService } from './shared/utils/notification.service';

import {SlimLoadingBarService} from 'ng2-slim-loading-bar/ng2-slim-loading-bar';

export const APP_PROVIDERS = [
    ConfigService,
    DataService,
    ItemsService,
    NotificationService,
    FORM_PROVIDERS,
    HTTP_PROVIDERS,
    SlimLoadingBarService
];

Beside some Angular’s modules we can see that we have added some @Injectable() services that we will create later on. The DataService holds the CRUD operations for sending HTTP request to the API, the ItemsService defines custom methods for manipulating mostly arrays using the lodash library and last but not least the NotificationService has methods to display notifications to the user. The AppComponent is the root component which means has a router-outlet element on its template where other children components are rendered. Add the app.component.html file under app folder.

<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" [routerLink]="['/']">
                <i class="fa fa-home fa-3x" aria-hidden="true"></i>
            </a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li>
                    <a [routerLink]="['/schedules']"><i class="fa fa-calendar fa-3x" aria-hidden="true"></i></a>
                </li>
                <li>
                    <a [routerLink]="['/users']"><i class="fa fa-users fa-3x" aria-hidden="true"></i></a>
                </li>
                <li>
                    <a href="#"><i class="fa fa-info fa-3x" aria-hidden="true"></i></a>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li>
                    <a href="https://www.facebook.com/chsakells.blog" target="_blank">
                        <i class="fa fa-facebook fa-3x" aria-hidden="true"></i>
                    </a>
                </li>
                <li>
                    <a href="https://twitter.com/chsakellsBlog" target="_blank">
                        <i class="fa fa-twitter fa-3x" aria-hidden="true"></i>
                    </a>
                </li>
                <li>
                    <a href="https://github.com/chsakell" target="_blank">
                        <i class="fa fa-github fa-3x" aria-hidden="true"></i>
                    </a>
                </li>
                <li>
                    <a href="https://chsakell.com" target="_blank">
                        <i class="fa fa-rss-square fa-3x" aria-hidden="true"></i>
                    </a>
                </li>
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>
<br/>
<!-- Page Content -->
<div class="container">
    <router-outlet></router-outlet>
</div>
<footer class="navbar navbar-fixed-bottom">
    <div class="text-center">
        <h4 class="white">
            <a href="https://chsakell.com/" target="_blank">chsakell's Blog</a>
            <i>Anything around ASP.NET MVC,Web API, WCF, Entity Framework & Angular</i>
        </h4>
    </div>
</footer>
<ng2-slim-loading-bar></ng2-slim-loading-bar>

Shared services & interfaces

Before implementing the Users and Schedules features we ‘ll create any service or interface is going to be used across the app. Create a folder named shared under the app and add the interfaces.ts TypeScript file.

export interface IUser {
    id: number;
    name: string;
    avatar: string;
    profession: string;
    schedulesCreated: number;
}

export interface ISchedule {
     id: number;
     title: string;
     description: string;
     timeStart: Date;
     timeEnd: Date;
     location: string;
     type: string;
     status: string;
     dateCreated: Date;
     dateUpdated: Date;
     creator: string;
     creatorId: number;
     attendees: number[];
}

export interface IScheduleDetails {
     id: number;
     title: string;
     description: string;
     timeStart: Date;
     timeEnd: Date;
     location: string;
     type: string;
     status: string;
     dateCreated: Date;
     dateUpdated: Date;
     creator: string;
     creatorId: number;
     attendees: IUser[];
     statuses: string[];
     types: string[];
}

export interface Pagination {
    CurrentPage : number;
    ItemsPerPage : number;
    TotalItems : number;
    TotalPages: number;
}

export class PaginatedResult<T> {
    result :  T;
    pagination : Pagination;
}

export interface Predicate<T> {
    (item: T): boolean
}

In case you have read the Building REST APIs using .NET and Entity Framework Core you will be aware with most of the classes defined on the previous file. They are the TypeScript models that matches the API’s ViewModels. The last interface that I defined is my favorite one. The Predicate interface is a predicate which allows us to pass generic predicates in TypeScript functions. For example we ‘ll see later on the following function.

removeItems<T>(array: Array<T>, predicate: Predicate<T>) {
    _.remove(array, predicate);
}

This is extremely powerfull. What this function can do? It can remove any item from an array that fulfills a certain predicate. Assuming that you have an array of type IUser and you want to remove any user item that has id<0 you would write..

this.itemsService.removeItems<IUser>(this.users, x => x.id < 0);

Add a pipes folder under shared and create a DateTime related pipe.

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'dateFormat'
})

export class DateFormatPipe implements PipeTransform {
    transform(value: any, args: any[]): any {

        if (args && args[0] === 'local') {
            return new Date(value).toLocaleString();
        }
        else if (value) {
            return new Date(value);
        }
        return value;
    }
}

The pipe simply converts a date to a JavaScript datetime that Angular understands. It can be used either inside an html template..

{{schedule.timeStart | dateFormat | date:'medium'}}

.. or programatically..

this.scheduleDetails.timeStart = new DateFormatPipe().transform(schedule.timeStart, ['local'])

We proceed with the directives. Add a folder named directives under shared. The first one is a simple one that toggles the background color of an element when the mouse enters or leaves. It ‘s very similar to the one described at official’s Angular’s website.

import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
    selector: '[highlight]'
 })
export class HighlightDirective {
    private _defaultColor = 'beige';
    private el: HTMLElement;

    constructor(el: ElementRef) {
        this.el = el.nativeElement;
    }

    @Input('highlight') highlightColor: string;

    @HostListener('mouseenter') onMouseEnter() {
        this.highlight(this.highlightColor || this._defaultColor);
    }
    @HostListener('mouseleave') onMouseLeave() {
        this.highlight(null);
    }

    private highlight(color: string) {
        this.el.style.backgroundColor = color;
    }
}

The second one though is an exciting one. The home page has a carousel with each slide having a font-awesome icon on its left.
angular-crud-modal-animation-02
The thing is that when you reduce the width of the browser the font-image moves on top giving a bad user experience.
angular-crud-modal-animation-03
What I want is the font-awesome icon to hide when the browser reaches a certain width and more over I want this width to be customizable. I believe I have just opened the gates for responsive web design using Angular 2.. Add the following MobileHide directive in a mobile-hide.directive.ts file under shared/directives folder.

import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
    selector: '[mobileHide]',
    host: {
        '(window:resize)': 'onResize($event)'
    }
 })
export class MobileHideDirective {
    private _defaultMaxWidth: number = 768;
    private el: HTMLElement;

    constructor(el: ElementRef) {
        this.el = el.nativeElement;
    }

    @Input('mobileHide') mobileHide: number;

    onResize(event:Event) {
        var window : any = event.target;
        var currentWidth = window.innerWidth;
        if(currentWidth < (this.mobileHide || this._defaultMaxWidth))
        {
            this.el.style.display = 'none';
        }
        else
        {
            this.el.style.display = 'block';
        }
    }
}

What this directive does is bind to window.resize event and when triggered check browser’s width: if width is less that the one defined or the default one then hides the element, otherwise shows it. You can apply this directive on the dom like this.

<div mobileHide="772" class="col-md-2 col-sm-2 col-xs-12">
   <span class="fa-stack fa-4x">
    <i class="fa fa-square fa-stack-2x text-primary"></i>
    <i class="fa fa-code fa-stack-1x fa-inverse" style="color:#FFC107"></i>
   </span>
</div>

The div element will be hidden when browser’s width is less than 772px..
angular-scheduler-spa-02
You can extend this directive by creating a new Input parameter which represents a class and instead of hiding the element apply a different class!

Shared services

@Injectable() services that are going to be used across many components in our application will also be placed inside the shared folder. We will separate them though in two different types, core and utilities. Add two folders named services and utils under the shared folder. We will place all core services under services and utilities under utitlities. The most important core service in our SPA is the one responsible to send HTTP requests to the API, the DataService. Add the data.service.ts under the services folder.

import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
//Grab everything with import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

import { IUser, ISchedule, IScheduleDetails, Pagination, PaginatedResult } from '../interfaces';
import { ItemsService } from '../utils/items.service';
import { ConfigService } from '../utils/config.service';

@Injectable()
export class DataService {

    _baseUrl: string = '';

    constructor(private http: Http,
        private itemsService: ItemsService,
        private configService: ConfigService) {
        this._baseUrl = configService.getApiURI();
    }

    getUsers(): Observable<IUser[]> {
        return this.http.get(this._baseUrl + 'users')
            .map((res: Response) => {
                return res.json();
            })
            .catch(this.handleError);
    }

    getUserSchedules(id: number): Observable<ISchedule[]> {
        return this.http.get(this._baseUrl + 'users/' + id + '/schedules')
            .map((res: Response) => {
                return res.json();
            })
            .catch(this.handleError);
    }

    createUser(user: IUser): Observable<IUser> {

        let headers = new Headers();
        headers.append('Content-Type', 'application/json');

        return this.http.post(this._baseUrl + 'users/', JSON.stringify(user), {
            headers: headers
        })
            .map((res: Response) => {
                return res.json();
            })
            .catch(this.handleError);
    }

    updateUser(user: IUser): Observable<void> {

        let headers = new Headers();
        headers.append('Content-Type', 'application/json');

        return this.http.put(this._baseUrl + 'users/' + user.id, JSON.stringify(user), {
            headers: headers
        })
            .map((res: Response) => {
                return;
            })
            .catch(this.handleError);
    }

    deleteUser(id: number): Observable<void> {
        return this.http.delete(this._baseUrl + 'users/' + id)
            .map((res: Response) => {
                return;
            })
            .catch(this.handleError);
    }

    getSchedules(page?: number, itemsPerPage?: number): Observable<PaginatedResult<ISchedule[]>> {
        var peginatedResult: PaginatedResult<ISchedule[]> = new PaginatedResult<ISchedule[]>();

        let headers = new Headers();
        if (page != null && itemsPerPage != null) {
            headers.append('Pagination', page + ',' + itemsPerPage);
        }

        return this.http.get(this._baseUrl + 'schedules', {
            headers: headers
        })
            .map((res: Response) => {
                console.log(res.headers.keys());
                peginatedResult.result = res.json();

                if (res.headers.get("Pagination") != null) {
                    //var pagination = JSON.parse(res.headers.get("Pagination"));
                    var paginationHeader: Pagination = this.itemsService.getSerialized<Pagination>(JSON.parse(res.headers.get("Pagination")));
                    console.log(paginationHeader);
                    peginatedResult.pagination = paginationHeader;
                }
                return peginatedResult;
            })
            .catch(this.handleError);
    }

    getSchedule(id: number): Observable<Schedule> {
        return this.http.get(this._baseUrl + 'schedules/' + id)
            .map((res: Response) => {
                return res.json();
            })
            .catch(this.handleError);
    }

    getScheduleDetails(id: number): Observable<IScheduleDetails> {
        return this.http.get(this._baseUrl + 'schedules/' + id + '/details')
            .map((res: Response) => {
                return res.json();
            })
            .catch(this.handleError);
    }

    updateSchedule(schedule: ISchedule): Observable<void> {

        let headers = new Headers();
        headers.append('Content-Type', 'application/json');

        return this.http.put(this._baseUrl + 'schedules/' + schedule.id, JSON.stringify(schedule), {
            headers: headers
        })
            .map((res: Response) => {
                return;
            })
            .catch(this.handleError);
    }

    deleteSchedule(id: number): Observable<void> {
        return this.http.delete(this._baseUrl + 'schedules/' + id)
            .map((res: Response) => {
                return;
            })
            .catch(this.handleError);
    }

    deleteScheduleAttendee(id: number, attendee: number) {

        return this.http.delete(this._baseUrl + 'schedules/' + id + '/removeattendee/' + attendee)
            .map((res: Response) => {
                return;
            })
            .catch(this.handleError);
    }

    private handleError(error: any) {
        var applicationError = error.headers.get('Application-Error');
        var serverError = error.json();
        var modelStateErrors: string = '';

        if (!serverError.type) {
            console.log(serverError);
            for (var key in serverError) {
                if (serverError[key])
                    modelStateErrors += serverError[key] + '\n';
            }
        }

        modelStateErrors = modelStateErrors = '' ? null : modelStateErrors;

        return Observable.throw(applicationError || modelStateErrors || 'Server error');
    }
}

The service implements several CRUD operations targeting the API we have built on a previous post. It uses the ConfigService in order to get the API’s URI and the ItemsService to parse JSON objects to typed ones (we ‘ll see it later). Another important function that this service provides is the handleError which can read response errors either from the ModelState or the Application-Error header. The simplest util service is the ConfigService which has only one method to get the API’s URI. Add it under the utils folder.

import { Injectable } from '@angular/core';

@Injectable()
export class ConfigService {

    _apiURI : string;

    constructor() {
        this._apiURI = 'http://localhost:5000/api/';
     }

     getApiURI() {
         return this._apiURI;
     }

     getApiHost() {
         return this._apiURI.replace('api/','');
     }
}

Make sure to change this URI to reflect your back-end API’s URI. It’ s going to be different when you host the API from the console using the dotnet run command and different when you run the application through Visual Studio. The most interesting util service is the ItemsService. I don’t know any client-side application that doesn’t have to deal with array of items and that’s why we need that service. Let’s view the code first. Add it under the utils folder.

import { Injectable } from '@angular/core';
import { Predicate } from '../interfaces'

import * as _ from 'lodash';

@Injectable()
export class ItemsService {

    constructor() { }

    /*
    Removes an item from an array using the lodash library
    */
    removeItemFromArray<T>(array: Array<T>, item: any) {
        _.remove(array, function (current) {
            //console.log(current);
            return JSON.stringify(current) === JSON.stringify(item);
        });
    }

    removeItems<T>(array: Array<T>, predicate: Predicate<T>) {
        _.remove(array, predicate);
    }

    /*
    Finds a specific item in an array using a predicate and repsaces it
    */
    setItem<T>(array: Array<T>, predicate: Predicate<T>, item: T) {
        var _oldItem = _.find(array, predicate);
        if(_oldItem){
            var index = _.indexOf(array, _oldItem);
            array.splice(index, 1, item);
        } else {
            array.push(item);
        }
    }

    /*
    Adds an item to zero index
    */
    addItemToStart<T>(array: Array<T>, item: any) {
        array.splice(0, 0, item);
    }

    /*
    From an array of type T, select all values of type R for property
    */
    getPropertyValues<T,R>(array: Array<T>, property : string) : R
    {
        var result = _.map(array, property);
        return <R><any>result;
    }

    /*
    Util method to serialize a string to a specific Type
    */
    getSerialized<T>(arg: any): T {
        return <T>JSON.parse(JSON.stringify(arg));
    }
}

We can see extensive use of TypeScript in compination with the lodash library. All those functions are used inside the app so you will be able to see how they actually work. Let’s view though some examples right now. The setItem(array: Array, predicate: Predicate, item: T) method can replace a certain item in a typed array of T. For example if there is an array of type IUser that has a user item with id=-1 and you need to replace it with a new IUser, you can simply write..

this.itemsService.setItem<IUser>(this.users, (u) => u.id == -1, _user);

Here we passed the array of IUser, the predicate which is what items to be replaced and the preplacement new item value. Continue by adding the NotificationService and the MappingService which are pretty much self-explanatory, under the utils folder.

import { Injectable } from '@angular/core';
import { Predicate } from '../interfaces'

declare var alertify: any;

@Injectable()
export class NotificationService {
    private _notifier: any = alertify;

    constructor() { }

    /*
    Opens a confirmation dialog using the alertify.js lib
    */
    openConfirmationDialog(message: string, okCallback: () => any) {
        this._notifier.confirm(message, function (e) {
            if (e) {
                okCallback();
            } else {
            }
        });
    }

    /*
    Prints a success message using the alertify.js lib
    */
    printSuccessMessage(message: string) {

        this._notifier.success(message);
    }

    /*
    Prints an error message using the alertify.js lib
    */
    printErrorMessage(message: string) {
        this._notifier.error(message);
    }
}
import { Injectable } from '@angular/core';

import { ISchedule, IScheduleDetails, IUser } from '../interfaces';
import  { ItemsService } from './items.service'

@Injectable()
export class MappingService {

    constructor(private itemsService : ItemsService) { }

    mapScheduleDetailsToSchedule(scheduleDetails: IScheduleDetails): ISchedule {
        var schedule: ISchedule = {
            id: scheduleDetails.id,
            title: scheduleDetails.title,
            description: scheduleDetails.description,
            timeStart: scheduleDetails.timeStart,
            timeEnd: scheduleDetails.timeEnd,
            location: scheduleDetails.location,
            type: scheduleDetails.type,
            status: scheduleDetails.status,
            dateCreated: scheduleDetails.dateCreated,
            dateUpdated: scheduleDetails.dateUpdated,
            creator: scheduleDetails.creator,
            creatorId: scheduleDetails.creatorId,
            attendees: this.itemsService.getPropertyValues<IUser, number[]>(scheduleDetails.attendees, 'id')
        }

        return schedule;
    }

}

Features

Time to implement the SPA’s features starting from the simplest one, the HomeComponent which is responsible to render a landing page. Add a folder named home under app and create the HomeComponent in a home.component.ts file.

import { Component, OnInit, trigger, state, style, animate, transition } from '@angular/core';

import { MobileHideDirective } from '../shared/directives/mobile-hide.directive';

declare let componentHandler: any;

@Component({
    moduleId: module.id,
    templateUrl: 'home.component.html',
    directives: [MobileHideDirective],
    animations: [
        trigger('flyInOut', [
            state('in', style({ opacity: 1, transform: 'translateX(0)' })),
            transition('void => *', [
                style({
                    opacity: 0,
                    transform: 'translateX(-100%)'
                }),
                animate('0.6s ease-in')
            ]),
            transition('* => void', [
                animate('0.2s 10 ease-out', style({
                    opacity: 0,
                    transform: 'translateX(100%)'
                }))
            ])
        ])
    ]
})
export class HomeComponent {

    constructor() {

    }
}

Despite that this is the simplest component in our SPA it still make use of some interesting Angular features. The first one is the Angular animations and the second is the the MobileHideDirective directive we created before in order to hide the font-awesome icons when browser’s width is less than 772px. The animation will make the template appear from left to right. Let’s view the template’s code and a preview of what the animation looks like.

<div @flyInOut="'in'">

    <div class="container content">
        <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
            <!-- Indicators -->
            <ol class="carousel-indicators">
                <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
                <li data-target="#carousel-example-generic" data-slide-to="1"></li>
                <li data-target="#carousel-example-generic" data-slide-to="2"></li>
            </ol>
            <!-- Wrapper for slides -->
            <div class="carousel-inner">
                <div class="item active">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="thumbnail adjust1">
                                <div mobileHide="772" class="col-md-2 col-sm-2 col-xs-12">
                                    <span class="fa-stack fa-4x">
                                      <i class="fa fa-square fa-stack-2x text-primary"></i>
                                      <i class="fa fa-html5 fa-stack-1x fa-inverse" style="color:#FFC107"></i>
                                    </span>
                                </div>
                                <div class="col-md-10 col-sm-10 col-xs-12">
                                    <div class="caption">
                                        <p class="text-info lead adjust2">ASP.NET Core</p>
                                        <p><span class="glyphicon glyphicon-thumbs-up"></span> ASP.NET Core is a new open-source
                                            and cross-platform framework for building modern cloud based internet connected
                                            applications, such as web apps, IoT apps and mobile backends.</p>
                                        <blockquote class="adjust2">
                                            <p>Microsoft Corp.</p> <small><cite title="Source Title"><i class="glyphicon glyphicon-globe"></i> https://docs.asp.net/en/latest/</cite></small>                                            </blockquote>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="item">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="thumbnail adjust1">
                                <div mobileHide="772" class="col-md-2 col-sm-2 col-xs-12">
                                    <span class="fa-stack fa-4x">
                                        <i class="fa fa-square fa-stack-2x text-primary"></i>
                                        <i class="fa fa-code fa-stack-1x fa-inverse" style="color:#FFC107"></i>
                                    </span>
                                </div>
                                <div class="col-md-10 col-sm-10 col-xs-12">
                                    <div class="caption">
                                        <p class="text-info lead adjust2">Angular 2</p>
                                        <p><span class="glyphicon glyphicon-thumbs-up"></span> Learn one way to build applications
                                            with Angular and reuse your code and abilities to build apps for any deployment
                                            target. For web, mobile web, native mobile and native desktop.</p>
                                        <blockquote class="adjust2">
                                            <p>Google</p> <small><cite title="Source Title"><i class="glyphicon glyphicon-globe"></i>https://angular.io/</cite></small>                                            </blockquote>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="item">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="thumbnail adjust1">
                                <div mobileHide="772" class="col-md-2 col-sm-2 col-xs-12">
                                    <span class="fa-stack fa-4x">
                                      <i class="fa fa-square fa-stack-2x text-primary"></i>
                                      <i class="fa fa-rss fa-stack-1x fa-inverse" style="color:#FFC107"></i>
                                    </span>
                                </div>
                                <div class="col-md-10 col-sm-10 col-xs-12">
                                    <div class="caption">
                                        <p class="text-info lead adjust2">chsakell's Blog</p>
                                        <p><span class="glyphicon glyphicon-thumbs-up"></span> Anything around ASP.NET MVC,Web
                                            API, WCF, Entity Framework & Angular.</p>
                                        <blockquote class="adjust2">
                                            <p>Chris Sakellarios</p> <small><cite title="Source Title"><i class="glyphicon glyphicon-globe"></i> https://chsakell.com</cite></small>                                            </blockquote>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Controls -->
            <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a>
            <a class="right carousel-control" href="#carousel-example-generic" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a>
        </div>
    </div>

    <hr>
    <!-- Title -->
    <div class="row">
        <div class="col-lg-12">
            <h3>Latest Features</h3>
        </div>
    </div>
    <!-- /.row -->
    <!-- Page Features -->
    <div class="row text-center">
        <div class="col-md-3 col-sm-6 hero-feature">
            <div class="thumbnail">
                <span class="fa-stack fa-5x">
                    <i class="fa fa-square fa-stack-2x text-primary"></i>
                    <i class="fa fa-html5 fa-stack-1x fa-inverse"></i>
                </span>
                <div class="caption">
                    <h3>ASP.NET Core</h3>
                    <p>ASP.NET Core is a significant redesign of ASP.NET.</p>
                    <p>
                        <a href="https://docs.asp.net/en/latest/" target="_blank" class="btn btn-primary">More..</a>
                    </p>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 hero-feature">
            <div class="thumbnail">
                <span class="fa-stack fa-5x">
                    <i class="fa fa-square fa-stack-2x text-primary"></i>
                    <i class="fa fa-database fa-stack-1x fa-inverse"></i>
                </span>
                <div class="caption">
                    <h3>EF Core</h3>
                    <p>A cross-platform version of Entity Framework.</p>
                    <p>
                        <a href="https://docs.efproject.net/en/latest/" target="_blank" class="btn btn-primary">More..</a>
                    </p>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 hero-feature">
            <div class="thumbnail">
                <span class="fa-stack fa-5x">
                    <i class="fa fa-square fa-stack-2x text-primary"></i>
                    <i class="fa fa-code fa-stack-1x fa-inverse"></i>
                </span>
                <div class="caption">
                    <h3>Angular</h3>
                    <p>Angular is a platform for building mobile and desktop web apps.</p>
                    <p>
                        <a href="https://angular.io/" target="_blank" class="btn btn-primary">More..</a>
                    </p>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 hero-feature">
            <div class="thumbnail">
                <span class="fa-stack fa-5x">
                    <i class="fa fa-square fa-stack-2x text-primary"></i>
                    <i class="fa fa-terminal fa-stack-1x fa-inverse"></i>
                </span>
                <div class="caption">
                    <h3>TypeScript</h3>
                    <p>A free and open source programming language.</p>
                    <p>
                        <a href="https://www.typescriptlang.org/" target="_blank" class="btn btn-primary">More..</a>
                    </p>
                </div>
            </div>
        </div>
    </div>
    <!-- /.row -->
    <hr>
    <!-- Footer -->
    <footer>
        <div class="row">
            <div class="col-lg-12">
                <p>Copyright &copy; <a href="https://chsakell.com" target="_blank">chsakell's Blog</a></p>
            </div>
        </div>
    </footer>
</div>

angular-scheduler-spa-03
Add a folder named Schedules. This feature will have two distinct routes, one to display all the schedules in a table and another one to edit a specific schedule. Add a schedule.routes.ts file under schedules folder.

import { RouterConfig }          from '@angular/router';

import { ScheduleListComponent } from './schedule-list.component';
import { ScheduleEditComponent } from './schedule-edit.component';

export const ScheduleRoutes: RouterConfig = [
    { path: 'schedules', component: ScheduleListComponent },
    { path: 'schedules/:id/edit', component: ScheduleEditComponent }
];

Now you can see where the ScheduleRoutes route configuration came from, in the app.routes.ts file. The ScheduleListComponent is a quite complex one. Add the schedule-list.component.ts under schedules as well.

import { Component, OnInit, ViewChild, Input, Output,
    trigger,
    state,
    style,
    animate,
    transition } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';

import {SlimLoadingBarService} from 'ng2-slim-loading-bar/ng2-slim-loading-bar';
import { MODAL_DIRECTIVES, ModalComponent } from 'ng2-bs3-modal/ng2-bs3-modal';
import {PAGINATION_DIRECTIVES, PaginationComponent } from 'ng2-bootstrap';

import { DataService } from '../shared/services/data.service';
import { ItemsService } from '../shared/utils/items.service';
import { NotificationService } from '../shared/utils/notification.service';
import { ConfigService } from '../shared/utils/config.service';
import { ISchedule, IScheduleDetails, Pagination, PaginatedResult } from '../shared/interfaces';
import { DateFormatPipe } from '../shared/pipes/date-format.pipe';

@Component({
    moduleId: module.id,
    selector: 'app-schedules',
    templateUrl: 'schedule-list.component.html',
    directives: [ROUTER_DIRECTIVES, MODAL_DIRECTIVES, PAGINATION_DIRECTIVES],
    pipes: [DateFormatPipe],
    animations: [
        trigger('flyInOut', [
            state('in', style({ opacity: 1, transform: 'translateX(0)' })),
            transition('void => *', [
                style({
                    opacity: 0,
                    transform: 'translateX(-100%)'
                }),
                animate('0.5s ease-in')
            ]),
            transition('* => void', [
                animate('0.2s 10 ease-out', style({
                    opacity: 0,
                    transform: 'translateX(100%)'
                }))
            ])
        ])
    ]
})
export class ScheduleListComponent implements OnInit {

    schedules: ISchedule[];
    apiHost: string;

    public itemsPerPage: number = 2;
    public totalItems: number = 0;
    public currentPage: number = 1;

    // Modal properties
    @ViewChild('modal')
    modal: ModalComponent;
    items: string[] = ['item1', 'item2', 'item3'];
    selected: string;
    output: string;
    selectedScheduleId: number;
    scheduleDetails: IScheduleDetails;
    selectedScheduleLoaded: boolean = false;
    index: number = 0;
    backdropOptions = [true, false, 'static'];
    animation: boolean = true;
    keyboard: boolean = true;
    backdrop: string | boolean = true;

    constructor(private slimLoader: SlimLoadingBarService,
        private dataService: DataService,
        private itemsService: ItemsService,
        private notificationService: NotificationService,
        private configService: ConfigService) { }

    ngOnInit() {
        this.apiHost = this.configService.getApiHost();
        this.loadSchedules();
    }

    loadSchedules() {
        this.slimLoader.start();

        this.dataService.getSchedules(this.currentPage, this.itemsPerPage)
            .subscribe((res: PaginatedResult<ISchedule[]>) => {
                this.schedules = res.result;// schedules;
                this.totalItems = res.pagination.TotalItems;
                this.slimLoader.complete();
            },
            error => {
                this.slimLoader.complete();
                this.notificationService.printErrorMessage('Failed to load schedules. ' + error);
            });
    }

    pageChanged(event: any): void {
        this.currentPage = event.page;
        this.loadSchedules();
        //console.log('Page changed to: ' + event.page);
        //console.log('Number items per page: ' + event.itemsPerPage);
    };

    removeSchedule(schedule: ISchedule) {
        this.notificationService.openConfirmationDialog('Are you sure you want to delete this schedule?',
            () => {
                this.slimLoader.start();
                this.dataService.deleteSchedule(schedule.id)
                    .subscribe(() => {
                        this.itemsService.removeItemFromArray<ISchedule>(this.schedules, schedule);
                        this.notificationService.printSuccessMessage(schedule.title + ' has been deleted.');
                        this.slimLoader.complete();
                    },
                    error => {
                        this.slimLoader.complete();
                        this.notificationService.printErrorMessage('Failed to delete ' + schedule.title + ' ' + error);
                    });
            });
    }

    viewScheduleDetails(id: number) {
        this.selectedScheduleId = id;
        this.modal.open('lg');
        console.log('test');
    }

    closed() {
        this.output = '(closed) ' + this.selected;
    }

    dismissed() {
        this.output = '(dismissed)';
    }

    opened() {
        this.slimLoader.start();
        this.dataService.getScheduleDetails(this.selectedScheduleId)
            .subscribe((schedule: IScheduleDetails) => {
                this.scheduleDetails = this.itemsService.getSerialized<IScheduleDetails>(schedule);
                // Convert date times to readable format
                this.scheduleDetails.timeStart = new DateFormatPipe().transform(schedule.timeStart, ['local']);
                this.scheduleDetails.timeEnd = new DateFormatPipe().transform(schedule.timeEnd, ['local']);
                this.slimLoader.complete();
                this.selectedScheduleLoaded = true;
            },
            error => {
                this.slimLoader.complete();
                this.notificationService.printErrorMessage('Failed to load schedule. ' + error);
            });

        this.output = '(opened)';
    }
}

Firstly, the component loads the schedules passing the current page and the number of items per page on the service call. The PaginatedResult response, contains the items plus the pagination information. The component uses PAGINATION_DIRECTIVES and PaginationComponent modules from ng2-bootstrap to render a pagination bar under the schedules table..

 <pagination [boundaryLinks]="true" [totalItems]="totalItems" [itemsPerPage]="itemsPerPage" [(ngModel)]="currentPage" class="pagination-sm"
        previousText="&lsaquo;" nextText="&rsaquo;" firstText="&laquo;" lastText="&raquo;" (pageChanged)="pageChanged($event)"></pagination>

The next important feature on this component is the custom modal popup it uses to display schedule’s details. It makes use of the MODAL_DIRECTIVES and ModalComponent modules from ng2-bs3-modal. This plugin requires that you place a modal directive in your template and bind the model properties you wish to display on its template body. You also need to use the @ViewChild(‘modal’) for this to work. Let’s view the entire schedule-list.component.html template and a small preview.


<button class="btn btn-primary" type="button" *ngIf="schedules">
   <i class="fa fa-calendar" aria-hidden="true"></i> Schedules
   <span class="badge">{{totalItems}}</span>
</button>

<hr/>

<div  @flyInOut="'in'">
    <table class="table table-hover">
        <thead>
            <tr>
                <th><i class="fa fa-text-width fa-2x" aria-hidden="true"></i>Title</th>
                <th><i class="fa fa-user fa-2x" aria-hidden="true"></i>Creator</th>
                <th><i class="fa fa-paragraph fa-2x" aria-hidden="true"></i>Description</th>
                <th><i class="fa fa-map-marker fa-2x" aria-hidden="true"></i></th>
                <th><i class="fa fa-calendar-o fa-2x" aria-hidden="true"></i>Time Start</th>
                <th><i class="fa fa-calendar-o fa-2x" aria-hidden="true"></i>Time End</th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor="let schedule of schedules">
                <td> {{schedule.title}}</td>
                <td>{{schedule.creator}}</td>
                <td>{{schedule.description}}</td>
                <td>{{schedule.location}}</td>
                <td>{{schedule.timeStart | dateFormat | date:'medium'}}</td>
                <td>{{schedule.timeEnd | dateFormat | date:'medium'}}</td>
                <td><button class="btn btn-primary" (click)="viewScheduleDetails(schedule.id)">
            <i class="fa fa-info-circle" aria-hidden="true"></i>Details</button>
                </td>
                <td><a class="btn btn-primary" [routerLink]="['/schedules',schedule.id,'edit']"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>Edit</a></td>
                <td>
                    <button class="btn btn-danger" (click)="removeSchedule(schedule)"><i class="fa fa-trash" aria-hidden="true"></i>Delete</button>
                </td>
            </tr>
        </tbody>
    </table>

    <pagination [boundaryLinks]="true" [totalItems]="totalItems" [itemsPerPage]="itemsPerPage" [(ngModel)]="currentPage" class="pagination-sm"
        previousText="&lsaquo;" nextText="&rsaquo;" firstText="&laquo;" lastText="&raquo;" (pageChanged)="pageChanged($event)"></pagination>
</div>
<modal [animation]="animation" [keyboard]="keyboard" [backdrop]="backdrop" (onClose)="closed()" (onDismiss)="dismissed()"
    (onOpen)="opened()" #modal>
    <modal-header [show-close]="true">
        <h4 class="modal-title" *ngIf="selectedScheduleLoaded">{{scheduleDetails.title}} details</h4>
    </modal-header>
    <modal-body *ngIf="selectedScheduleLoaded">
        <form method="post">
            <div class="form-group">
                <div class="row">
                    <div class="col-md-4">
                        <label class="control-label"><i class="fa fa-user" aria-hidden="true"></i>Creator</label>
                        <input type="text" class="form-control" [(ngModel)]="scheduleDetails.creator" disabled />
                    </div>

                    <div class="col-md-4">
                        <label class="control-label"><i class="fa fa-text-width" aria-hidden="true"></i>Title</label>
                        <input type="text" class="form-control" [(ngModel)]="scheduleDetails.title" disabled />
                    </div>

                    <div class="col-md-4">
                        <label class="control-label"><i class="fa fa-paragraph" aria-hidden="true"></i>Description</label>
                        <input type="text" class="form-control" [(ngModel)]="scheduleDetails.description" disabled />
                    </div>
                </div>
            </div>

            <div class="form-group">
                <div class="row">
                    <div class="col-xs-6">
                        <label class="control-label"><i class="fa fa-calendar-o" aria-hidden="true"></i>Time Start</label>
                        <input type="text" class="form-control" [(ngModel)]="scheduleDetails.timeStart" disabled />
                    </div>

                    <div class="col-xs-6">
                        <label class="control-label"><i class="fa fa-calendar-check-o" aria-hidden="true"></i>Time End</label>
                        <input type="text" class="form-control" [(ngModel)]="scheduleDetails.timeEnd" disabled />
                    </div>
                </div>
            </div>

            <div class="form-group">
                <div class="row">
                    <div class="col-md-4">
                        <label class="control-label"><i class="fa fa-map-marker" aria-hidden="true"></i>Location</label>
                        <input type="text" class="form-control" [(ngModel)]="scheduleDetails.location" disabled />
                    </div>

                    <div class="col-md-4 selectContainer">
                        <label class="control-label"><i class="fa fa-spinner" aria-hidden="true"></i>Status</label>
                        <input type="text" class="form-control" [(ngModel)]="scheduleDetails.status" disabled />
                    </div>
                    <div class="col-md-4 selectContainer">
                        <label class="control-label"><i class="fa fa-tag" aria-hidden="true"></i>Type</label>
                        <input type="text" class="form-control" [(ngModel)]="scheduleDetails.type" disabled />
                    </div>
                </div>
            </div>
            <hr/>
            <div class="panel panel-info">
                <!-- Default panel contents -->
                <div class="panel-heading">Attendes</div>

                <!-- Table -->
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th></th>
                            <th><i class="fa fa-user" aria-hidden="true"></i>Name</th>
                            <th><i class="fa fa-linkedin-square" aria-hidden="true"></i>Profession</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr *ngFor="let attendee of scheduleDetails.attendees">
                            <td [style.valign]="'middle'">
                                <img class="img-thumbnail img-small" src="{{apiHost}}images/{{attendee.avatar}}" alt="attendee.name" />
                            </td>
                            <td [style.valign]="'middle'">{{attendee.name}}</td>
                            <td [style.valign]="'middle'">{{attendee.profession}}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </form>
    </modal-body>
    <modal-footer [show-default-buttons]="false">
        <button class="btn btn-danger btn-sm pull-right" (click)="modal.close()">
            <i class="fa fa-times" aria-hidden="true"></i>Dismiss</button>
    </modal-footer>
</modal>

angular-scheduler-spa-04
The ScheduleEditComponent is responsible to edit the details of a single Schedule. The interface used for this component is the IScheduleDetails which encapsulates all schedule’s details (creator, attendees, etc..). Add the schedule-edit.component.ts file under the schedules folder.

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { FORM_DIRECTIVES, NgForm } from '@angular/common';

import { DataService } from '../shared/services/data.service';
import { ItemsService } from '../shared/utils/items.service';
import { NotificationService } from '../shared/utils/notification.service';
import { ConfigService } from '../shared/utils/config.service';
import { MappingService } from '../shared/utils/mapping.service';
import { ISchedule, IScheduleDetails, IUser } from '../shared/interfaces';
import { DateFormatPipe } from '../shared/pipes/date-format.pipe';

import {SlimLoadingBarService} from 'ng2-slim-loading-bar/ng2-slim-loading-bar';
import { NKDatetime } from 'ng2-datetime/ng2-datetime';

@Component({
    moduleId: module.id,
    selector: 'app-schedule-edit',
    templateUrl: 'schedule-edit.component.html',
    directives: [NKDatetime, FORM_DIRECTIVES],
    providers: [MappingService],
    pipes: [DateFormatPipe]
})
export class ScheduleEditComponent implements OnInit {
    apiHost: string;
    id: number;
    schedule: IScheduleDetails;
    scheduleLoaded: boolean = false;
    statuses: string[];
    types: string[];
    private sub: any;

    constructor(private route: ActivatedRoute,
        private router: Router,
        private dataService: DataService,
        private itemsService: ItemsService,
        private notificationService: NotificationService,
        private configService: ConfigService,
        private mappingService: MappingService,
        private slimLoader: SlimLoadingBarService) { }

    ngOnInit() {
        // (+) converts string 'id' to a number
	    this.id = +this.route.snapshot.params['id'];
        this.apiHost = this.configService.getApiHost();
        this.loadScheduleDetails();
    }

    loadScheduleDetails() {
        this.slimLoader.start();
        this.dataService.getScheduleDetails(this.id)
            .subscribe((schedule: IScheduleDetails) => {
                this.schedule = this.itemsService.getSerialized<IScheduleDetails>(schedule);
                this.scheduleLoaded = true;
                // Convert date times to readable format
                this.schedule.timeStart = new Date(this.schedule.timeStart.toString()); // new DateFormatPipe().transform(schedule.timeStart, ['local']);
                this.schedule.timeEnd = new Date(this.schedule.timeEnd.toString()); //new DateFormatPipe().transform(schedule.timeEnd, ['local']);
                this.statuses = this.schedule.statuses;
                this.types = this.schedule.types;

                this.slimLoader.complete();
            },
            error => {
                this.slimLoader.complete();
                this.notificationService.printErrorMessage('Failed to load schedule. ' + error);
            });
    }

    updateSchedule(editScheduleForm: NgForm) {
        console.log(editScheduleForm.value);

        var scheduleMapped = this.mappingService.mapScheduleDetailsToSchedule(this.schedule);

        this.slimLoader.start();
        this.dataService.updateSchedule(scheduleMapped)
            .subscribe(() => {
                this.notificationService.printSuccessMessage('Schedule has been updated');
                this.slimLoader.complete();
            },
            error => {
                this.slimLoader.complete();
                this.notificationService.printErrorMessage('Failed to update schedule. ' + error);
            });
    }

    removeAttendee(attendee: IUser) {
        this.notificationService.openConfirmationDialog('Are you sure you want to remove '
            + attendee.name + ' from this schedule?',
            () => {
                this.slimLoader.start();
                this.dataService.deleteScheduleAttendee(this.schedule.id, attendee.id)
                    .subscribe(() => {
                        this.itemsService.removeItemFromArray<IUser>(this.schedule.attendees, attendee);
                        this.notificationService.printSuccessMessage(attendee.name + ' will not attend the schedule.');
                        this.slimLoader.complete();
                    },
                    error => {
                        this.slimLoader.complete();
                        this.notificationService.printErrorMessage('Failed to remove ' + attendee.name + ' ' + error);
                    });
            });
    }

    back() {
        this.router.navigate(['/schedules']);
    }

}

I have highlighted only the Form related modules imports because the interesting part about this component is the validations it carries on the template schedule-edit.component.html.

<form #editScheduleForm="ngForm" *ngIf="scheduleLoaded" (ngSubmit)="updateSchedule(editScheduleForm)" novalidate>

    <div class="alert alert-danger" [hidden]="editScheduleForm.valid">
        <ul *ngIf="creator.dirty && !creator.valid">
            <li *ngIf="creator.errors.required">Creator name is required</li>
            <li *ngIf="creator.errors.pattern">Creator name should have 5-50 characters</li>
        </ul>
        <ul *ngIf="title.dirty && !title.valid">
            <li *ngIf="title.errors.required">Title is required</li>
            <li *ngIf="title.errors.pattern">Title should have 5-20 characters</li>
        </ul>
        <ul *ngIf="description.dirty && !description.valid">
            <li *ngIf="description.errors.required">Description is required</li>
            <li *ngIf="description.errors.pattern">Description should have at least 10 characters</li>
        </ul>
        <ul *ngIf="location.dirty && !location.valid">
            <li *ngIf="location.errors.required">Location is required</li>
        </ul>
    </div>

    <button type="button" class="btn btn-danger" (click)="back()">
        <i class="fa fa-arrow-circle-left" aria-hidden="true"></i>Back</button>
    <button type="submit" [disabled]="!editScheduleForm.valid" class="btn btn-default">
        <i class="fa fa-pencil-square-o" aria-hidden="true"></i>Update</button>

    <hr/>

    <div class="form-group">
        <div class="row">
            <div class="col-md-4">
                <label class="control-label"><i class="fa fa-user" aria-hidden="true"></i>Creator</label>
                <input type="text" class="form-control" [(ngModel)]="schedule.creator" ngControl="creator" #creator="ngForm" required pattern=".{5,50}"
                />
            </div>

            <div class="col-md-4">
                <label class="control-label"><i class="fa fa-text-width" aria-hidden="true"></i>Title</label>
                <input type="text" class="form-control" [(ngModel)]="schedule.title" ngControl="title" #title="ngForm" required pattern=".{5,20}"
                />
            </div>

            <div class="col-md-4">
                <label class="control-label"><i class="fa fa-paragraph" aria-hidden="true"></i>Description</label>
                <input type="text" class="form-control" [(ngModel)]="schedule.description" ngControl="description" #description="ngForm"
                    required pattern=".{10,}" />
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="row">
            <div class="col-xs-6">
                <label class="control-label"><i class="fa fa-calendar-o" aria-hidden="true"></i>Time Start</label>
                <datetime [(ngModel)]="schedule.timeStart" [timepicker]="{icon: 'fa fa-clock-o'}" [datepicker]="{icon: 'fa fa-calendar', autoclose : true, orientation : 'bottom'}"
                    ngControl="timeStart" #timeStart="ngForm"></datetime>
                <!--<input type="text" class="form-control" [(ngModel)]="schedule.timeStart" required />-->
            </div>

            <div class="col-xs-6">
                <label class="control-label"><i class="fa fa-calendar-check-o" aria-hidden="true"></i>Time End</label>
                <!--<input type="text" class="form-control" [(ngModel)]="schedule.timeEnd" required />-->
                <datetime [(ngModel)]="schedule.timeEnd" [timepicker]="{icon: 'fa fa-clock-o'}" [datepicker]="{icon: 'fa fa-calendar', autoclose : true, orientation : 'bottom' }"
                    ngControl="timeEnd" #timeEnd="ngForm"></datetime>
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="row">
            <div class="col-md-4">
                <label class="control-label"><i class="fa fa-map-marker" aria-hidden="true"></i>Location</label>
                <input type="text" class="form-control" [(ngModel)]="schedule.location" ngControl="location" #location="ngForm" required
                />
            </div>

            <div class="col-md-4 selectContainer">
                <label class="control-label"><i class="fa fa-spinner" aria-hidden="true"></i>Status</label>
                <select class="form-control" [ngModel]="schedule.status">
                    <option *ngFor="let status of statuses" [value]="status">{{status}}</option>
                </select>
            </div>
            <div class="col-md-4 selectContainer">
                <label class="control-label"><i class="fa fa-tag" aria-hidden="true"></i>Type</label>
                <select class="form-control" [ngModel]="schedule.type">
                    <option *ngFor="let type of types" [value]="type">{{type}}</option>
                </select>
            </div>
        </div>
    </div>
    <hr/>
    <div class="panel panel-info">
        <!-- Default panel contents -->
        <div class="panel-heading">Attendes</div>

        <!-- Table -->
        <table class="table table-hover">
            <thead>
                <tr>
                    <th></th>
                    <th><i class="fa fa-user" aria-hidden="true"></i>Name</th>
                    <th><i class="fa fa-linkedin-square" aria-hidden="true"></i>Profession</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let attendee of schedule.attendees">
                    <td [style.valign]="'middle'">
                        <img class="img-thumbnail img-small" src="{{apiHost}}images/{{attendee.avatar}}" alt="attendee.name" />
                    </td>
                    <td [style.valign]="'middle'">{{attendee.name}}</td>
                    <td [style.valign]="'middle'">{{attendee.profession}}</td>
                    <td [style.valign]="'middle'">
                        <button type="button" class="btn btn-danger btn-sm" (click)="removeAttendee(attendee)"><i class="fa fa-user-times" aria-hidden="true"></i>Remove</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</form>

angular-crud-modal-animation-04
Don’t forget that we have also set server-side validations, so if you try to edit a schedule and set the start time to be greater than the end time you should receive an error that was encapsulated by the server in the response message, either on the header or the body.
angular-crud-modal-animation-05
The Users feature is an interesting one as well. I have decided on this one to display each user as a card element instead of using a table. This required to create a user-card custom element which encapsulates all the logic not only for rendering but also manipulating user’s data (CRUD ops..). Add a folder named Users under app and create the UserCardComponent.

import { Component, Input, Output, OnInit, ViewContainerRef, EventEmitter, ViewChild,
    trigger,
    state,
    style,
    animate,
    transition  } from '@angular/core';

import {SlimLoadingBarService} from 'ng2-slim-loading-bar/ng2-slim-loading-bar';
import { MODAL_DIRECTIVES, ModalComponent } from 'ng2-bs3-modal/ng2-bs3-modal';

import { IUser, ISchedule } from '../shared/interfaces';
import { DataService } from '../shared/services/data.service';
import { ItemsService } from '../shared/utils/items.service';
import { NotificationService } from '../shared/utils/notification.service';
import { ConfigService } from '../shared/utils/config.service';
import { DateFormatPipe } from '../shared/pipes/date-format.pipe';
import { HighlightDirective } from '../shared/directives/highlight.directive';

@Component({
    moduleId: module.id,
    selector: 'user-card',
    templateUrl: 'user-card.component.html',
    directives: [MODAL_DIRECTIVES, HighlightDirective],
    pipes: [DateFormatPipe],
    animations: [
        trigger('flyInOut', [
            state('in', style({ opacity: 1, transform: 'translateX(0)' })),
            transition('void => *', [
                style({
                    opacity: 0,
                    transform: 'translateX(-100%)'
                }),
                animate('0.5s ease-in')
            ]),
            transition('* => void', [
                animate('0.2s 10 ease-out', style({
                    opacity: 0,
                    transform: 'translateX(100%)'
                }))
            ])
        ])
    ]
})
export class UserCardComponent implements OnInit {

    @Input() user: IUser;
    @Output() removeUser = new EventEmitter();
    @Output() userCreated = new EventEmitter();

    edittedUser: IUser;
    onEdit: boolean = false;
    apiHost: string;
    // Modal properties
    @ViewChild('modal')
    modal: ModalComponent;
    items: string[] = ['item1', 'item2', 'item3'];
    selected: string;
    output: string;
    userSchedules: ISchedule[];
    userSchedulesLoaded: boolean = false;
    index: number = 0;
    backdropOptions = [true, false, 'static'];
    animation: boolean = true;
    keyboard: boolean = true;
    backdrop: string | boolean = true;

    constructor(private itemsService: ItemsService,
        private notificationService: NotificationService,
        private slimLoader: SlimLoadingBarService,
        private dataService: DataService,
        private configService: ConfigService) { }

    ngOnInit() {
        this.apiHost = this.configService.getApiHost();
        this.edittedUser = this.itemsService.getSerialized<IUser>(this.user);
        if (this.user.id < 0)
            this.editUser();
    }

    editUser() {
        this.onEdit = !this.onEdit;
        this.edittedUser = this.itemsService.getSerialized<IUser>(this.user);
        // <IUser>JSON.parse(JSON.stringify(this.user)); // todo Utils..
    }

    createUser() {
        this.slimLoader.start();
        this.dataService.createUser(this.edittedUser)
            .subscribe((userCreated) => {
                this.user = this.itemsService.getSerialized<IUser>(userCreated);
                this.edittedUser = this.itemsService.getSerialized<IUser>(this.user);
                this.onEdit = false;

                this.userCreated.emit({ value: userCreated });
                this.slimLoader.complete();
            },
            error => {
                this.notificationService.printErrorMessage('Failed to created user');
                this.notificationService.printErrorMessage(error);
                this.slimLoader.complete();
            });
    }

    updateUser() {
        this.slimLoader.start();
        this.dataService.updateUser(this.edittedUser)
            .subscribe(() => {
                this.user = this.edittedUser;
                this.onEdit = !this.onEdit;
                this.notificationService.printSuccessMessage(this.user.name + ' has been updated');
                this.slimLoader.complete();
            },
            error => {
                this.notificationService.printErrorMessage('Failed to edit user');
                this.notificationService.printErrorMessage(error);
                this.slimLoader.complete();
            });
    }

    openRemoveModal() {
        this.notificationService.openConfirmationDialog('Are you sure you want to remove '
            + this.user.name + '?',
            () => {
                this.slimLoader.start();
                this.dataService.deleteUser(this.user.id)
                    .subscribe(
                    res => {
                        this.removeUser.emit({
                            value: this.user
                        });
                        this.slimLoader.complete();
                        this.slimLoader.complete();
                    }, error => {
                        this.notificationService.printErrorMessage(error);
                        this.slimLoader.complete();
                    })
            });
    }

    viewSchedules(user: IUser) {
        console.log(user);
        this.modal.open('lg');
    }

    closed() {
        this.output = '(closed) ' + this.selected;
    }

    dismissed() {
        this.output = '(dismissed)';
    }

    opened() {
        this.slimLoader.start();
        this.dataService.getUserSchedules(this.edittedUser.id)
            .subscribe((schedules: ISchedule[]) => {
                this.userSchedules = schedules;
                console.log(this.userSchedules);
                this.userSchedulesLoaded = true;
                this.slimLoader.complete();
            },
            error => {
                this.slimLoader.complete();
                this.notificationService.printErrorMessage('Failed to load users. ' + error);
            });
        this.output = '(opened)';
    }

    isUserValid(): boolean {
        return !(this.edittedUser.name.trim() === "")
            && !(this.edittedUser.profession.trim() === "");
    }

}

The logic about the modal and the animations should be familiar to you at this point. The new feature to notice on this component are the @Input() and @Output() properties. The first one is used so that the host component which is the UserListComponent pass the user item foreach user in a array of IUser items. The two @Output() properties are required so that a user-card can inform the host component that something happend, in our case that a user created or removed. Why? It’s a matter of Separation of Concerns. The list of users is maintained by the UserListComponent and a single UserCardComponent knows nothing about it. That’s why when something happens the UserListComponent needs to be informed and update the user list respectively. Here’s the user-card.component.html.

<div class="panel panel-primary" [ngClass]="{shadowCard: onEdit}" @flyInOut="'in'">
    <div class="panel-heading">
        <h3 class="panel-title pull-left" [class.hidden]="onEdit"><i class="fa fa-user" aria-hidden="true"></i>{{edittedUser.name}}</h3>
        <input [(ngModel)]="edittedUser.name" [class.hidden]="!onEdit" [style.color]="'brown'" required class="form-control" />
        <div class="clearfix"></div>
    </div>

    <div highlight="whitesmoke" class="panel-body">
        <div class="">
            <img src="{{apiHost}}images/{{edittedUser.avatar}}" class="img-avatar" alt="">
            <div class="caption">
                <p>
                    <span [class.hidden]="onEdit">{{edittedUser.profession}}</span>
                </p>
                <p [hidden]="!onEdit">
                    <input [(ngModel)]="edittedUser.profession" class="form-control" required />
                </p>
                <p>
                    <button class="btn btn-primary" (click)="viewSchedules(edittedUser)" [disabled]="edittedUser.schedulesCreated === 0">
                    <i class="fa fa-calendar-check-o" aria-hidden="true"></i> Schedules <span class="badge">
                        {{edittedUser.schedulesCreated}}</span>
                    </button>
                </p>
            </div>
        </div>
    </div>
    <div class="panel-footer">
        <div [class.hidden]="edittedUser.id < 0">
            <button class="btn btn-default btn-xs" (click)="editUser()">
                <i class="fa fa-pencil" aria-hidden="true"></i>
                    {{onEdit === false ? "Edit" : "Cancel"}}
                </button>
            <button class="btn btn-default btn-xs" [class.hidden]="!onEdit" (click)="updateUser()" [disabled]="!isUserValid()">
                <i class="fa fa-pencil-square-o" aria-hidden="true"></i>Update</button>
            <button class="btn btn-danger btn-xs" (click)="openRemoveModal()">
                <i class="fa fa-times" aria-hidden="true"></i>Remove</button>
        </div>
        <div [class.hidden]="!(edittedUser.id < 0)">
            <button class="btn btn-default btn-xs" [class.hidden]="!onEdit" (click)="createUser()" [disabled]="!isUserValid()">
                <i class="fa fa-plus" aria-hidden="true"></i>Create</button>
        </div>
    </div>
</div>

<modal [animation]="animation" [keyboard]="keyboard" [backdrop]="backdrop" (onClose)="closed()" (onDismiss)="dismissed()"
    (onOpen)="opened()" #modal>
    <modal-header [show-close]="true">
        <h4 class="modal-title">{{edittedUser.name}} schedules created</h4>
    </modal-header>
    <modal-body>
        <table class="table table-hover" *ngIf="userSchedulesLoaded">
            <thead>
                <tr>
                    <th>Title</th>
                    <th>Description</th>
                    <th>Place</th>
                    <th>Time Start</th>
                    <th>Time End</th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let schedule of userSchedules">
                    <td> {{schedule.title}}</td>
                    <td>{{schedule.description}}</td>
                    <td>{{schedule.location}}</td>
                    <td>{{schedule.timeStart | dateFormat | date:'medium'}}</td>
                    <td>{{schedule.timeEnd | dateFormat | date:'medium'}}</td>
                </tr>
            </tbody>
        </table>
    </modal-body>
    <modal-footer [show-default-buttons]="false">
        <button class="btn btn-danger btn-sm pull-right" (click)="modal.close()">
            <i class="fa fa-times" aria-hidden="true"></i>Dismiss</button>
    </modal-footer>
</modal>

Also add the user-list.component.ts and notice the usage of the ItemsService for manipulating items.

import { Component, OnInit } from '@angular/core';

import {SlimLoadingBarService} from 'ng2-slim-loading-bar/ng2-slim-loading-bar';

import { DataService } from '../shared/services/data.service';
import { ItemsService } from '../shared/utils/items.service';
import { NotificationService } from '../shared/utils/notification.service';
import { IUser } from '../shared/interfaces';
import { UserCardComponent } from './user-card.component';

@Component({
    moduleId: module.id,
    selector: 'users',
    templateUrl: 'user-list.component.html',
    directives: [UserCardComponent]
})
export class UserListComponent implements OnInit {

    users: IUser[];
    addingUser: boolean = false;

    constructor(private dataService: DataService,
        private itemsService: ItemsService,
        private notificationService: NotificationService,
        private slimLoader: SlimLoadingBarService) { }

    ngOnInit() {
        this.slimLoader.start();
        this.dataService.getUsers()
            .subscribe((users: IUser[]) => {
                this.users = users;
                this.slimLoader.complete();
            },
            error => {
                this.slimLoader.complete();
                this.notificationService.printErrorMessage('Failed to load users. ' + error);
            });
    }

    removeUser(user: any) {
        var _user: IUser = this.itemsService.getSerialized<IUser>(user.value);
        this.itemsService.removeItemFromArray<IUser>(this.users, _user);
        // inform user
        this.notificationService.printSuccessMessage(_user.name + ' has been removed');
    }

    userCreated(user: any) {
        var _user: IUser = this.itemsService.getSerialized<IUser>(user.value);
        this.addingUser = false;
        // inform user
        this.notificationService.printSuccessMessage(_user.name + ' has been created');
        console.log(_user.id);
        this.itemsService.setItem<IUser>(this.users, (u) => u.id == -1, _user);
        // todo fix user with id:-1
    }

    addUser() {
        this.addingUser = true;
        var newUser = { id: -1, name: '', avatar: 'avatar_05.png', profession: '', schedulesCreated: 0 };
        this.itemsService.addItemToStart<IUser>(this.users, newUser);
        //this.users.splice(0, 0, newUser);
    }

    cancelAddUser() {
        this.addingUser = false;
        this.itemsService.removeItems<IUser>(this.users, x => x.id < 0);
    }
}

The removeUser and userCreated are the events triggered from child UserCardComponent components. When those events are triggered, the action has already finished in API/Database level and what remains is to update the client-side list. Here’s the template for the UserListComponent.

<button [class.hidden]="addingUser" class="btn btn-primary" (click)="addUser()">
    <i class="fa fa-user-plus fa-2x" aria-hidden="true"></i>Add</button>
<button [class.hidden]="!addingUser" class="btn btn-danger" (click)="cancelAddUser()">
    <i class="fa fa-ban fa-2x" aria-hidden="true"></i>Cancel</button>

<hr/>

<div class="row text-center">
    <div class="col-md-3 col-sm-6 hero-feature" *ngFor="let user of users">
        <user-card [user]="user" (removeUser)="removeUser($event);" (userCreated)="userCreated($event);"></user-card>
    </div>
</div>

The SPA uses some custom stylesheet styles.css which you can find here. Add it in a new folder named assets/styles under the root of the application. At this point you should be able to run the SPA. Make sure you have set the API first and configure the API’s endpoint in the ConfigService to point it properly. Fire the app by running the following command.

npm start

Conclusion

That’s it we have finished! We have seen many Angular 2 features on this SPA but I believe the more exciting one was how TypeScript can ease client-side development. We saw typed predicates, array manipulation using lodash and last but not least how to install and use 3rd party libraries in our app using SystemJS.

Source Code: You can find the source code for this project here where you will also find instructions on how to run the application.

In case you find my blog’s content interesting, register your email to receive notifications of new posts and follow chsakell’s Blog on its Facebook or Twitter accounts.

Facebook Twitter
.NET Web Application Development by Chris S.
facebook twitter-small


Viewing all articles
Browse latest Browse all 43

Trending Articles