Ajout d'une page de mise à jour du

profile utilisateur
This commit is contained in:
Sébastien André
2020-04-28 11:06:51 +02:00
parent 8f09a0d225
commit 43c5f640e8
8 changed files with 2017 additions and 869 deletions

View File

@@ -12,8 +12,10 @@
<h2 style="display: inline;">{{ title }}</h2>
</div>
<div>
<h2 style="display: inline;" *ngIf="currentUser">{{ this.currentUser.firstName }}
{{ this.currentUser.lastName }}</h2>
<a routerLink="/user" routerLinkActive="active" skipLocationChange>
<h2 style="display: inline;" *ngIf="currentUser">{{ this.currentUser.firstName }}
{{ this.currentUser.lastName }}</h2>
</a>
<a *ngIf="currentUser" (click)="logout()" style="cursor: pointer; margin-left:15px;">Logout</a>
</div>
</div>

View File

@@ -55,6 +55,7 @@ import { CachingInterceptor } from "../interceptor/caching.interceptor";
//import { BasicAuthInterceptor } from '../interceptor/basic-auth.interceptor';
import { JwtAuthInterceptor } from "../interceptor/jwt-auth.interceptor";
import { ErrorInterceptor } from "../interceptor/error.interceptor";
import { UserProfileComponent } from "./user-profile/user-profile.component";
const appRoutes: Routes = [
{ path: "", component: DefaultComponent, canActivate: [AuthGuardService] },
@@ -94,6 +95,11 @@ const appRoutes: Routes = [
component: ListOfGearsComponent,
canActivate: [AuthGuardService],
},
{
path: "user",
component: UserProfileComponent,
canActivate: [AuthGuardService],
},
{ path: "login", component: LoginComponent },
@@ -118,6 +124,7 @@ const appRoutes: Routes = [
LoginComponent,
CreateUserComponent,
LoginUserComponent,
UserProfileComponent,
],
imports: [
RouterModule.forRoot(

View File

@@ -0,0 +1,41 @@
<form [formGroup]="userForm" (ngSubmit)="onSubmit(userForm.value)">
<p>
<mat-form-field>
<mat-label>Login</mat-label>
<input matInput type="text" formControlName="login" />
</mat-form-field>
</p>
<p>
<mat-form-field>
<mat-label>Firstname</mat-label>
<input matInput type="text" formControlName="firstName" />
</mat-form-field>
</p>
<p>
<mat-form-field>
<mat-label>Lastname</mat-label>
<input matInput type="text" formControlName="lastName" />
</mat-form-field>
</p>
<p>
<mat-form-field>
<mat-label>E-mail</mat-label>
<input matInput type="text" formControlName="email" />
</mat-form-field>
</p>
<p>
<mat-form-field>
<mat-label>Current password</mat-label>
<input matInput type="text" formControlName="currentPassword" />
</mat-form-field>
</p>
<p>
<mat-form-field>
<mat-label>New password</mat-label>
<input matInput type="text" formControlName="newPassword" />
</mat-form-field>
</p>
<button type="submit" mat-raised-button color="accent">Update my profile</button>
</form>

View File

@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { UserProfileComponent } from './user-profile.component';
describe('UserProfileComponent', () => {
let component: UserProfileComponent;
let fixture: ComponentFixture<UserProfileComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ UserProfileComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(UserProfileComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -0,0 +1,61 @@
import { Component, OnInit } from "@angular/core";
import { FormGroup, FormControl, Validators } from "@angular/forms";
import { AuthenticationService } from "../../services/authentication.service";
import { User } from "../../models/user";
@Component({
selector: "app-user-profile",
templateUrl: "./user-profile.component.html",
styleUrls: ["./user-profile.component.css"],
})
export class UserProfileComponent implements OnInit {
public userForm: FormGroup;
constructor(private authenticationService: AuthenticationService) {}
ngOnInit(): void {
let currentUser = this.authenticationService.currentUserValue;
this.userForm = new FormGroup(
{
login: new FormControl(currentUser.login, Validators.required),
firstName: new FormControl(currentUser.firstName, [
Validators.required,
Validators.minLength(3),
]),
lastName: new FormControl(currentUser.lastName, [
Validators.required,
Validators.minLength(3),
]),
email: new FormControl(currentUser.email, [
Validators.required,
Validators.email,
]),
currentPassword: new FormControl(
"",
Validators.pattern("^[A-Za-z0-9_-]{8,15}$")
),
newPassword: new FormControl(
"",
Validators.pattern("^[A-Za-z0-9_-]{8,15}$")
),
},
{ updateOn: "blur" }
);
}
onSubmit(formData) {
if (this.userForm.invalid) {
return;
}
let updatedUser = new User();
updatedUser.login = formData.username;
updatedUser.password = formData.password;
updatedUser.firstName = formData.firstname;
updatedUser.lastName = formData.lastname;
updatedUser.email = formData.email;
this.authenticationService.create(updatedUser);
}
}