Skip to content

Commit

Permalink
Replaced instances of omitting a value from an interface with a new i…
Browse files Browse the repository at this point in the history
…nterface that extends from the previosu
  • Loading branch information
RLCorp committed Aug 6, 2024
1 parent a3641ad commit ffd2f48
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { AccessibilityService } from '@providers/accessibility/accessibility.service';
import { ExaminerRecordData } from '@pages/examiner-records/examiner-records.selector';
import { ExaminerRecordDataWithPercentage } from '@pages/examiner-records/examiner-records.selector';
import { ChartType } from 'ng-apexcharts';

export interface ExaminerReportsCardClick {
Expand All @@ -20,7 +20,7 @@ export class ExaminerReportsCard {
onCardClick: EventEmitter<ExaminerReportsCardClick> = new EventEmitter<ExaminerReportsCardClick>();

@Input()
passedData: ExaminerRecordData<any>[] = null;
passedData: ExaminerRecordDataWithPercentage<any>[] = null;
@Input()
chartID: string = null;
@Input()
Expand Down Expand Up @@ -87,11 +87,11 @@ export class ExaminerReportsCard {
* The `count` property is converted to a number before summing.
*
* @template T - The type of the data contained in the `ExaminerRecordData` objects.
* @param {ExaminerRecordData<T>[]} value - The array of `ExaminerRecordData` objects to be totaled.
* @param {ExaminerRecordDataWithPercentage<T>[]} value - The array of `ExaminerRecordData` objects to be totaled.
* @returns {number} The total count of all `ExaminerRecordData` objects.
*/
getTotal = <T>(
value: ExaminerRecordData<T>[],
value: ExaminerRecordDataWithPercentage<T>[],
): number => value.reduce((total, val) => total + Number(val.count), 0);

/**
Expand All @@ -102,11 +102,12 @@ export class ExaminerReportsCard {
* containing an empty array.
*
* @template T - The type of the data contained in the `ExaminerRecordData` objects.
* @param {ExaminerRecordData<T>[]} examinerRecordData - The array of `ExaminerRecordData` objects to be formatted.
* @param {ExaminerRecordDataWithPercentage<T>[]} examinerRecordData - The array of `ExaminerRecordData` objects
* to be formatted.
* @returns {T[][]} A two-dimensional array where each inner array contains the values of an `ExaminerRecordData`
* object.
*/
filterDataForGrid<T>(examinerRecordData: ExaminerRecordData<T>[]): T[][] {
filterDataForGrid<T>(examinerRecordData: ExaminerRecordDataWithPercentage<T>[]): T[][] {
if (!!examinerRecordData && examinerRecordData.length > 0) {
return examinerRecordData.map((obj) => Object.values(obj) as T[]);
}
Expand Down
75 changes: 38 additions & 37 deletions src/app/pages/examiner-records/examiner-records.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '@pages/examiner-records/examiner-records.actions';
import {
ExaminerRecordData,
ExaminerRecordDataWithPercentage,
getBalanceQuestions,
getCategories,
getCircuits,
Expand Down Expand Up @@ -64,35 +65,35 @@ import {
import { selectEmployeeId } from '@store/app-info/app-info.selectors';

export interface ExaminerRecordsPageStateData {
routeGrid: ExaminerRecordData<string>[],
manoeuvresGrid: ExaminerRecordData<string>[],
showMeQuestionsGrid: ExaminerRecordData<string>[],
independentDrivingGrid: ExaminerRecordData<string>[],
tellMeQuestionsGrid: ExaminerRecordData<string>[],
safetyGrid: ExaminerRecordData<string>[],
balanceGrid: ExaminerRecordData<string>[],
routeGrid: ExaminerRecordDataWithPercentage<string>[],
manoeuvresGrid: ExaminerRecordDataWithPercentage<string>[],
showMeQuestionsGrid: ExaminerRecordDataWithPercentage<string>[],
independentDrivingGrid: ExaminerRecordDataWithPercentage<string>[],
tellMeQuestionsGrid: ExaminerRecordDataWithPercentage<string>[],
safetyGrid: ExaminerRecordDataWithPercentage<string>[],
balanceGrid: ExaminerRecordDataWithPercentage<string>[],
testCount: number,
emergencyStops: ExaminerRecordData<string>[],
circuits: ExaminerRecordData<string>[],
locationList: Omit<ExaminerRecordData<TestCentre>, 'percentage'>[],
categoryList: Omit<ExaminerRecordData<TestCategory>, 'percentage'>[]
emergencyStops: ExaminerRecordDataWithPercentage<string>[],
circuits: ExaminerRecordDataWithPercentage<string>[],
locationList: ExaminerRecordData<TestCentre>[],
categoryList: ExaminerRecordData<TestCategory>[]
}

interface ExaminerRecordsState {
cachedRecords$: Observable<ExaminerRecordModel[]>;
isLoadingRecords$: Observable<boolean>;
routeNumbers$: Observable<ExaminerRecordData<string>[]>;
manoeuvres$: Observable<ExaminerRecordData<string>[]>;
showMeQuestions$: Observable<ExaminerRecordData<string>[]>;
tellMeQuestions$: Observable<ExaminerRecordData<string>[]>;
safetyQuestions$: Observable<ExaminerRecordData<string>[]>;
balanceQuestions$: Observable<ExaminerRecordData<string>[]>;
independentDriving$: Observable<ExaminerRecordData<string>[]>;
routeNumbers$: Observable<ExaminerRecordDataWithPercentage<string>[]>;
manoeuvres$: Observable<ExaminerRecordDataWithPercentage<string>[]>;
showMeQuestions$: Observable<ExaminerRecordDataWithPercentage<string>[]>;
tellMeQuestions$: Observable<ExaminerRecordDataWithPercentage<string>[]>;
safetyQuestions$: Observable<ExaminerRecordDataWithPercentage<string>[]>;
balanceQuestions$: Observable<ExaminerRecordDataWithPercentage<string>[]>;
independentDriving$: Observable<ExaminerRecordDataWithPercentage<string>[]>;
testCount$: Observable<number>;
locationList$: Observable<Omit<ExaminerRecordData<TestCentre>, 'percentage'>[]>;
categoryList$: Observable<Omit<ExaminerRecordData<TestCategory>, 'percentage'>[]>;
emergencyStops$: Observable<ExaminerRecordData<string>[]>;
circuits$: Observable<ExaminerRecordData<string>[]>;
locationList$: Observable<ExaminerRecordData<TestCentre>[]>;
categoryList$: Observable<ExaminerRecordData<TestCategory>[]>;
emergencyStops$: Observable<ExaminerRecordDataWithPercentage<string>[]>;
circuits$: Observable<ExaminerRecordDataWithPercentage<string>[]>;
}

@Component({
Expand Down Expand Up @@ -411,20 +412,20 @@ export class ExaminerRecordsPage implements OnInit {
* - If not, finds the most common category and sets it as the default.
* - If yes, calls `changeEligibleTests` to update the eligible tests.
*
* @param {Omit<ExaminerRecordData<TestCategory>, 'percentage'>[]} categories - The array of category data to
* @param {ExaminerRecordData<TestCategory>[]} categories - The array of category data to
* set up the select list.
*/
setupCategorySelectList(categories: Omit<ExaminerRecordData<TestCategory>, 'percentage'>[]) {
setupCategorySelectList(categories: ExaminerRecordData<TestCategory>[]) {
this.categoryFilterOptions = [];

//add every completed category to category array
categories.forEach((val: Omit<ExaminerRecordData<TestCategory>, 'percentage'>) => {
categories.forEach((val: ExaminerRecordData<TestCategory>) => {
this.categoryFilterOptions.push(val.item);
});

if (!this.categoryFilterOptions.includes(this.categorySubject$.value)) {
//find most common category and set it as the default
const mostUsed: Omit<ExaminerRecordData<TestCategory>, 'percentage'> = this.setDefault(categories);
const mostUsed: ExaminerRecordData<TestCategory> = this.setDefault(categories);

if (!!mostUsed) {
this.categoryPlaceholder = mostUsed.item;
Expand All @@ -445,14 +446,14 @@ export class ExaminerRecordsPage implements OnInit {
* 3. Checks if the current location is included in the `locationFilterOptions`.
* - If not, finds the most common location and sets it as the default.
*
* @param {Omit<ExaminerRecordData<TestCentre>, 'percentage'>[]} locations - The array of location data to
* @param {ExaminerRecordData<TestCentre>[]} locations - The array of location data to
* set up the select list.
*/
setupLocationSelectList(locations: Omit<ExaminerRecordData<TestCentre>, 'percentage'>[]) {
setupLocationSelectList(locations: ExaminerRecordData<TestCentre>[]) {
this.locationFilterOptions = [];

//add every visited location to location array
locations.forEach((val: Omit<ExaminerRecordData<TestCentre>, 'percentage'>) => {
locations.forEach((val: ExaminerRecordData<TestCentre>) => {
if (!val.item?.centreName) {
// Should there be no centre name available, display cost code or centre id,
// depending on whether cost code is available
Expand All @@ -469,7 +470,7 @@ export class ExaminerRecordsPage implements OnInit {
if (!this.locationFilterOptions.map(({ centreId }) => centreId)
.includes(this.locationSubject$.value)) {
//find most common location and set it as the default
const mostUsed: Omit<ExaminerRecordData<TestCentre>, 'percentage'> = this.setDefault(locations);
const mostUsed: ExaminerRecordData<TestCentre> = this.setDefault(locations);
if (!!mostUsed) {
this.locationPlaceholder = mostUsed.item.centreName;
this.handleLocationFilter(mostUsed.item);
Expand Down Expand Up @@ -522,13 +523,13 @@ export class ExaminerRecordsPage implements OnInit {
circuits$: this.getTestsByParameters(getCircuits),
locationList$: this.getLocationsByParameters(getLocations)
.pipe(
tap((value: Omit<ExaminerRecordData<TestCentre>, 'percentage'>[]) => {
tap((value: ExaminerRecordData<TestCentre>[]) => {
this.setupLocationSelectList(value);
}),
),
categoryList$: this.getCategoriesByParameters(getCategories)
.pipe(
tap((value: Omit<ExaminerRecordData<TestCategory>, 'percentage'>[]) => {
tap((value: ExaminerRecordData<TestCategory>[]) => {
this.setupCategorySelectList(value)
}),
),
Expand Down Expand Up @@ -644,11 +645,11 @@ export class ExaminerRecordsPage implements OnInit {
* 2. Uses the reduce function to find the element with the highest count.
*
* @template T The type of the elements in the data array.
* @param {Omit<ExaminerRecordData<T>, 'percentage'>[]} data - The array of data elements to search.
* @returns {Omit<ExaminerRecordData<T>, 'percentage'> | null} The element with the highest count, or null if the
* @param {ExaminerRecordData<T>[]} data - The array of data elements to search.
* @returns {ExaminerRecordData<T>} The element with the highest count, or null if the
* input data is null or empty.
*/
setDefault<T>(data: Omit<ExaminerRecordData<T>, 'percentage'>[]): Omit<ExaminerRecordData<T>, 'percentage'> {
setDefault<T>(data: ExaminerRecordData<T>[]): ExaminerRecordData<T> {
if (!data || data?.length === 0) {
return null;
}
Expand Down Expand Up @@ -857,11 +858,11 @@ export class ExaminerRecordsPage implements OnInit {
* by summing up the `count` property of each object in the array.
*
* @template T The type of the data in the `ExaminerRecordData` objects.
* @param {ExaminerRecordData<T>[]} value - The array of `ExaminerRecordData` objects.
* @param {ExaminerRecordDataWithPercentage<T>[]} value - The array of `ExaminerRecordData` objects.
* @returns {number} The total count of individual instances of data.
*/
getTotal = <T>(
value: ExaminerRecordData<T>[],
value: ExaminerRecordDataWithPercentage<T>[],
): number => value.reduce((total, val) => total + Number(val.count), 0);

/**
Expand Down
22 changes: 13 additions & 9 deletions src/app/pages/examiner-records/examiner-records.selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import { ExaminerRecordModel } from '@dvsa/mes-microservice-common/domain/examin
export interface ExaminerRecordData<T> {
item: T;
count: number;
}

// Generic `T` is the configurable type of the item
export interface ExaminerRecordDataWithPercentage<T> extends ExaminerRecordData<T>{
percentage: string;
}

Expand Down Expand Up @@ -97,7 +101,7 @@ export const getLocations = (
startedTests: ExaminerRecordModel[],
range: DateRange = null,
// Omit is a TS type, to remove a property from an interface
): Omit<ExaminerRecordData<TestCentre>, 'percentage'>[] => {
): Omit<ExaminerRecordDataWithPercentage<TestCentre>, 'percentage'>[] => {
if (startedTests) {
const data: ExaminerRecordModel[] = getEligibleTests(startedTests, null, range, null)
.filter((record) => !!get(record, 'testCentre', null).centreId);
Expand All @@ -121,7 +125,7 @@ export const getLocations = (
export const getIndependentDrivingStats = (
startedTests: ExaminerRecordModel[],
category: TestCategory,
): ExaminerRecordData<string>[] => {
): ExaminerRecordDataWithPercentage<string>[] => {
//IndependentDriving is not applicable to the following categories, and so we can avoid the entire function
if (!startedTests || !category || isAnyOf(category, [
TestCategory.ADI3, TestCategory.SC,
Expand Down Expand Up @@ -165,7 +169,7 @@ export const getIndependentDrivingStats = (
export const getCircuits = (
startedTests: ExaminerRecordModel[],
category: TestCategory,
): ExaminerRecordData<string>[] => {
): ExaminerRecordDataWithPercentage<string>[] => {
//getCircuits is only applicable to the following categories, and so we can avoid the entire function
if (!startedTests || !category || !isAnyOf(category, [
TestCategory.EUA1M1, TestCategory.EUA2M1, TestCategory.EUAM1, TestCategory.EUAMM1,
Expand Down Expand Up @@ -238,7 +242,7 @@ export const getStartedTestCount = (
*/
export const getRouteNumbers = (
startedTests: ExaminerRecordModel[],
): ExaminerRecordData<string>[] => {
): ExaminerRecordDataWithPercentage<string>[] => {
if (startedTests) {
const data = (startedTests)
.filter((record: ExaminerRecordModel) => get(record, 'routeNumber', null) !== null);
Expand All @@ -264,7 +268,7 @@ export const getRouteNumbers = (
export const getSafetyQuestions = (
startedTests: ExaminerRecordModel[],
category: TestCategory = null,
): ExaminerRecordData<string>[] => {
): ExaminerRecordDataWithPercentage<string>[] => {
const qp = new QuestionProvider();

if (startedTests) {
Expand Down Expand Up @@ -305,7 +309,7 @@ export const getSafetyQuestions = (
export const getBalanceQuestions = (
startedTests: ExaminerRecordModel[],
category: TestCategory = null,
): ExaminerRecordData<string>[] => {
): ExaminerRecordDataWithPercentage<string>[] => {
const qp = new QuestionProvider();

if (startedTests) {
Expand Down Expand Up @@ -344,7 +348,7 @@ export const getBalanceQuestions = (
export const getShowMeQuestions = (
startedTests: ExaminerRecordModel[],
category: TestCategory = null,
): ExaminerRecordData<string>[] => {
): ExaminerRecordDataWithPercentage<string>[] => {
const qp = new QuestionProvider();

if (startedTests) {
Expand Down Expand Up @@ -376,7 +380,7 @@ export const getShowMeQuestions = (
export const getTellMeQuestions = (
startedTests: ExaminerRecordModel[],
category: TestCategory = null,
): ExaminerRecordData<string>[] => {
): ExaminerRecordDataWithPercentage<string>[] => {
const qp = new QuestionProvider();

if (startedTests) {
Expand Down Expand Up @@ -422,7 +426,7 @@ export const getManoeuvreTypeLabels = (category: TestCategory, type?: ManoeuvreT
export const getManoeuvresUsed = (
startedTests: ExaminerRecordModel[],
category: TestCategory = null,
): ExaminerRecordData<string>[] => {
): ExaminerRecordDataWithPercentage<string>[] => {
let faultsEncountered: string[] = [];
let manoeuvreTypeLabels: string[] = [];
if (category) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/common/chart/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/cor
import ApexCharts from 'apexcharts';
import { ApexAxisChartSeries, ApexNonAxisChartSeries, ApexOptions, ChartType } from 'ng-apexcharts';
import { isEqual } from 'lodash-es';
import { ExaminerRecordData } from '@pages/examiner-records/examiner-records.selector';
import { ExaminerRecordDataWithPercentage } from '@pages/examiner-records/examiner-records.selector';

@Component({
selector: 'chart',
Expand All @@ -19,7 +19,7 @@ export class ChartComponent implements OnInit, OnChanges {
public chartType: ChartType = 'pie';

@Input()
public passedData: ExaminerRecordData<any>[] = null;
public passedData: ExaminerRecordDataWithPercentage<any>[] = null;

@Input()
public showLegend: boolean = false;
Expand Down

0 comments on commit ffd2f48

Please sign in to comment.