회원수정 페이지 수정

This commit is contained in:
byung eun park 2019-08-29 17:27:05 +09:00
parent 636f10eae1
commit 1f828b5f95
5 changed files with 109 additions and 6 deletions

View File

@ -55,7 +55,7 @@
fxFlex="1 0 auto" fxFlex="1 0 auto"
name="userForm" name="userForm"
[formGroup]="userForm" [formGroup]="userForm"
(ngSubmit)="registUser()" (ngSubmit)="updateUser()"
> >
<div class="h2 mb-24">기본정보</div> <div class="h2 mb-24">기본정보</div>
@ -110,6 +110,23 @@
<mat-icon matSuffix class="secondary-text" <mat-icon matSuffix class="secondary-text"
>vpn_key</mat-icon >vpn_key</mat-icon
> >
<mat-error
*ngIf="
userForm.get('passwordConfirm').hasError('required')
"
>
패스워드 확인은 필수 사항입니다!
</mat-error>
<mat-error
*ngIf="
!userForm.get('passwordConfirm').hasError('required') &&
userForm
.get('passwordConfirm')
.hasError('passwordsNotMatching')
"
>
패스워드가 일치하지 않습니다!
</mat-error>
</mat-form-field> </mat-form-field>
</div> </div>

View File

@ -1,7 +1,15 @@
import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core'; import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms'; import {
AbstractControl,
FormBuilder,
FormGroup,
Validators,
ValidationErrors,
ValidatorFn
} from '@angular/forms';
import { Subject } from 'rxjs'; import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators'; import { take } from 'rxjs/operators';
import { takeUntil } from 'rxjs/internal/operators';
import { fuseAnimations } from 'src/@fuse/animations'; import { fuseAnimations } from 'src/@fuse/animations';
import { User } from 'src/modules/user/model/user.model'; import { User } from 'src/modules/user/model/user.model';
@ -52,6 +60,12 @@ export class UserDetailComponent implements OnInit, OnDestroy {
this.initializeForm(); this.initializeForm();
}); });
this.userForm
.get('password')
.valueChanges.pipe(takeUntil(this._unsubscribeAll))
.subscribe(() => {
this.userForm.get('passwordConfirm').updateValueAndValidity();
});
} }
/** /**
@ -75,17 +89,33 @@ export class UserDetailComponent implements OnInit, OnDestroy {
} }
], ],
password: '', password: '',
passwordConfirm: ['', confirmPasswordValidator],
email: this.user.email, email: this.user.email,
nickname: this.user.nickname, nickname: [this.user.nickname, Validators.required],
phone: this.user.phone, phone: this.user.phone,
descriptions: this.user.descriptions descriptions: this.user.descriptions
}); });
} }
// ----------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------
// @ Public methods // @ Public methodsAbstractControl
// ----------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------
updateUser(): void {
let updateUser: User = <User>this.userForm.value;
const pw: string = this.userForm.get('password').value;
this.userService
.updateUser(this.user.id, updateUser, pw)
.pipe(take(1))
.subscribe(
res => {
console.log(res);
},
err => {
console.log(err);
}
);
}
/** /**
* Update status * Update status
*/ */
@ -101,3 +131,28 @@ export class UserDetailComponent implements OnInit, OnDestroy {
// this.order.status.unshift(newStatus); // this.order.status.unshift(newStatus);
} }
} }
export const confirmPasswordValidator: ValidatorFn = (
control: AbstractControl
): ValidationErrors | null => {
if (!control.parent || !control) {
return null;
}
const password = control.parent.get('password');
const passwordConfirm = control.parent.get('passwordConfirm');
if (!password || !passwordConfirm) {
return null;
}
if (passwordConfirm.value === '') {
return null;
}
if (password.value === passwordConfirm.value) {
return null;
}
return { passwordsNotMatching: true };
};

View File

@ -11,7 +11,7 @@ import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/internal/operators'; import { takeUntil } from 'rxjs/internal/operators';
import { take } from 'rxjs/operators'; import { take } from 'rxjs/operators';
import { fuseAnimations } from 'src/@fuse/animations'; import { fuseAnimations } from 'src/@fuse/animations';
import { User } from 'src/modules/user/model/user.model';
import { SignupRequest } from 'src/modules/auth/model/auth-signup-request.model'; import { SignupRequest } from 'src/modules/auth/model/auth-signup-request.model';
import { AuthService } from 'src/modules/auth/service/auth.service'; import { AuthService } from 'src/modules/auth/service/auth.service';
@Component({ @Component({

View File

@ -2,6 +2,7 @@ import { Role } from './role.model';
import { DateAudit } from 'src/modules/common/data/model/audit'; import { DateAudit } from 'src/modules/common/data/model/audit';
export interface User extends DateAudit { export interface User extends DateAudit {
id?: number;
username?: string; username?: string;
nickname?: string; nickname?: string;
email?: string; email?: string;

View File

@ -4,6 +4,7 @@ import { Observable } from 'rxjs';
import { User } from 'src/modules/user/model/user.model'; import { User } from 'src/modules/user/model/user.model';
import { API_BASE_URL } from 'src/modules/common/type/injection-token.type'; import { API_BASE_URL } from 'src/modules/common/type/injection-token.type';
import { Page } from 'src/modules/common/data/model/page'; import { Page } from 'src/modules/common/data/model/page';
import { map } from 'rxjs/operators';
@Injectable() @Injectable()
export class UserService { export class UserService {
@ -19,4 +20,33 @@ export class UserService {
public getUser(id: number): Observable<User> { public getUser(id: number): Observable<User> {
return this.httpClient.get<User>(`${this.apiBaseUrl}/users/${id}`, {}); return this.httpClient.get<User>(`${this.apiBaseUrl}/users/${id}`, {});
} }
public updateUser(
id: number,
newUser: User,
newPassword: string
): Observable<User> {
const username = newUser.username;
const password = newPassword;
const email = newUser.email;
const phone = newUser.phone;
const descriptions = newUser.descriptions;
const nickname = newUser.nickname;
return this.httpClient
.put<any>(`${this.apiBaseUrl}/users/${id}`, {
username,
nickname,
email,
password,
descriptions,
phone
})
.pipe(
map(res => {
console.log('update response: ' + res);
return res;
})
);
}
} }