From 99e8009dadf1eabacd79a0abd970f08547b72c9e Mon Sep 17 00:00:00 2001 From: Ivan Shmidt Date: Fri, 29 May 2020 16:51:25 +0200 Subject: [PATCH] feat: success and error from the api Co-authored-by: Nikita Dudnik Co-authored-by: Ben Lambert --- plugins/catalog/src/api/CatalogClient.ts | 3 +++ plugins/catalog/src/api/types.ts | 1 + .../RegisterComponentPage.tsx | 18 +++++++++++++++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/plugins/catalog/src/api/CatalogClient.ts b/plugins/catalog/src/api/CatalogClient.ts index 94ee9aa4b3..1b3b26a11e 100644 --- a/plugins/catalog/src/api/CatalogClient.ts +++ b/plugins/catalog/src/api/CatalogClient.ts @@ -55,6 +55,9 @@ export class CatalogClient implements CatalogApi { body: JSON.stringify({ type, target }), }, ); + if (response.status !== 201) { + throw new Error(`Location wasn't added: ${target}`); + } const location = await response.json(); if (location) return location; throw new Error(`'Location wasn't added: ${target}`); diff --git a/plugins/catalog/src/api/types.ts b/plugins/catalog/src/api/types.ts index eb2cdca562..a016842e41 100644 --- a/plugins/catalog/src/api/types.ts +++ b/plugins/catalog/src/api/types.ts @@ -25,4 +25,5 @@ export const catalogApiRef = createApiRef({ export interface CatalogApi { getEntities(): Promise; getEntityByName(name: string): Promise; + addLocation(type: string, target: string): Promise<{}>; } diff --git a/plugins/register-component/src/components/RegisterComponentPage/RegisterComponentPage.tsx b/plugins/register-component/src/components/RegisterComponentPage/RegisterComponentPage.tsx index 33029a012d..b44e3aa238 100644 --- a/plugins/register-component/src/components/RegisterComponentPage/RegisterComponentPage.tsx +++ b/plugins/register-component/src/components/RegisterComponentPage/RegisterComponentPage.tsx @@ -14,7 +14,7 @@ * limitations under the License. */ -import React, { FC, useEffect, useState } from 'react'; +import React, { FC, useState } from 'react'; import { Grid } from '@material-ui/core'; import { InfoCard, @@ -24,6 +24,8 @@ import { ContentHeader, SupportButton, useApi, + alertApiRef, + errorApiRef, } from '@backstage/core'; import RegisterComponentForm from '../RegisterComponentForm'; import { catalogApiRef } from '@backstage/plugin-catalog'; @@ -32,12 +34,22 @@ const RegisterComponentPage: FC<{}> = () => { const catalogApi = useApi(catalogApiRef); const [isSubmitting, setIsSubmitting] = useState(false); + const alertApi = useApi(alertApiRef); + const errorApi = useApi(errorApiRef); + const onSubmit = async (formData: { componentIdInput: string }) => { setIsSubmitting(true); const { componentIdInput: target } = formData; - - const location = await catalogApi.addLocation('github', target); + try { + await catalogApi.addLocation('github', target); + alertApi.post({ + message: 'Successfully added the location', + severity: 'success', + }); + } catch (e) { + errorApi.post(e); + } setIsSubmitting(false); };