Skip to content

Commit

Permalink
better code formatting and indenting for chapter 8
Browse files Browse the repository at this point in the history
  • Loading branch information
deeleman committed Mar 17, 2016
1 parent 994098a commit 10ef8fa
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 102 deletions.
47 changes: 23 additions & 24 deletions chapter_08/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from 'angular2/core';
import { TimerComponent } from './timer/timer';
import { TASKS_DIRECTIVES, TasksComponent, TaskEditorComponent } from './tasks/tasks';
import { TasksComponent, TaskEditorComponent } from './tasks/tasks';
import { SHARED_PROVIDERS, SHARED_DIRECTIVES, AuthenticationService } from './shared/shared';
import { HTTP_PROVIDERS } from 'angular2/http';
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, Router } from 'angular2/router';
Expand Down Expand Up @@ -34,34 +34,33 @@ import { LoginComponent } from './login/login';
</nav>
<pomodoro-router-outlet protectedPath="tasks/editor" loginUrl="login">
</pomodoro-router-outlet>
`
`
})
@RouteConfig([
{ path: '', name: 'Home', redirectTo: ['TasksComponent'] },
{ path: 'tasks', name: 'TasksComponent', component: TasksComponent, useAsDefault: true },
{ path: 'tasks/editor', name: 'TaskEditorComponent', component: TaskEditorComponent },
{ path: 'timer/...', name: 'TimerComponent', component: TimerComponent },
{ path: 'login', name: 'LoginComponent', component: LoginComponent }
{ path: '', name: 'Home', redirectTo: ['TasksComponent'] },
{ path: 'tasks', name: 'TasksComponent', component: TasksComponent, useAsDefault: true },
{ path: 'tasks/editor', name: 'TaskEditorComponent', component: TaskEditorComponent },
{ path: 'timer/...', name: 'TimerComponent', component: TimerComponent },
{ path: 'login', name: 'LoginComponent', component: LoginComponent }
])
export default class AppComponent {
userIsLogged: boolean;
userIsLogged: boolean;

constructor(
private authenticationService: AuthenticationService,
private router: Router)
{
authenticationService.userLoggedInStatus.subscribe(userIsloggedIn => {
this.userIsLogged = userIsloggedIn;
});
}
constructor(
private authenticationService: AuthenticationService,
private router: Router) {
authenticationService.userLoggedInStatus.subscribe(userIsloggedIn => {
this.userIsLogged = userIsloggedIn;
});
}

logout($event): void {
$event.preventDefault();
logout($event): void {
$event.preventDefault();

this.authenticationService.logout().then(success => {
if(success) {
this.router.navigateByUrl('/');
}
});
}
this.authenticationService.logout().then(success => {
if (success) {
this.router.navigateByUrl('/');
}
});
}
}
84 changes: 42 additions & 42 deletions chapter_08/app/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,50 @@ import { Router } from 'angular2/router';
import { AuthenticationService } from '../shared/shared';

