diff --git a/src/app/profile-page/profile-page-metadata-form/profile-page-metadata-form.component.ts b/src/app/profile-page/profile-page-metadata-form/profile-page-metadata-form.component.ts index dbd49b802a..0b06b3f076 100644 --- a/src/app/profile-page/profile-page-metadata-form/profile-page-metadata-form.component.ts +++ b/src/app/profile-page/profile-page-metadata-form/profile-page-metadata-form.component.ts @@ -23,12 +23,24 @@ import { NotificationsService } from '../../shared/notifications/notifications.s selector: 'ds-profile-page-metadata-form', templateUrl: './profile-page-metadata-form.component.html' }) +/** + * Component for a user to edit their metadata + * Displays a form containing: + * - readonly email field, + * - required first name text field + * - required last name text field + * - phone text field + * - language dropdown + */ export class ProfilePageMetadataFormComponent implements OnInit { /** * The user to display the form for */ @Input() user: EPerson; + /** + * The form's input models + */ formModel: DynamicFormControlModel[] = [ new DynamicInputModel({ id: 'email', @@ -72,14 +84,24 @@ export class ProfilePageMetadataFormComponent implements OnInit { */ formGroup: FormGroup; + /** + * Prefix for the form's label messages of this component + */ LABEL_PREFIX = 'profile.metadata.form.label.'; + /** + * Prefix for the form's error messages of this component + */ ERROR_PREFIX = 'profile.metadata.form.error.'; + /** + * Prefix for the notification messages of this component + */ NOTIFICATION_PREFIX = 'profile.metadata.form.notifications.'; /** * All of the configured active languages + * Used to populate the language dropdown */ activeLangs: LangConfig[]; @@ -101,6 +123,10 @@ export class ProfilePageMetadataFormComponent implements OnInit { }); } + /** + * Loop over all the form's input models and set their values depending on the user's metadata + * Create the FormGroup + */ setFormValues() { this.formModel.forEach( (fieldModel: DynamicInputModel | DynamicSelectModel) => { @@ -118,6 +144,9 @@ export class ProfilePageMetadataFormComponent implements OnInit { this.formGroup = this.formBuilderService.createFormGroup(this.formModel); } + /** + * Update the translations of the field labels and error messages + */ updateFieldTranslations() { this.formModel.forEach( (fieldModel: DynamicInputModel) => { @@ -132,6 +161,15 @@ export class ProfilePageMetadataFormComponent implements OnInit { ); } + /** + * Update the user's metadata + * + * Sends a patch request for updating the user's metadata when at least one value changed or got added/removed and the + * form is valid. + * Nothing happens when the form is invalid or no metadata changed. + * + * Returns false when nothing happened. + */ updateProfile(): boolean { if (!this.formGroup.valid) { return false; diff --git a/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.ts b/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.ts index d5a8d358b8..2c02027b4a 100644 --- a/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.ts +++ b/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.ts @@ -16,12 +16,19 @@ import { NotificationsService } from '../../shared/notifications/notifications.s selector: 'ds-profile-page-security-form', templateUrl: './profile-page-security-form.component.html' }) +/** + * Component for a user to edit their security information + * Displays a form containing a password field and a confirmation of the password + */ export class ProfilePageSecurityFormComponent implements OnInit { /** * The user to display the form for */ @Input() user: EPerson; + /** + * The form's input models + */ formModel: DynamicFormControlModel[] = [ new DynamicInputModel({ id: 'password', @@ -40,8 +47,14 @@ export class ProfilePageSecurityFormComponent implements OnInit { */ formGroup: FormGroup; + /** + * Prefix for the notification messages of this component + */ NOTIFICATIONS_PREFIX = 'profile.security.form.notifications.'; + /** + * Prefix for the form's label messages of this component + */ LABEL_PREFIX = 'profile.security.form.label.'; constructor(protected formService: DynamicFormService, @@ -59,6 +72,9 @@ export class ProfilePageSecurityFormComponent implements OnInit { }); } + /** + * Update the translations of the field labels + */ updateFieldTranslations() { this.formModel.forEach( (fieldModel: DynamicInputModel) => { @@ -67,6 +83,10 @@ export class ProfilePageSecurityFormComponent implements OnInit { ); } + /** + * Check if both password fields are filled in and equal + * @param group The FormGroup to validate + */ checkPasswords(group: FormGroup) { const pass = group.get('password').value; const repeatPass = group.get('passwordrepeat').value; @@ -74,6 +94,16 @@ export class ProfilePageSecurityFormComponent implements OnInit { return isEmpty(repeatPass) || pass === repeatPass ? null : { notSame: true }; } + /** + * Update the user's security details + * + * Sends a patch request for changing the user's password when a new password is present and the password confirmation + * matches the new password. + * Nothing happens when no passwords are filled in. + * An error notification is displayed when the password confirmation does not match the new password. + * + * Returns false when nothing happened + */ updateSecurity() { const pass = this.formGroup.get('password').value; const passEntered = isNotEmpty(pass); diff --git a/src/app/profile-page/profile-page.component.ts b/src/app/profile-page/profile-page.component.ts index f353ab108f..5a2736593a 100644 --- a/src/app/profile-page/profile-page.component.ts +++ b/src/app/profile-page/profile-page.component.ts @@ -25,9 +25,14 @@ import { followLink } from '../shared/utils/follow-link-config.model'; * Component for a user to edit their profile information */ export class ProfilePageComponent implements OnInit { - + /** + * A reference to the metadata form component + */ @ViewChild(ProfilePageMetadataFormComponent, { static: false }) metadataForm: ProfilePageMetadataFormComponent; + /** + * A reference to the security form component + */ @ViewChild(ProfilePageSecurityFormComponent, { static: false }) securityForm: ProfilePageSecurityFormComponent; /** @@ -40,6 +45,9 @@ export class ProfilePageComponent implements OnInit { */ groupsRD$: Observable>>; + /** + * Prefix for the notification messages of this component + */ NOTIFICATIONS_PREFIX = 'profile.notifications.'; constructor(private store: Store, @@ -59,6 +67,10 @@ export class ProfilePageComponent implements OnInit { this.groupsRD$ = this.user$.pipe(switchMap((user: EPerson) => user.groups)); } + /** + * Fire an update on both the metadata and security forms + * Show a warning notification when no changes were made in both forms + */ updateProfile() { const metadataChanged = this.metadataForm.updateProfile(); const securityChanged = this.securityForm.updateSecurity();