diff --git a/src/app/accessibility/accessibility-settings.service.spec.ts b/src/app/accessibility/accessibility-settings.service.spec.ts index d6f61840575..5344c31d452 100644 --- a/src/app/accessibility/accessibility-settings.service.spec.ts +++ b/src/app/accessibility/accessibility-settings.service.spec.ts @@ -368,12 +368,4 @@ describe('accessibilitySettingsService', () => { }); }); - describe('getInputType', () => { - it('should correctly return the input type', () => { - expect(service.getInputType(AccessibilitySetting.NotificationTimeOut)).toEqual('number'); - expect(service.getInputType(AccessibilitySetting.LiveRegionTimeOut)).toEqual('number'); - expect(service.getInputType('unknownValue' as AccessibilitySetting)).toEqual('text'); - }); - }); - }); diff --git a/src/app/accessibility/accessibility-settings.service.stub.ts b/src/app/accessibility/accessibility-settings.service.stub.ts index b619a337de9..4ac965059e6 100644 --- a/src/app/accessibility/accessibility-settings.service.stub.ts +++ b/src/app/accessibility/accessibility-settings.service.stub.ts @@ -31,4 +31,10 @@ export class AccessibilitySettingsServiceStub { setSettingsInCookie = jasmine.createSpy('setSettingsInCookie'); getInputType = jasmine.createSpy('getInputType').and.returnValue('text'); + + convertFormValuesToStoredValues = jasmine.createSpy('convertFormValuesToStoredValues').and.returnValue({}); + + convertStoredValuesToFormValues = jasmine.createSpy('convertStoredValuesToFormValues').and.returnValue({}); + + getPlaceholder = jasmine.createSpy('getPlaceholder').and.returnValue('placeholder'); } diff --git a/src/app/accessibility/accessibility-settings.service.ts b/src/app/accessibility/accessibility-settings.service.ts index 4089fd03b17..f2151675391 100644 --- a/src/app/accessibility/accessibility-settings.service.ts +++ b/src/app/accessibility/accessibility-settings.service.ts @@ -2,13 +2,14 @@ import { Injectable } from '@angular/core'; import { Observable, of, switchMap } from 'rxjs'; import { map, take } from 'rxjs/operators'; import { CookieService } from '../core/services/cookie.service'; -import { hasValue, isNotEmpty, isNotEmptyOperator } from '../shared/empty.util'; +import { hasValue, isNotEmpty } from '../shared/empty.util'; import { AuthService } from '../core/auth/auth.service'; import { EPerson } from '../core/eperson/models/eperson.model'; import { EPersonDataService } from '../core/eperson/eperson-data.service'; import { getFirstCompletedRemoteData } from '../core/shared/operators'; import cloneDeep from 'lodash/cloneDeep'; import { environment } from '../../environments/environment'; +import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils'; /** * Name of the cookie used to store the settings locally @@ -22,15 +23,27 @@ export const ACCESSIBILITY_SETTINGS_METADATA_KEY = 'dspace.accessibility.setting /** * Enum containing all possible accessibility settings. - * When adding new settings, the {@link AccessibilitySettingsService#getInputType} method and the i18n keys for the - * accessibility settings page should be updated. + * When adding new settings, make sure to add the new setting to the accessibility-settings component. + * The converter methods to convert from stored format to form format (and vice-versa) need to be updated as well. */ export enum AccessibilitySetting { NotificationTimeOut = 'notificationTimeOut', LiveRegionTimeOut = 'liveRegionTimeOut', } -export type AccessibilitySettings = { [key in AccessibilitySetting]?: any }; +/** + * Type representing an object that contains accessibility settings values. + */ +export type AccessibilitySettings = { [key in AccessibilitySetting]?: string }; + +/** + * The accessibility settings object format used by the accessibility-settings component form. + */ +export interface AccessibilitySettingsFormValues { + disableNotificationTimeOut: boolean, + notificationTimeOut: string, + liveRegionTimeOut: string, +} /** * Service handling the retrieval and configuration of accessibility settings. @@ -192,8 +205,8 @@ export class AccessibilitySettingsService { return this.ePersonService.createPatchFromCache(user).pipe( take(1), - isNotEmptyOperator(), - switchMap(operations => this.ePersonService.patch(user, operations)), + switchMap(operations => + isNotEmpty(operations) ? this.ePersonService.patch(user, operations) : createSuccessfulRemoteDataObject$({})), getFirstCompletedRemoteData(), map(rd => rd.hasSucceeded), ); @@ -211,17 +224,67 @@ export class AccessibilitySettingsService { } /** - * Returns the input type that a form should use for the provided {@link AccessibilitySetting} + * Clears all settings in the cookie and attempts to clear settings in metadata. + * Emits true if settings in metadata were cleared and false otherwise. */ - getInputType(setting: AccessibilitySetting): string { + clearSettings(): Observable { + this.setSettingsInCookie({}); + return this.setSettingsInAuthenticatedUserMetadata({}); + } + + /** + * Retrieve the placeholder to be used for the provided AccessibilitySetting. + * Returns an empty string when no placeholder is specified for the provided setting. + */ + getPlaceholder(setting: AccessibilitySetting): string { switch (setting) { case AccessibilitySetting.NotificationTimeOut: - return 'number'; + return millisecondsToSeconds(environment.notifications.timeOut.toString()); case AccessibilitySetting.LiveRegionTimeOut: - return 'number'; + return millisecondsToSeconds(environment.liveRegion.messageTimeOutDurationMs.toString()); default: - return 'text'; + return ''; } } + /** + * Convert values in the provided accessibility settings object to values ready to be stored. + */ + convertFormValuesToStoredValues(settings: AccessibilitySettingsFormValues): AccessibilitySettings { + return { + 'notificationTimeOut': settings.disableNotificationTimeOut ? '0' + : secondsToMilliseconds(settings.notificationTimeOut), + 'liveRegionTimeOut': secondsToMilliseconds(settings.liveRegionTimeOut), + }; + } + + /** + * Convert values in the provided accessibility settings object to values ready to show in the form. + */ + convertStoredValuesToFormValues(settings: AccessibilitySettings): AccessibilitySettingsFormValues { + return { + disableNotificationTimeOut: parseFloat(settings.notificationTimeOut) === 0, + notificationTimeOut: millisecondsToSeconds(settings.notificationTimeOut), + liveRegionTimeOut: millisecondsToSeconds(settings.liveRegionTimeOut), + }; + } + +} + +function secondsToMilliseconds(secondsStr: string): string { + const seconds = parseFloat(secondsStr); + if (isNaN(seconds)) { + return null; + } else { + return (seconds * 1000).toString(); + } +} + +function millisecondsToSeconds(millisecondsStr: string): string { + const milliseconds = parseFloat(millisecondsStr); + if (isNaN(milliseconds)) { + return null; + } else { + return (milliseconds / 1000).toString(); + } } diff --git a/src/app/info/accessibility-settings/accessibility-settings.component.html b/src/app/info/accessibility-settings/accessibility-settings.component.html index 6550c6a2880..d247b12f949 100644 --- a/src/app/info/accessibility-settings/accessibility-settings.component.html +++ b/src/app/info/accessibility-settings/accessibility-settings.component.html @@ -2,25 +2,87 @@

{{ 'info.accessibility-settings.title' | translate }}

-
-