@Component({
selector: 'pomodoro-login',
templateUrl: 'app/login/login.component.html'
selector: 'pomodoro-login',
templateUrl: 'app/login/login.component.html'
})
export default class LoginComponent {
loginForm: ControlGroup;
showAlert: boolean = false;
showUsernameHint: boolean = false;

constructor(
formBuilder: FormBuilder,
private router: Router,
private authenticationService: AuthenticationService) {
this.loginForm = formBuilder.group({
username: ['', Validators.compose([Validators.required, this.usernameValidator])],
password: ['', Validators.required]
});

const username = this.loginForm.controls['username'];
username.valueChanges.subscribe(value => {
this.showUsernameHint = (username.dirty && value.indexOf('@') < 0);
});

}

private usernameValidator(control: Control): {[key: string]: boolean} {
if(!/(.+)@(.+){2,}\.(.+){2,}/.test(control.value)) {
return {
'emailNotValid': true
};
}

return null;
loginForm: ControlGroup;
showAlert: boolean = false;
showUsernameHint: boolean = false;

constructor(
formBuilder: FormBuilder,
private router: Router,
private authenticationService: AuthenticationService) {
this.loginForm = formBuilder.group({
username: ['', Validators.compose([Validators.required, this.usernameValidator])],
password: ['', Validators.required]
});

const username = this.loginForm.controls['username'];
username.valueChanges.subscribe(value => {
this.showUsernameHint = (username.dirty && value.indexOf('@') < 0);
});

}

private usernameValidator(control: Control): { [key: string]: boolean } {
if (!/(.+)@(.+){2,}\.(.+){2,}/.test(control.value)) {
return {
'emailNotValid': true
};
}

authenticate() {
this.showAlert = !this.loginForm.valid && this.loginForm.dirty;
let credentials: any = this.loginForm.value;

this.authenticationService.login(credentials).then(success => {
if(success) {
this.router.navigateByUrl('/');
} else {
this.showAlert = true;
}
});
}
return null;
}

authenticate() {
this.showAlert = !this.loginForm.valid && this.loginForm.dirty;
let credentials: any = this.loginForm.value;

this.authenticationService.login(credentials).then(success => {
if (success) {
this.router.navigateByUrl('/');
} else {
this.showAlert = true;
}
});
}
}
58 changes: 29 additions & 29 deletions chapter_08/app/shared/services/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@ import { Injectable, EventEmitter } from 'angular2/core';

@Injectable()
export default class AuthenticationService {
userLoggedInStatus: EventEmitter<boolean>;
userLoggedInStatus: EventEmitter<boolean>;

constructor() {
this.userLoggedInStatus = new EventEmitter();
}
constructor() {
this.userLoggedInStatus = new EventEmitter();
}

login({ username, password }): Promise<boolean> {
return new Promise(resolve => {
let validCredentials: boolean = false;
login({ username, password }): Promise<boolean> {
return new Promise(resolve => {
let validCredentials: boolean = false;

// @NOTE: In a normal case scenario this check should
// be performed against a web service, which would return
// the session token upon validating the user successfully
if (username === '[email protected]' &&
password === 'letmein') {
validCredentials = true;
window.sessionStorage.setItem('token', 'eyJhbGciOi');
}
// @NOTE: In a normal case scenario this check should
// be performed against a web service, which would return
// the session token upon validating the user successfully
if (username === '[email protected]' &&
password === 'letmein') {
validCredentials = true;
window.sessionStorage.setItem('token', 'eyJhbGciOi');
}

this.userLoggedInStatus.emit(validCredentials);
resolve(validCredentials);
});
}
this.userLoggedInStatus.emit(validCredentials);
resolve(validCredentials);
});
}

logout(): Promise<boolean> {
return new Promise(resolve => {
window.sessionStorage.removeItem('token');
this.userLoggedInStatus.emit(false);
resolve(true);
});
}
logout(): Promise<boolean> {
return new Promise(resolve => {
window.sessionStorage.removeItem('token');
this.userLoggedInStatus.emit(false);
resolve(true);
});
}

static grantAccess(): boolean {
return !!window.sessionStorage.getItem('token');
}
static grantAccess(): boolean {
return !!window.sessionStorage.getItem('token');
}
}
4 changes: 2 additions & 2 deletions chapter_08/app/timer/timer-widget.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Input, OnInit } from 'angular2/core';
import { Component, OnInit } from 'angular2/core';
import { RouteParams, CanReuse, OnReuse } from 'angular2/router';
import { SettingsService, TaskService } from '../shared/shared';

Expand All @@ -16,7 +16,7 @@ import { SettingsService, TaskService } from '../shared/shared';
</p>
</div>`
})
export default class TimerWidgetComponent {
export default class TimerWidgetComponent implements OnInit, CanReuse, OnReuse {
minutes: number;
seconds: number;
isPaused: boolean;
Expand Down
10 changes: 5 additions & 5 deletions chapter_08/app/timer/timer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { RouteConfig, ROUTER_DIRECTIVES } from 'angular2/router';
import TimerWidgetComponent from './timer-widget.component';

@Component({
selector: 'pomodoro-timer',
directives: [ROUTER_DIRECTIVES],
template: '<router-outlet></router-outlet>'
selector: 'pomodoro-timer',
directives: [ROUTER_DIRECTIVES],
template: '<router-outlet></router-outlet>'
})
@RouteConfig([
{ path: '/task/:id', name: 'TaskTimer', component: TimerWidgetComponent },
{ path: '/', name: 'GenericTimer', component: TimerWidgetComponent, useAsDefault: true }
{ path: '/task/:id', name: 'TaskTimer', component: TimerWidgetComponent },
{ path: '/', name: 'GenericTimer', component: TimerWidgetComponent, useAsDefault: true }
])
export default class TimerComponent {}

0 comments on commit 10ef8fa

Please sign in to comment.