70504: New user registration

This commit is contained in:
Yana De Pauw
2020-05-11 17:27:10 +02:00
parent 780b8b7db9
commit f136ea7d4f
24 changed files with 1164 additions and 8 deletions

View File

@@ -0,0 +1,155 @@
import { Component, Inject, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { map } from 'rxjs/operators';
import { Registration } from '../../core/shared/registration.model';
import { Observable } from 'rxjs';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { ConfirmedValidator } from './confirmed.validator';
import { TranslateService } from '@ngx-translate/core';
import { EPersonDataService } from '../../core/eperson/eperson-data.service';
import { EPerson } from '../../core/eperson/models/eperson.model';
import { LangConfig } from '../../../config/lang-config.interface';
import { GLOBAL_CONFIG, GlobalConfig } from '../../../config';
import { Store } from '@ngrx/store';
import { CoreState } from '../../core/core.reducers';
import { AuthenticateAction } from '../../core/auth/auth.actions';
import { NotificationsService } from '../../shared/notifications/notifications.service';
/**
* Component that renders the create profile page to be used by a user registering through a token
*/
@Component({
selector: 'ds-create-profile',
templateUrl: './create-profile.component.html'
})
export class CreateProfileComponent implements OnInit {
private registration$: Observable<Registration>;
email: string;
token: string;
userInfoForm: FormGroup;
passwordForm: FormGroup;
activeLangs: LangConfig[];
constructor(
@Inject(GLOBAL_CONFIG) public config: GlobalConfig,
private translateService: TranslateService,
private ePersonDataService: EPersonDataService,
private store: Store<CoreState>,
private router: Router,
private route: ActivatedRoute,
private formBuilder: FormBuilder,
private notificationsService: NotificationsService
) {
}
ngOnInit(): void {
this.registration$ = this.route.data.pipe(
map((data) => data.registration as Registration),
);
this.registration$.subscribe((registration: Registration) => {
this.email = registration.email;
this.token = registration.token;
});
this.activeLangs = this.config.languages.filter((MyLangConfig) => MyLangConfig.active === true);
this.userInfoForm = this.formBuilder.group({
firstName: new FormControl('', {
validators: [Validators.required],
}),
lastName: new FormControl('', {
validators: [Validators.required],
}),
contactPhone: new FormControl(''),
language: new FormControl(''),
});
this.passwordForm = this.formBuilder.group({
password: new FormControl('', {
validators: [Validators.required, Validators.minLength(6)],
updateOn: 'blur'
}),
confirmPassword: new FormControl('', {
validators: [Validators.required],
updateOn: 'blur'
})
}, {
validator: ConfirmedValidator('password', 'confirmPassword')
});
}
get firstName() {
return this.userInfoForm.get('firstName');
}
get lastName() {
return this.userInfoForm.get('lastName');
}
get contactPhone() {
return this.userInfoForm.get('contactPhone');
}
get language() {
return this.userInfoForm.get('language');
}
get password() {
return this.passwordForm.get('password');
}
get confirmPassword() {
return this.passwordForm.get('confirmPassword');
}
/**
* Submits the eperson to the service to be created.
* The submission will not be made when the form is not valid.
*/
submitEperson() {
if (!(this.userInfoForm.invalid || this.passwordForm.invalid)) {
const values = {
metadata: {
'eperson.firstname': [
{
value: this.firstName.value
}
],
'eperson.lastname': [
{
value: this.lastName.value
},
],
'eperson.phone': [
{
value: this.contactPhone.value
}
],
'eperson.language': [
{
value: this.language.value
}
]
},
email: this.email,
password: this.password.value,
canLogIn: true,
requireCertificate: false
};
const eperson = Object.assign(new EPerson(), values);
this.ePersonDataService.createEPersonForToken(eperson, this.token).subscribe((response) => {
if (response.isSuccessful) {
this.notificationsService.success('register-page.create-profile.submit.success.head', 'register-page.create-profile.submit.success.content');
this.store.dispatch(new AuthenticateAction(this.email, this.password.value));
this.router.navigate(['/home']);
} else {
this.notificationsService.error('register-page.create-profile.submit.error.head', 'register-page.create-profile.submit.error.content');
}
});
}
}
}