WIP - Change email form

This commit is contained in:
Matthew Ross 2016-07-22 06:28:01 -04:00
parent 06c0dcf93d
commit 42ec6959c3
4 changed files with 85 additions and 36 deletions

View File

@ -53,7 +53,7 @@
<label for="username" class="hidden">New Username</label> <label for="username" class="hidden">New Username</label>
<input type="text" id="username" name="username" <input type="text" id="username" name="username"
placeholder="New Username" placeholder="New Username"
[(ngModel)]="changeUsername.newName" > [(ngModel)]="changeUsername.newName">
<button type="submit" [disabled]="changeUsername.submitted" <button type="submit" [disabled]="changeUsername.submitted"
(click)="updateUsername()">Change Username</button> (click)="updateUsername()">Change Username</button>
@ -65,10 +65,14 @@
<h3 class="tall">Change Email</h3> <h3 class="tall">Change Email</h3>
<label for="email" class="hidden">New Email</label> <label for="email" class="hidden">New Email</label>
<input type="text" id="email" placeholder="New Email"> <input type="text" id="email" name="email"
placeholder="New Email - Blank to disable"
[(ngModel)]="changeEmail.newEmail">
<button type="submit">Change Email</button> <button type="submit" [disabled]="changeEmail.submitted"
<button class="flat">Reset</button> (click)="updateEmail()">Change Email</button>
<button class="flat" [disabled]="changeEmail.submitted"
(click)="resetEmailForm()">Reset</button>
</form> </form>
<div class="hold-bottom"> <div class="hold-bottom">

View File

@ -1,6 +1,11 @@
import { Component } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { UserSettingsService } from './user-settings.service'; import { UserSettingsService } from './user-settings.service';
import {
PassForm,
UsernameForm,
EmailForm
} from './user-settings.models';
import { import {
AuthService, AuthService,
NotificationsService, NotificationsService,
@ -9,32 +14,24 @@ import {
ApiResponse ApiResponse
} from '../../shared/index'; } from '../../shared/index';
interface PassForm {
current: string;
newPass: string;
verPass: string;
submitted: boolean;
};
interface UsernameForm {
newName: string;
submitted: boolean;
};
@Component({ @Component({
selector: 'tb-user-settings', selector: 'tb-user-settings',
templateUrl: 'app/settings/user-settings/user-settings.component.html', templateUrl: 'app/settings/user-settings/user-settings.component.html',
providers: [ UserSettingsService ] providers: [ UserSettingsService ]
}) })
export class UserSettings { export class UserSettings implements OnInit {
private user: User; private user: User;
private changePassword: PassForm; private changePassword: PassForm;
private changeUsername: UsernameForm; private changeUsername: UsernameForm;
private changeEmail: EmailForm
constructor(private auth: AuthService, constructor(private auth: AuthService,
private notes: NotificationsService, private notes: NotificationsService,
private userService: UserSettingsService) { private userService: UserSettingsService) {
auth.userChanged.subscribe(user => this.user = user); auth.userChanged.subscribe(user => this.user = user);
}
ngOnInit() {
this.resetForms(); this.resetForms();
} }
@ -49,10 +46,7 @@ export class UserSettings {
.changePassword(this.changePassword.current, .changePassword(this.changePassword.current,
this.changePassword.newPass) this.changePassword.newPass)
.subscribe((response: ApiResponse) => { .subscribe((response: ApiResponse) => {
response.alerts.forEach(msg => { this.addAlerts(response.alerts);
this.notes.add(new Notification(msg.type, msg.text));
});
this.resetPasswordForm(); this.resetPasswordForm();
this.changePassword.submitted = false; this.changePassword.submitted = false;
}); });
@ -72,34 +66,57 @@ export class UserSettings {
this.userService this.userService
.changeUsername(this.changeUsername.newName) .changeUsername(this.changeUsername.newName)
.subscribe((response: ApiResponse) => { .subscribe((response: ApiResponse) => {
response.alerts.forEach(msg => { this.addAlerts(response.alerts);
this.notes.add(new Notification(msg.type, msg.text));
});
this.resetUsernameForm(); this.resetUsernameForm();
this.changeUsername.submitted = false; this.changeUsername.submitted = false;
}); });
} }
updateEmail() {
this.changeEmail.submitted = true;
let emailRegex = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
let match = this.changeEmail.newEmail.match(emailRegex);
if (!match && this.changeEmail.newEmail !== '') {
this.notes.add(new Notification('error',
'Invalid email address.'));
this.changeEmail.submitted = false;
return;
}
this.userService
.changeEmail(this.changeEmail.newEmail)
.subscribe((response: ApiResponse) => {
this.addAlerts(response.alerts);
this.resetEmailForm();
this.changeEmail.submitted = false;
});
}
resetPasswordForm() { resetPasswordForm() {
this.changePassword = { this.changePassword = new PassForm();
current: '',
newPass: '',
verPass: '',
submitted: false
};
} }
resetUsernameForm() { resetUsernameForm() {
this.changeUsername = { this.changeUsername = new UsernameForm();
newName: '', }
submitted: false
}; resetEmailForm() {
this.changeEmail = new EmailForm(this.user.email);
}
private addAlerts(alerts: Array<Notification>) {
alerts.forEach(msg => {
this.notes.add(msg);
});
} }
private resetForms() { private resetForms() {
this.resetPasswordForm(); this.resetPasswordForm();
this.resetUsernameForm(); this.resetUsernameForm();
this.resetEmailForm();
} }
private validatePassForm() { private validatePassForm() {

View File

@ -0,0 +1,18 @@
export class PassForm {
constructor(public current: string = '', public newPass: string = '',
public verPass: string = '', public submitted: boolean = false) {
}
};
export class UsernameForm {
constructor(public newName: string = '',
public submitted: boolean = false) {
}
};
export class EmailForm {
constructor(public newEmail: string = '',
public submitted: boolean = false) {
}
};

View File

@ -62,5 +62,15 @@ export class UserSettingsService {
return Observable.of(response); return Observable.of(response);
}); });
} }
changeEmail(newEmail: string): Observable<ApiResponse> {
let json = JSON.stringify(this.activeUser);
return this.http.post('api/users/' + this.activeUser.id, json)
.map(res => {
let response: ApiResponse = res.json();
return response;
});
}
} }