From da337a79eb6a4d27ba25a3bd48cde14b182df2dc Mon Sep 17 00:00:00 2001 From: pbardy2000 <146740183+pbardy2000@users.noreply.github.com> Date: Fri, 15 Dec 2023 13:18:57 +0000 Subject: [PATCH] fix(cb2-10211): fix duplicate error messages + global error service not focusing elements (#1331) --- .../global-error/global-error.component.ts | 24 +++++++++++++++---- .../reducers/global-error-service.reducer.ts | 3 ++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/app/core/components/global-error/global-error.component.ts b/src/app/core/components/global-error/global-error.component.ts index c704b9789..d8dc2e79f 100644 --- a/src/app/core/components/global-error/global-error.component.ts +++ b/src/app/core/components/global-error/global-error.component.ts @@ -11,10 +11,26 @@ export class GlobalErrorComponent { goto(error: GlobalError) { if (error.anchorLink) { - const el = document.getElementById(error.anchorLink); - if (el) { - el.focus({ preventScroll: false }); - } + let focusCount = 0; + + document.querySelectorAll(` + #${error.anchorLink}, + #${error.anchorLink} a[href]:not([tabindex='-1']), + #${error.anchorLink} area[href]:not([tabindex='-1']), + #${error.anchorLink} input:not([disabled]):not([tabindex='-1']), + #${error.anchorLink} select:not([disabled]):not([tabindex='-1']), + #${error.anchorLink} textarea:not([disabled]):not([tabindex='-1']), + #${error.anchorLink} button:not([disabled]):not([tabindex='-1']), + #${error.anchorLink} iframe:not([tabindex='-1']), + #${error.anchorLink} [tabindex]:not([tabindex='-1']), + #${error.anchorLink} [contentEditable=true]:not([tabindex='-1']) + `) + .forEach((el) => { + if (el instanceof HTMLElement && focusCount < 2) { + focusCount++; + el.focus({ preventScroll: false }); + } + }); } } } diff --git a/src/app/store/global-error/reducers/global-error-service.reducer.ts b/src/app/store/global-error/reducers/global-error-service.reducer.ts index 59a1567d4..0795c0978 100644 --- a/src/app/store/global-error/reducers/global-error-service.reducer.ts +++ b/src/app/store/global-error/reducers/global-error-service.reducer.ts @@ -6,6 +6,7 @@ import { import { fetchSearchResult, fetchSearchResultFailed } from '@store/tech-record-search/actions/tech-record-search.actions'; // eslint-disable-next-line import/no-cycle import * as TestResultActions from '@store/test-records'; +import _ from 'lodash'; import * as ReferenceDataActions from '../../reference-data/actions/reference-data.actions'; import * as TechnicalRecordServiceActions from '../../technical-records/actions/technical-record-service.actions'; import { createVehicleRecordFailure } from '../../technical-records/actions/technical-record-service.actions'; @@ -57,7 +58,7 @@ export const globalErrorReducer = createReducer( ), on(GlobalErrorActions.setErrors, TestResultActions.updateTestResultFailed, TestResultActions.createTestResultFailed, (state, { errors }) => ({ ...state, - errors: [...errors], + errors: [...(_.uniqWith(errors, (a, b) => _.isEqual(a.error, b.error)))], })), on(GlobalErrorActions.patchErrors, (state, { errors }) => ({ ...state, errors: [...state.errors, ...errors] })), on(createVehicleRecordFailure, (state, action) => ({ errors: [...state.errors, { error: action.error }] })),