diff --git a/src/app/settings/user-settings/user-settings.component.html b/src/app/settings/user-settings/user-settings.component.html
index 2917139..9ed3269 100644
--- a/src/app/settings/user-settings/user-settings.component.html
+++ b/src/app/settings/user-settings/user-settings.component.html
@@ -53,7 +53,7 @@
+ [(ngModel)]="changeUsername.newName">
@@ -65,10 +65,14 @@
diff --git a/src/app/settings/user-settings/user-settings.component.ts b/src/app/settings/user-settings/user-settings.component.ts
index 8a128ad..2f401c6 100644
--- a/src/app/settings/user-settings/user-settings.component.ts
+++ b/src/app/settings/user-settings/user-settings.component.ts
@@ -1,6 +1,11 @@
-import { Component } from '@angular/core';
+import { Component, OnInit } from '@angular/core';
import { UserSettingsService } from './user-settings.service';
+import {
+ PassForm,
+ UsernameForm,
+ EmailForm
+} from './user-settings.models';
import {
AuthService,
NotificationsService,
@@ -9,32 +14,24 @@ import {
ApiResponse
} from '../../shared/index';
-interface PassForm {
- current: string;
- newPass: string;
- verPass: string;
- submitted: boolean;
-};
-
-interface UsernameForm {
- newName: string;
- submitted: boolean;
-};
-
@Component({
selector: 'tb-user-settings',
templateUrl: 'app/settings/user-settings/user-settings.component.html',
providers: [ UserSettingsService ]
})
-export class UserSettings {
+export class UserSettings implements OnInit {
private user: User;
private changePassword: PassForm;
private changeUsername: UsernameForm;
+ private changeEmail: EmailForm
constructor(private auth: AuthService,
private notes: NotificationsService,
private userService: UserSettingsService) {
auth.userChanged.subscribe(user => this.user = user);
+ }
+
+ ngOnInit() {
this.resetForms();
}
@@ -49,10 +46,7 @@ export class UserSettings {
.changePassword(this.changePassword.current,
this.changePassword.newPass)
.subscribe((response: ApiResponse) => {
- response.alerts.forEach(msg => {
- this.notes.add(new Notification(msg.type, msg.text));
- });
-
+ this.addAlerts(response.alerts);
this.resetPasswordForm();
this.changePassword.submitted = false;
});
@@ -72,34 +66,57 @@ export class UserSettings {
this.userService
.changeUsername(this.changeUsername.newName)
.subscribe((response: ApiResponse) => {
- response.alerts.forEach(msg => {
- this.notes.add(new Notification(msg.type, msg.text));
- });
-
+ this.addAlerts(response.alerts);
this.resetUsernameForm();
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() {
- this.changePassword = {
- current: '',
- newPass: '',
- verPass: '',
- submitted: false
- };
+ this.changePassword = new PassForm();
}
resetUsernameForm() {
- this.changeUsername = {
- newName: '',
- submitted: false
- };
+ this.changeUsername = new UsernameForm();
+ }
+
+ resetEmailForm() {
+ this.changeEmail = new EmailForm(this.user.email);
+ }
+
+ private addAlerts(alerts: Array
) {
+ alerts.forEach(msg => {
+ this.notes.add(msg);
+ });
}
private resetForms() {
this.resetPasswordForm();
this.resetUsernameForm();
+ this.resetEmailForm();
}
private validatePassForm() {
diff --git a/src/app/settings/user-settings/user-settings.models.ts b/src/app/settings/user-settings/user-settings.models.ts
new file mode 100644
index 0000000..750dbc4
--- /dev/null
+++ b/src/app/settings/user-settings/user-settings.models.ts
@@ -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) {
+ }
+};
+
diff --git a/src/app/settings/user-settings/user-settings.service.ts b/src/app/settings/user-settings/user-settings.service.ts
index 3afa5ec..ca1f837 100644
--- a/src/app/settings/user-settings/user-settings.service.ts
+++ b/src/app/settings/user-settings/user-settings.service.ts
@@ -62,5 +62,15 @@ export class UserSettingsService {
return Observable.of(response);
});
}
+
+ changeEmail(newEmail: string): Observable {
+ 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;
+ });
+ }
}