-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EXUI-2058: Add error message on staffUI (#3778)
* Add ngrx store for staff ui, Add error handling when staff user select returns 404 * add Unit tests * Code review comments resolved --------- Co-authored-by: RiteshHMCTS <[email protected]>
- Loading branch information
1 parent
069b8bc
commit 97962c9
Showing
13 changed files
with
231 additions
and
6 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
src/staff-administrator/containers/staff-users/staff-users.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { StaffUserResponseError } from './staff-user-response-error.model'; | ||
|
||
export interface StaffState { | ||
staffGetError: null | StaffUserResponseError; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/staff-administrator/models/staff-user-response-error.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface StaffUserResponseError { | ||
errorCode: number; | ||
errorDescription: string; | ||
errorMessage: string; | ||
status: string; | ||
timeStamp: string; | ||
} |
18 changes: 14 additions & 4 deletions
18
src/staff-administrator/resolvers/staff-user-details-resolver.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ActivatedRouteSnapshot } from '@angular/router'; | ||
import { map } from 'rxjs/operators'; | ||
import { Store } from '@ngrx/store'; | ||
import { catchError, map } from 'rxjs/operators'; | ||
import { of } from 'rxjs'; | ||
import { StaffUser } from '../models/staff-user.model'; | ||
import { StaffDataAccessService } from '../services/staff-data-access/staff-data-access.service'; | ||
import { SetError, ResetStaffSelect } from '../store/actions/staff-select.action'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class StaffUserDetailsResolverService { | ||
constructor(private staffDataAccessService: StaffDataAccessService) { | ||
} | ||
constructor( | ||
private staffDataAccessService: StaffDataAccessService, | ||
private store: Store | ||
) {} | ||
|
||
public resolve(route?: ActivatedRouteSnapshot) { | ||
this.store.dispatch(new ResetStaffSelect()); | ||
return this.staffDataAccessService.fetchSingleUserById(route.params.id).pipe( | ||
map((user) => StaffUser.from(user)) | ||
map((user) => StaffUser.from(user)), | ||
catchError((error) => { | ||
this.store.dispatch(new SetError(error.error)); | ||
return of(); | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/staff-administrator/store/actions/staff-select.action.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { StaffUserResponseError } from '../../models/staff-user-response-error.model'; | ||
import { SetError, ResetStaffSelect, SET_STAFF_SELECT_ERROR, RESET_STAFF_SELECT_ERROR } from './staff-select.action'; | ||
|
||
describe('StaffSelect Actions', () => { | ||
describe('SetError', () => { | ||
it('should create an action', () => { | ||
const error: StaffUserResponseError = { | ||
errorMessage: 'An error occurred.', | ||
errorCode: 404, | ||
errorDescription: 'A description of the error.', | ||
status: 'Failed', | ||
timeStamp: new Date().toISOString() | ||
}; | ||
const action = new SetError(error); | ||
|
||
expect({ ...action }).toEqual({ | ||
type: SET_STAFF_SELECT_ERROR, | ||
payload: error | ||
}); | ||
}); | ||
}); | ||
|
||
describe('ResetStaffSelect', () => { | ||
it('should create an action', () => { | ||
const action = new ResetStaffSelect(); | ||
|
||
expect({ ...action }).toEqual({ | ||
type: RESET_STAFF_SELECT_ERROR | ||
}); | ||
}); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
src/staff-administrator/store/actions/staff-select.action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Action } from '@ngrx/store'; | ||
import { StaffUserResponseError } from '../../models/staff-user-response-error.model'; | ||
|
||
export const SET_STAFF_SELECT_ERROR = '[Staff Select] Set Select Staff Error'; | ||
export const RESET_STAFF_SELECT_ERROR = '[Staff Select] Reset Staff Select Error'; | ||
|
||
export class SetError implements Action { | ||
public readonly type = SET_STAFF_SELECT_ERROR; | ||
constructor(public payload: StaffUserResponseError) { } | ||
} | ||
|
||
export class ResetStaffSelect implements Action { | ||
public readonly type = RESET_STAFF_SELECT_ERROR; | ||
} | ||
|
||
export type StaffSelectAction = | ||
| SetError | ||
| ResetStaffSelect; |
44 changes: 44 additions & 0 deletions
44
src/staff-administrator/store/reducers/staff-select.reducer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { provideMockStore } from '@ngrx/store/testing'; | ||
import { staffSelectReducer, initialState } from './staff-select.reducer'; | ||
import * as ErrorActions from '../actions/staff-select.action'; | ||
import { StaffUserResponseError } from '../../models/staff-user-response-error.model'; | ||
|
||
describe('staffSelectReducer', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [provideMockStore({ initialState })] | ||
}); | ||
}); | ||
|
||
it('should return the initial state', () => { | ||
const action = { type: 'NOOP' } as any; | ||
const result = staffSelectReducer(initialState, action); | ||
|
||
expect(result).toEqual(initialState); | ||
}); | ||
|
||
it('should handle RESET_STAFF_SELECT_ERROR', () => { | ||
const action = new ErrorActions.ResetStaffSelect(); | ||
const result = staffSelectReducer(initialState, action); | ||
|
||
expect(result).toEqual(initialState); | ||
}); | ||
|
||
it('should handle SET_STAFF_SELECT_ERROR', () => { | ||
const error: StaffUserResponseError = { | ||
errorMessage: 'Test error', | ||
errorCode: 123, | ||
errorDescription: 'Test error description', | ||
status: 'Error', | ||
timeStamp: '2022-01-01T00:00:00Z' | ||
}; | ||
const action = new ErrorActions.SetError(error); | ||
const result = staffSelectReducer(initialState, action); | ||
|
||
expect(result).toEqual({ | ||
...initialState, | ||
staffGetError: error | ||
}); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
src/staff-administrator/store/reducers/staff-select.reducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { StaffState } from '../../models/staff-state.model'; | ||
import * as ErrorActions from '../actions/staff-select.action'; | ||
|
||
export const initialState: StaffState = { | ||
staffGetError: null | ||
}; | ||
|
||
export function staffSelectReducer( | ||
currentState = initialState, | ||
action: ErrorActions.StaffSelectAction | ||
) { | ||
switch (action.type) { | ||
case ErrorActions.RESET_STAFF_SELECT_ERROR: { | ||
return { | ||
...initialState | ||
}; | ||
} | ||
case ErrorActions.SET_STAFF_SELECT_ERROR: { | ||
return { | ||
...currentState, | ||
staffGetError: action.payload | ||
}; | ||
} | ||
default: { | ||
return { | ||
...currentState | ||
}; | ||
} | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
src/staff-administrator/store/selectors/staff-select.selector.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { StoreModule, Store, select } from '@ngrx/store'; | ||
import { selectStaffError } from './staff-select.selector'; | ||
import { staffSelectReducer } from '../reducers/staff-select.reducer'; | ||
|
||
describe('Staff Selectors', () => { | ||
let store: Store; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
StoreModule.forRoot({}), | ||
StoreModule.forFeature('staffUI', staffSelectReducer) | ||
] | ||
}); | ||
store = TestBed.inject(Store); | ||
spyOn(store, 'dispatch').and.callThrough(); | ||
}); | ||
|
||
describe('getHearingConditions', () => { | ||
it('should return hearings navigation state', () => { | ||
let result = null; | ||
store.pipe(select(selectStaffError)).subscribe((value) => { | ||
result = value; | ||
}); | ||
expect(result).toBeNull(); | ||
}); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
src/staff-administrator/store/selectors/staff-select.selector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createFeatureSelector, createSelector } from '@ngrx/store'; | ||
import { StaffState } from '../../models/staff-state.model'; | ||
|
||
export const selectStaffFeature = createFeatureSelector<any>('staffUI'); | ||
|
||
export const selectStaffError = createSelector( | ||
selectStaffFeature, | ||
(state: StaffState) => state.staffGetError | ||
); |