diff --git a/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-group-select.component.html b/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-group-select.component.html index ece7a2c946..b40eb8efd8 100644 --- a/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-group-select.component.html +++ b/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-group-select.component.html @@ -1,7 +1,7 @@ this.parentGroups = data, () => {}); + this.informalTaxonService.informalTaxonGroupGetParent(this.id, this.translate.currentLang) + .subscribe(data => this.parentGroup = data, () => {}); } } diff --git a/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-list-breadcrumb/informal-list-breadcrumb.component.html b/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-list-breadcrumb/informal-list-breadcrumb.component.html index 6f2712d3a5..736a987e62 100644 --- a/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-list-breadcrumb/informal-list-breadcrumb.component.html +++ b/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-list-breadcrumb/informal-list-breadcrumb.component.html @@ -2,8 +2,8 @@
  • {{ 'observation.active.informalTaxonGroupAll' | translate }}
  • -
  • - {{ group.name }} +
  • + {{ parentGroup.name }}
  • {{ informalGroup.name }} diff --git a/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-list-breadcrumb/informal-list-breadcrumb.component.ts b/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-list-breadcrumb/informal-list-breadcrumb.component.ts index c7e167c705..2011dd0087 100644 --- a/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-list-breadcrumb/informal-list-breadcrumb.component.ts +++ b/projects/laji/src/app/+taxonomy/browse-species/informal-group-select/informal-list-breadcrumb/informal-list-breadcrumb.component.ts @@ -9,6 +9,6 @@ import { InformalTaxonGroup } from '../../../../shared/model/InformalTaxonGroup' export class InformalListBreadcrumbComponent { @Input() informalGroup?: InformalTaxonGroup; - @Input() parentGroups!: Array; + @Input() parentGroup?: InformalTaxonGroup; @Output() informalGroupSelect = new EventEmitter(); } diff --git a/projects/laji/src/app/shared-modules/extended-group-select/extended-group-select.component.ts b/projects/laji/src/app/shared-modules/extended-group-select/extended-group-select.component.ts index c802ac2860..968d358701 100644 --- a/projects/laji/src/app/shared-modules/extended-group-select/extended-group-select.component.ts +++ b/projects/laji/src/app/shared-modules/extended-group-select/extended-group-select.component.ts @@ -10,6 +10,7 @@ import { SelectedOption, TreeOptionsChangeEvent, TreeOptionsNode } from '../tree import { Util } from '../../shared/service/util.service'; import { WarehouseQueryInterface } from '../../shared/model/WarehouseQueryInterface'; import { RedListTaxonGroup } from '../../shared/model/RedListTaxonGroup'; +import { ArrayResult } from '../../shared/model/ArrayResult'; export interface InformalGroupEvent { [key: string]: string[]; @@ -60,7 +61,7 @@ export abstract class ExtendedGroupSelectComponent>; abstract convertToInformalTaxonGroup(group: T): InformalTaxonGroup; - abstract getTree(lang: string): Observable>; + abstract getTree(lang: string): Observable>; abstract getOptions(query: Record): string[][]; abstract prepareEmit(includedOptions: string[], excludedOptions?: string[]): InformalGroupEvent; diff --git a/projects/laji/src/app/shared-modules/extended-group-select/observation-extended-group-select.component.ts b/projects/laji/src/app/shared-modules/extended-group-select/observation-extended-group-select.component.ts index 5e1a55476f..f8d24292a5 100644 --- a/projects/laji/src/app/shared-modules/extended-group-select/observation-extended-group-select.component.ts +++ b/projects/laji/src/app/shared-modules/extended-group-select/observation-extended-group-select.component.ts @@ -9,6 +9,7 @@ import { ExtendedGroupSelectComponent, InformalGroupEvent } from './extended-gro import { InformalTaxonGroup } from '../../shared/model/InformalTaxonGroup'; import { PagedResult } from '../../shared/model/PagedResult'; import { RedListTaxonGroup } from '../../shared/model/RedListTaxonGroup'; +import { ArrayResult } from '../../shared/model/ArrayResult'; export const OBSERVATION_GROUP_SELECT_VALUE_ACCESSOR: any = { provide: NG_VALUE_ACCESSOR, @@ -39,14 +40,14 @@ export class ObservationExtendedGroupSelectComponent extends ExtendedGroupSelect } findByIds(groupIds: string[], lang: string): Observable> { - return this.informalTaxonService.informalTaxonGroupFind(lang, undefined, undefined, {idIn: groupIds}); + return this.informalTaxonService.informalTaxonGroupFind(lang, undefined, undefined, groupIds); } convertToInformalTaxonGroup(group: InformalTaxonGroup): InformalTaxonGroup { return {...group}; } - getTree(lang: string): Observable> { + getTree(lang: string): Observable> { return this.informalTaxonService.informalTaxonGroupGetTree(lang); } diff --git a/projects/laji/src/app/shared/api/InformalTaxonGroupApi.ts b/projects/laji/src/app/shared/api/InformalTaxonGroupApi.ts index 5c635fbec0..6827573dd4 100644 --- a/projects/laji/src/app/shared/api/InformalTaxonGroupApi.ts +++ b/projects/laji/src/app/shared/api/InformalTaxonGroupApi.ts @@ -29,13 +29,11 @@ import { HttpClient } from '@angular/common/http'; import { Util } from '../service/util.service'; import { InformalTaxonGroup } from '../model/InformalTaxonGroup'; import { environment } from '../../../environments/environment'; - - -'use strict'; +import { ArrayResult } from '../model/ArrayResult'; @Injectable({providedIn: 'root'}) export class InformalTaxonGroupApi { - protected basePath = environment.apiBase; + protected basePath = `${environment.apiBase}/informal-taxon-groups`; constructor(protected http: HttpClient) { } @@ -47,12 +45,8 @@ export class InformalTaxonGroupApi { * @param page Page number * @param pageSize Page size */ - public informalTaxonGroupFind(lang?: string, page?: string, pageSize?: string, extraHttpRequestParams?: any): Observable> { - const path = this.basePath + '/informal-taxon-groups'; - - const queryParameters = {...Util.removeFromObject(extraHttpRequestParams)}; - - return this.httpGet(path, queryParameters, lang, page, pageSize); + public informalTaxonGroupFind(lang?: string, page?: string, pageSize?: string, idIn?: string[]): Observable> { + return this.http.get>(this.basePath, { params: Util.withNonNullableValues({ lang, page, pageSize, idIn }) }); } /** @@ -61,20 +55,11 @@ export class InformalTaxonGroupApi { * @param id * @param lang Language of fields that have multiple languages. Return english if asked language not found. If multi is selected fields will contain language objects */ - public informalTaxonGroupFindById(id: string, lang?: string, extraHttpRequestParams?: any): Observable { - const path = this.basePath + '/informal-taxon-groups/{id}' - .replace('{' + 'id' + '}', String(id)); - - const queryParameters = {...Util.removeFromObject(extraHttpRequestParams)}; - // verify required parameter 'id' is not null or undefined - if (id === null || id === undefined) { - throw new Error('Required parameter id was null or undefined when calling informalTaxonGroupFindById.'); + public informalTaxonGroupFindById(id: string, lang?: string): Observable { + if (!id.length) { + throw new Error('Required parameter id was empty when calling informalTaxonGroupFindById.'); } - if (lang !== undefined) { - queryParameters['lang'] = lang; - } - - return this.http.get(path, {params: queryParameters}); + return this.http.get(`${this.basePath}/${id}`, {params: Util.withNonNullableValues({ lang })}); } /** @@ -84,12 +69,8 @@ export class InformalTaxonGroupApi { * @param page Page number * @param pageSize Page size */ - public informalTaxonGroupFindRoots(lang?: string, page?: string, pageSize?: string, extraHttpRequestParams?: any): Observable> { - const path = this.basePath + '/informal-taxon-groups/roots'; - - const queryParameters = {...Util.removeFromObject(extraHttpRequestParams)}; - - return this.httpGet(path, queryParameters, lang, page, pageSize); + public informalTaxonGroupFindRoots(lang?: string): Observable> { + return this.http.get>(`${this.basePath}/roots`, { params: Util.withNonNullableValues({ lang }) }); } /** @@ -100,60 +81,18 @@ export class InformalTaxonGroupApi { * @param page Page number * @param pageSize Page size */ - public informalTaxonGroupGetChildren(id: string, lang?: string, page?: string, pageSize?: string, extraHttpRequestParams?: any): Observable> { - const path = this.basePath + '/informal-taxon-groups/{id}/children' - .replace('{' + 'id' + '}', String(id)); - - const queryParameters = {...Util.removeFromObject(extraHttpRequestParams)}; - // verify required parameter 'id' is not null or undefined - if (id === null || id === undefined) { - throw new Error('Required parameter id was null or undefined when calling informalTaxonGroupGetChildren.'); - } - - return this.httpGet(path, queryParameters, lang, page, pageSize); - } - - /** - * Get parents for a informal group - * - * @param id - * @param lang Language of fields that have multiple languages. Return english if asked language not found. - */ - public informalTaxonGroupGetParents(id: string, lang?: string, extraHttpRequestParams?: any): Observable> { - const path = this.basePath + '/informal-taxon-groups/{id}/parents' - .replace('{' + 'id' + '}', String(id)); - - const queryParameters = {...Util.removeFromObject(extraHttpRequestParams)}; - // verify required parameter 'id' is not null or undefined - if (id === null || id === undefined) { - throw new Error('Required parameter id was null or undefined when calling informalTaxonGroupGetChildren.'); - } - if (lang !== undefined) { - queryParameters['lang'] = lang; - } - - return this.http.get(path, {params: queryParameters}); + public informalTaxonGroupGetChildren(id: string, lang?: string): Observable> { + return this.http.get>(`${this.basePath}/${id}/children`, { params: Util.withNonNullableValues({ lang }) } ); } /** - * Get current groups parents and parents siblings + * Get parent for a informal group * * @param id * @param lang Language of fields that have multiple languages. Return english if asked language not found. - * @param page Page number - * @param pageSize Page size */ - public informalTaxonGroupGetParentLevel(id: string, lang?: string, page?: string, pageSize?: string, extraHttpRequestParams?: any): Observable> { - const path = this.basePath + '/informal-taxon-groups/{id}/parentLevel' - .replace('{' + 'id' + '}', String(id)); - - const queryParameters = {...Util.removeFromObject(extraHttpRequestParams)}; - // verify required parameter 'id' is not null or undefined - if (id === null || id === undefined) { - throw new Error('Required parameter id was null or undefined when calling informalTaxonGroupGetParentLevel.'); - } - - return this.httpGet(path, queryParameters, lang, page, pageSize); + public informalTaxonGroupGetParent(id: string, lang?: string): Observable { + return this.http.get(`${this.basePath}/${id}/parent`, { params: Util.withNonNullableValues({ lang })}); } /** @@ -163,11 +102,8 @@ export class InformalTaxonGroupApi { * @param page Page number * @param pageSize Page size */ - public informalTaxonGroupGetTree(lang?: string, page?: string, pageSize?: string, extraHttpRequestParams?: any): Observable> { - const path = this.basePath + '/informal-taxon-groups/tree'; - const queryParameters = {...Util.removeFromObject(extraHttpRequestParams)}; - - return this.httpGet(path, queryParameters, lang, page, pageSize); + public informalTaxonGroupGetTree(lang?: string): Observable> { + return this.http.get>(`${this.basePath}/tree`, { params: Util.withNonNullableValues({ lang }) }); } /** @@ -178,32 +114,7 @@ export class InformalTaxonGroupApi { * @param page Page number * @param pageSize Page size */ - public informalTaxonGroupGetWithSiblings(id: string, lang?: string, page?: string, pageSize?: string, extraHttpRequestParams?: any): Observable> { - const path = this.basePath + '/informal-taxon-groups/{id}/siblings' - .replace('{' + 'id' + '}', String(id)); - - const queryParameters = {...Util.removeFromObject(extraHttpRequestParams)}; - // verify required parameter 'id' is not null or undefined - if (id === null || id === undefined) { - throw new Error('Required parameter id was null or undefined when calling informalTaxonGroupGetWithSiblings.'); - } - - return this.httpGet(path, queryParameters, lang, page, pageSize); - } - - private httpGet(path: string, queryParameters: any, lang?: string, page?: string, pageSize?: string) { - if (lang !== undefined) { - queryParameters['lang'] = lang; - } - - if (page !== undefined) { - queryParameters['page'] = page; - } - - if (pageSize !== undefined) { - queryParameters['pageSize'] = pageSize; - } - - return this.http.get>(path, { params: queryParameters }); + public informalTaxonGroupGetWithSiblings(id: string, lang?: string): Observable> { + return this.http.get>(`${this.basePath}/${id}/siblings`, { params: Util.withNonNullableValues({ lang }) }); } } diff --git a/projects/laji/src/app/shared/group-select/group-select.component.ts b/projects/laji/src/app/shared/group-select/group-select.component.ts index d3ec1fdd40..a326d84913 100644 --- a/projects/laji/src/app/shared/group-select/group-select.component.ts +++ b/projects/laji/src/app/shared/group-select/group-select.component.ts @@ -7,6 +7,7 @@ import { Logger } from '../logger/logger.service'; import { TranslateService } from '@ngx-translate/core'; import { Group } from '../model/Group'; import { PagedResult } from '../model/PagedResult'; +import { ArrayResult } from '../model/ArrayResult'; @Directive() export abstract class GroupSelectComponent implements ControlValueAccessor, OnChanges { @@ -152,7 +153,7 @@ export abstract class GroupSelectComponent implements ControlVa } } - getRoot(lang: string): Observable> { + getRoot(lang: string): Observable> { if (this.rootGroups) { return this.findByIds(this.rootGroups, lang); } @@ -161,9 +162,9 @@ export abstract class GroupSelectComponent implements ControlVa abstract findById(groupId: string, lang: string): Observable; abstract findByIds(groupIds: string[], lang: any): Observable>; - abstract getWithSiblings(groupId: any, lang: string): Observable>; - abstract getChildren(groupId: any, lang: string): Observable>; - abstract findRoots(lang: any): Observable>; + abstract getWithSiblings(groupId: any, lang: string): Observable>; + abstract getChildren(groupId: any, lang: string): Observable>; + abstract findRoots(lang: any): Observable>; abstract convertToInformalTaxonGroup(group: T): InformalTaxonGroup; empty() { diff --git a/projects/laji/src/app/shared/group-select/observation-group-select.component.ts b/projects/laji/src/app/shared/group-select/observation-group-select.component.ts index dbb87cd7d9..d7680b1aa1 100644 --- a/projects/laji/src/app/shared/group-select/observation-group-select.component.ts +++ b/projects/laji/src/app/shared/group-select/observation-group-select.component.ts @@ -8,6 +8,7 @@ import { GroupSelectComponent } from './group-select.component'; import { InformalTaxonGroup } from '../model/InformalTaxonGroup'; import { PagedResult } from '../model/PagedResult'; import { RedListTaxonGroup } from '../model/RedListTaxonGroup'; +import { ArrayResult } from '../model/ArrayResult'; export const OBSERVATION_GROUP_SELECT_VALUE_ACCESSOR: any = { provide: NG_VALUE_ACCESSOR, @@ -39,18 +40,18 @@ export class ObservationGroupSelectComponent extends GroupSelectComponent> { - return this.informalTaxonService.informalTaxonGroupFind(lang, undefined, undefined, {idIn: groupIds}); + return this.informalTaxonService.informalTaxonGroupFind(lang, undefined, undefined, groupIds); } - getWithSiblings(groupId: string, lang: string | undefined): Observable> { + getWithSiblings(groupId: string, lang: string | undefined): Observable> { return this.informalTaxonService.informalTaxonGroupGetWithSiblings(groupId, lang); } - getChildren(groupId: string, lang: string | undefined): Observable> { + getChildren(groupId: string, lang: string | undefined): Observable> { return this.informalTaxonService.informalTaxonGroupGetChildren(groupId, lang); } - findRoots(lang: string | undefined): Observable> { + findRoots(lang: string | undefined): Observable> { return this.informalTaxonService.informalTaxonGroupFindRoots(lang); } diff --git a/projects/laji/src/app/shared/model/ArrayResult.ts b/projects/laji/src/app/shared/model/ArrayResult.ts new file mode 100644 index 0000000000..9a59456dec --- /dev/null +++ b/projects/laji/src/app/shared/model/ArrayResult.ts @@ -0,0 +1,4 @@ + +export interface ArrayResult { + results: T[]; +} diff --git a/projects/laji/src/app/shared/service/util.service.ts b/projects/laji/src/app/shared/service/util.service.ts index d393e4a407..4eae03c535 100644 --- a/projects/laji/src/app/shared/service/util.service.ts +++ b/projects/laji/src/app/shared/service/util.service.ts @@ -2,6 +2,10 @@ import { NavigationEnd, Event } from '@angular/router'; import * as merge from 'deepmerge'; import { Document } from '../model/Document'; +export type WithNonNullableKeys = T & { + [P in K]-?: NonNullable; +}; + export class Util { /** * Clones the object using JSON stringify @@ -66,6 +70,21 @@ export class Util { }, {} as Partial); } + /** + * Remove all undefined and null values from object + * + * @param obj object to remove keys from + */ + public static withNonNullableValues(obj: T): WithNonNullableKeys { + return Object.keys(obj).reduce((cumulative, current: keyof T) => { + if (obj[current] !== undefined && obj[current] !== null) { + (cumulative as any)[current] = obj[current]; + } + return cumulative; + }, {} as WithNonNullableKeys); + } + + /** * Add leading zero so that the length of return string will be 2 */