From e9029614c70332cffa0c9eafe8ad17e3d9ea24d6 Mon Sep 17 00:00:00 2001 From: Camille Moinier Date: Thu, 11 Jul 2024 17:24:25 +0200 Subject: [PATCH] feat: add error message when link is dead --- .../src/e2e/datasetDetailPage.cy.ts | 10 ++++ .../record-api-form.component.html | 15 +++++- .../record-api-form.component.ts | 47 ++++++++++++------- translations/de.json | 1 + translations/en.json | 1 + translations/es.json | 1 + translations/fr.json | 1 + translations/it.json | 1 + translations/nl.json | 1 + translations/pt.json | 1 + translations/sk.json | 1 + 11 files changed, 62 insertions(+), 18 deletions(-) diff --git a/apps/datahub-e2e/src/e2e/datasetDetailPage.cy.ts b/apps/datahub-e2e/src/e2e/datasetDetailPage.cy.ts index cb5a53d9e1..c9b58f735f 100644 --- a/apps/datahub-e2e/src/e2e/datasetDetailPage.cy.ts +++ b/apps/datahub-e2e/src/e2e/datasetDetailPage.cy.ts @@ -738,6 +738,16 @@ describe('api form', () => { .should('not.eq', url) }) }) + describe('When the api link has an error', () => { + beforeEach(() => { + cy.visit('/dataset/ee965118-2416-4d48-b07e-bbc696f002c2') + cy.get('gn-ui-api-card').last().find('button').eq(1).click() + }) + + it('should display the error message', () => { + cy.get('gn-ui-error').should('be.visible') + }) + }) }) describe('userFeedback', () => { diff --git a/libs/ui/elements/src/lib/record-api-form/record-api-form.component.html b/libs/ui/elements/src/lib/record-api-form/record-api-form.component.html index 81e72b4daa..f7cc898a2a 100644 --- a/libs/ui/elements/src/lib/record-api-form/record-api-form.component.html +++ b/libs/ui/elements/src/lib/record-api-form/record-api-form.component.html @@ -1,5 +1,8 @@
-
+
record.metadata.api.form.create @@ -78,7 +81,7 @@
-
+
record.metadata.api.form.customUrl
@@ -88,4 +91,12 @@ >
+ + + +
diff --git a/libs/ui/elements/src/lib/record-api-form/record-api-form.component.ts b/libs/ui/elements/src/lib/record-api-form/record-api-form.component.ts index 5d7d598c10..c46bf78c0c 100644 --- a/libs/ui/elements/src/lib/record-api-form/record-api-form.component.ts +++ b/libs/ui/elements/src/lib/record-api-form/record-api-form.component.ts @@ -6,6 +6,7 @@ import { } from '@geonetwork-ui/common/domain/model/record' import { mimeTypeToFormat } from '@geonetwork-ui/util/shared' import { BehaviorSubject, combineLatest, filter, map, switchMap } from 'rxjs' +import { ErrorType } from '../error/error.component' const DEFAULT_PARAMS = { OFFSET: '', @@ -26,12 +27,20 @@ interface OutputFormats { }) export class RecordApiFormComponent { @Input() set apiLink(value: DatasetServiceDistribution) { + this.isLoading = true this.outputFormats = [{ value: 'json', label: 'JSON' }] this.accessServiceProtocol = value ? value.accessServiceProtocol : undefined this.apiFeatureType = value ? value.name : undefined if (value) { this.apiBaseUrl = value.url.href - this.createEndpoint().then(() => this.parseOutputFormats()) + this.createEndpoint() + .then(() => { + this.parseOutputFormats() + this.isLoading = false + }) + .catch(() => { + this.isLoading = false + }) } this.resetUrl() } @@ -49,17 +58,18 @@ export class RecordApiFormComponent { outputFormats = [{ value: 'json', label: 'JSON' }] endpoint: WfsEndpoint | OgcApiEndpoint | undefined firstCollection: string | undefined + errorTypes = ErrorType + isLoading = false apiQueryUrl$ = combineLatest([ this.offset$, this.limit$, this.format$, - // only compute the url if the endpoint was created - this.endpoint$.pipe(filter((endpoint) => !!endpoint)), + this.endpoint$, ]).pipe( - switchMap(([offset, limit, format]) => - this.generateApiQueryUrl(offset, limit, format) - ) + switchMap(([offset, limit, format]) => { + return this.generateApiQueryUrl(offset, limit, format) || undefined + }) ) noLimitChecked$ = this.limit$.pipe( @@ -85,6 +95,7 @@ export class RecordApiFormComponent { this.offset$.next(DEFAULT_PARAMS.OFFSET) this.limit$.next(DEFAULT_PARAMS.LIMIT) this.format$.next(DEFAULT_PARAMS.FORMAT) + this.isLoading = false } async parseOutputFormats() { @@ -146,7 +157,6 @@ export class RecordApiFormComponent { format: string ): Promise { if (!this.apiBaseUrl || !this.endpoint || !this.apiFeatureType) return '' - const options = { outputFormat: format, startIndex: offset ? Number(offset) : undefined, @@ -154,15 +164,20 @@ export class RecordApiFormComponent { limit: limit !== '-1' ? Number(limit) : limit === '-1' ? -1 : undefined, offset: offset !== '' ? Number(offset) : undefined, } - - if (this.endpoint instanceof WfsEndpoint) { - options.maxFeatures = limit !== '-1' ? Number(limit) : undefined - return this.endpoint.getFeatureUrl(this.apiFeatureType, options) - } else { - return await this.endpoint.getCollectionItemsUrl( - this.firstCollection, - options - ) + try { + let url + if (this.endpoint instanceof WfsEndpoint) { + options.maxFeatures = limit !== '-1' ? Number(limit) : undefined + url = this.endpoint.getFeatureUrl(this.apiFeatureType, options) + } else { + url = await this.endpoint.getCollectionItemsUrl( + this.firstCollection, + options + ) + } + return url + } catch (error) { + return undefined } } } diff --git a/translations/de.json b/translations/de.json index 55feccb7c4..9f85133bb6 100644 --- a/translations/de.json +++ b/translations/de.json @@ -282,6 +282,7 @@ "record.metadata.api.form.closeForm": "Formular schließen", "record.metadata.api.form.create": "Ihre Anfrage erstellen", "record.metadata.api.form.customUrl": "Benutzerdefinierte URL", + "record.metadata.api.form.error": "", "record.metadata.api.form.limit": "Anzahl der Datensätze", "record.metadata.api.form.limit.all": "Alle", "record.metadata.api.form.offset": "Anzahl des ersten Datensatzes", diff --git a/translations/en.json b/translations/en.json index 1f3e28e966..1da7ae9ec0 100644 --- a/translations/en.json +++ b/translations/en.json @@ -282,6 +282,7 @@ "record.metadata.api.form.closeForm": "Close the form", "record.metadata.api.form.create": "Create your request", "record.metadata.api.form.customUrl": "Custom URL", + "record.metadata.api.form.error": "The service returned an error.", "record.metadata.api.form.limit": "Count of records", "record.metadata.api.form.limit.all": "All", "record.metadata.api.form.offset": "Count of first record", diff --git a/translations/es.json b/translations/es.json index 8973368432..7b5d3277bf 100644 --- a/translations/es.json +++ b/translations/es.json @@ -282,6 +282,7 @@ "record.metadata.api.form.closeForm": "", "record.metadata.api.form.create": "", "record.metadata.api.form.customUrl": "", + "record.metadata.api.form.error": "", "record.metadata.api.form.limit": "", "record.metadata.api.form.limit.all": "", "record.metadata.api.form.offset": "", diff --git a/translations/fr.json b/translations/fr.json index fb4d6e7462..6d7e759b4c 100644 --- a/translations/fr.json +++ b/translations/fr.json @@ -282,6 +282,7 @@ "record.metadata.api.form.closeForm": "Fermer le panneau de personnalisation", "record.metadata.api.form.create": "Paramétrer votre requête", "record.metadata.api.form.customUrl": "URL personnalisée", + "record.metadata.api.form.error": "Le service a renvoyé un message d'erreur.", "record.metadata.api.form.limit": "Nombre d'enregistrements", "record.metadata.api.form.limit.all": "Tous", "record.metadata.api.form.offset": "Numéro du 1er enregistrement", diff --git a/translations/it.json b/translations/it.json index e7fff9fb95..1587b1dff5 100644 --- a/translations/it.json +++ b/translations/it.json @@ -282,6 +282,7 @@ "record.metadata.api.form.closeForm": "Chiudi il pannello di personalizzazione", "record.metadata.api.form.create": "Configura la sua richiesta", "record.metadata.api.form.customUrl": "URL personalizzata", + "record.metadata.api.form.error": "", "record.metadata.api.form.limit": "Numero di record", "record.metadata.api.form.limit.all": "Tutti", "record.metadata.api.form.offset": "Numero del primo record", diff --git a/translations/nl.json b/translations/nl.json index 1086aa0994..61c3800b86 100644 --- a/translations/nl.json +++ b/translations/nl.json @@ -282,6 +282,7 @@ "record.metadata.api.form.closeForm": "", "record.metadata.api.form.create": "", "record.metadata.api.form.customUrl": "", + "record.metadata.api.form.error": "", "record.metadata.api.form.limit": "", "record.metadata.api.form.limit.all": "", "record.metadata.api.form.offset": "", diff --git a/translations/pt.json b/translations/pt.json index 761e097ab1..4b69c57fec 100644 --- a/translations/pt.json +++ b/translations/pt.json @@ -282,6 +282,7 @@ "record.metadata.api.form.closeForm": "", "record.metadata.api.form.create": "", "record.metadata.api.form.customUrl": "", + "record.metadata.api.form.error": "", "record.metadata.api.form.limit": "", "record.metadata.api.form.limit.all": "", "record.metadata.api.form.offset": "", diff --git a/translations/sk.json b/translations/sk.json index aa56902b73..c2d0ee2d02 100644 --- a/translations/sk.json +++ b/translations/sk.json @@ -282,6 +282,7 @@ "record.metadata.api.form.closeForm": "Zavrieť formulár", "record.metadata.api.form.create": "Vytvoriť požiadavku", "record.metadata.api.form.customUrl": "Vlastná URL", + "record.metadata.api.form.error": "", "record.metadata.api.form.limit": "Počet záznamov", "record.metadata.api.form.limit.all": "Všetky", "record.metadata.api.form.offset": "Počet prvých záznamov",