Skip to content

Commit

Permalink
Merge pull request #544 from ministryofjustice/bugfix/DST-9201
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl authored Jul 9, 2021
2 parents 7ab7d0c + d66e02a commit 4a0e2e3
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/main/java/uk/co/bconline/ndelius/model/Team.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package uk.co.bconline.ndelius.model;

import javax.validation.constraints.NotBlank;

import lombok.*;

import javax.validation.constraints.NotBlank;

@Getter
@Builder
@ToString
Expand All @@ -14,4 +14,5 @@ public final class Team
@NotBlank
private String code;
private String description;
private String providerCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public List<Team> getTeams(String probationArea)
{
List<TeamEntity> teams = ofNullable(probationArea)
.map(repository::findAllByEndDateIsNullAndProbationAreaCode)
.orElse(repository.findAllByEndDateIsNull());
.orElseGet(repository::findAllByEndDateIsNull);

return transformer.map(teams);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.springframework.stereotype.Component;
import uk.co.bconline.ndelius.model.Team;
import uk.co.bconline.ndelius.model.entity.ProbationAreaEntity;
import uk.co.bconline.ndelius.model.entity.TeamEntity;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

Expand All @@ -18,6 +20,7 @@ public List<Team> map(Collection<TeamEntity> teams)
.map(team -> Team.builder()
.code(team.getCode())
.description(team.getDescription())
.providerCode(Optional.ofNullable(team.getProbationArea()).map(ProbationAreaEntity::getCode).orElse(null))
.build())
.collect(toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void csvHasTestData() throws CsvRequiredFieldEmptyException, CsvDataTypeM
.build()), writer);
String csv = writer.toString();

String expectedTestUser = "\n\"test.user\",\"test\",\"user\",\"2020-01-01\",\"ABC123\",\"Team(code=TEAM01, description=Team 1) Team(code=TEAM02, description=Team 2)\"\n";
String expectedTestUser = "\n\"test.user\",\"test\",\"user\",\"2020-01-01\",\"ABC123\",\"Team(code=TEAM01, description=Team 1, providerCode=null) Team(code=TEAM02, description=Team 2, providerCode=null)\"\n";
assertThat(csv, containsString(expectedTestUser));
}

Expand Down
10 changes: 5 additions & 5 deletions ui/src/app/component/item-selector/item-selector.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,12 @@
style="cursor: pointer"
title="Only show selected options"
[value]="SELECTED_OPTION_SUB_MENU">Selected</option>
<option *ngFor="let dataset of subMenuItems"
[value]="dataset.code"
[selected]="dataset.description"
<option *ngFor="let items of subMenuItems"
[value]="items.code"
class="nav-link py-1"
tabindex="0"
title="Show teams for the {{dataset.description}} provider">
{{ dataset.description }}
title="Show teams for the {{items.description}} provider">
{{ items.description }}
</option>
</select>

Expand All @@ -99,6 +98,7 @@
title="Filter selectable options" placeholder="Search&hellip;" aria-label="Filter">
</div>
</div>
<div *ngIf="available == null || available.length === 0">{{ subMenuMessage }}</div>
<div class="text-truncate custom-control custom-{{multiple? 'checkbox': 'radio'}}"
[class.d-none]="onlyShowSelected && !isSelected(item)"
*ngFor="let item of filtered; let i = index">
Expand Down
50 changes: 38 additions & 12 deletions ui/src/app/component/item-selector/item-selector.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {
ChangeDetectionStrategy,
Component,
DoCheck,
ElementRef,
EventEmitter,
forwardRef,
Input,
OnChanges,
OnInit,
Output,
SimpleChanges,
ViewChild
} from '@angular/core';
import {
Expand Down Expand Up @@ -40,7 +43,7 @@ declare var Popper: any;
})

