feat: success and error from the api

Co-authored-by: Nikita Dudnik <nikdudnik@gmail.com>
Co-authored-by: Ben Lambert <ben@blam.sh>
This commit is contained in:
Ivan Shmidt
2020-05-29 16:51:25 +02:00
parent 304c544af5
commit 99e8009dad
3 changed files with 19 additions and 3 deletions
+3
View File
@@ -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}`);
+1
View File
@@ -25,4 +25,5 @@ export const catalogApiRef = createApiRef<CatalogApi>({
export interface CatalogApi {
getEntities(): Promise<Entity[]>;
getEntityByName(name: string): Promise<Entity>;
addLocation(type: string, target: string): Promise<{}>;
}
@@ -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);
};