export class ItemSelectorComponent
implements ControlValueAccessor, Validator, OnInit {
implements ControlValueAccessor, Validator, OnInit, DoCheck, OnChanges {
@ViewChild('filterControl', {static: true}) filterControl: ElementRef;
@ViewChild('toggleBtn', {static: true}) toggleBtn: ElementRef;
@ViewChild('dropdown', {static: true}) dropdown: ElementRef;
Expand All @@ -54,7 +57,8 @@ export class ItemSelectorComponent
@Input() alignRight: boolean;
@Input() placeholder = 'Please select...';
@Input() loadingText = 'Loading...';
@Input() subMenuItems: {code: string, description: string}[];
@Input() subMenuItems: { code: string, description: string }[];
@Input() prevSubMenuItems: { code: string, description: string }[]; // for change detection
@Input() selectedSubMenuItem: string;
@Input() disabled: boolean;
@Output() selectedChange: EventEmitter<any> = new EventEmitter<any>();
Expand All @@ -64,6 +68,7 @@ export class ItemSelectorComponent
dirty = false;
filter = '';
onlyShowSelected: boolean;
subMenuMessage: string;

@Input() idMapper: Function = null;
@Input() getSubMenu: Function;
Expand All @@ -74,6 +79,25 @@ export class ItemSelectorComponent
private propagateTouchChange = (_: any) => {
}

ngOnChanges(changes: SimpleChanges) {
// Change detection to the default selectedSubMenuItem (homeArea)
if (changes.hasOwnProperty('selectedSubMenuItem')) {
this.getSubMenuList();
}
}

ngDoCheck() {
// Using ngDoCheck for custom change detection on the subMenuItems Array
// Change detection when the subMenuItems changes (datasets)
if (this.subMenuItems != null && this.subMenuItems.length !== (this.prevSubMenuItems || []).length) {
this.prevSubMenuItems = [...this.subMenuItems];
// DST-9201 Filter selected items (teams) to contain only items where providerCode is in the list of available subMenuItems (datasets)
// Note: providerCode is specific to Teams - if we need to make this generic we'll need to rethink this.
this.selected = this.selected.filter(sel => this.subMenuItems.some(subMenuItem => subMenuItem.code === sel.providerCode));
this.getSubMenuList();
}
}

toggle(item): void {
if (this.multiple) {
if (this.selected == null) {
Expand Down Expand Up @@ -166,11 +190,7 @@ export class ItemSelectorComponent
};

if (this.subMenuItems != null) {
if (this.onlyShowSelected) {
return [...this.selected, ...this.availableItems].filter(removeNullAndDuplicates);
} else {
return this.availableItems.filter(removeNullAndDuplicates);
}
return this.availableItems.filter(removeNullAndDuplicates);
} else if (this.multiple) {
return [...this.selected, ...this.availableItems].filter(removeNullAndDuplicates);
} else {
Expand Down Expand Up @@ -269,16 +289,22 @@ export class ItemSelectorComponent
}

getSubMenuList(): void {
// Triggered when changing sub-menu, to populate the available items within the sub-menu
if (this.selectedSubMenuItem === this.SELECTED_OPTION_SUB_MENU) {
this.available = this.selected;
this.onlyShowSelected = true;
this.available = [];
return;
} else {
this.available = null;
this.onlyShowSelected = false;
this.subMenuMessage = 'Loading...';
this.getSubMenu(this.selectedSubMenuItem).subscribe(
items => {
this.subMenuMessage = '';
this.available = items;
},
() => this.subMenuMessage = 'Error loading menu items'
);
}
this.getSubMenu(this.selectedSubMenuItem).subscribe(
items => this.available = items
);
}

disableComponent(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/component/user/user.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ <h2 class="card-title mb-0" id="staff-details">Staff Details</h2>
[available]="teams"
[subMenuItems]="user.datasets"
[getSubMenu]="teamService.providerTeams.bind(teamService)"
[selectedSubMenuItem]="user.homeArea?.code"
[selectedSubMenuItem]="user.homeArea?.code || (user.datasets?.length > 0 && user.datasets[0]?.code)"
[readonly]="mode === 'View'"
[labelMapper]="LabelMappingUtils.codeDescriptionToLabel"></item-selector>
</div>
Expand Down
5 changes: 0 additions & 5 deletions ui/src/app/component/user/user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,11 @@ export class UserComponent implements OnInit {

homeAreaChanged() {
if (this.user.homeArea != null) {
this.teams = null;
this.teamService.providerTeams(this.user.homeArea.code).subscribe((teams: Team[]) => {
this.teams = teams;
});
this.subContractedProviders = null;
this.datasetService.subContractedProviders(this.user.homeArea.code).subscribe((subContractedProviders: Dataset[]) => {
this.subContractedProviders = subContractedProviders;
});
} else {
this.teams = [];
this.subContractedProviders = [];
}
}
Expand Down
1 change: 1 addition & 0 deletions ui/src/app/model/team.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export class Team {
code: string;
description: string;
providerCode: string;
}

0 comments on commit 4a0e2e3

Please sign in to comment.