fix(catalog): add types and clean up code

This commit is contained in:
Nikita Nek Dudnik
2020-06-11 10:56:35 +02:00
parent e7862589f6
commit 94a23357f2
9 changed files with 47 additions and 45 deletions
@@ -28,7 +28,7 @@ describe('DatabaseEntitiesCatalog', () => {
updateEntity: jest.fn(),
entities: jest.fn(),
entity: jest.fn(),
entityById: jest.fn(),
entityByUid: jest.fn(),
removeEntity: jest.fn(),
addLocation: jest.fn(),
removeLocation: jest.fn(),
@@ -81,7 +81,7 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog {
async removeEntityByUid(uid: string): Promise<void> {
return await this.database.transaction(async tx => {
const currentEntity = await this.database.entityById(tx, uid);
const currentEntity = await this.database.entityByUid(tx, uid);
if (!currentEntity) {
throw new NotFoundError(`Entity with ID ${uid} was not found`);
}
@@ -319,7 +319,7 @@ export class CommonDatabase implements Database {
return toEntityResponse(rows[0]);
}
async entityById(
async entityByUid(
txOpaque: unknown,
id: string,
): Promise<DbEntityResponse | undefined> {
@@ -130,7 +130,7 @@ export type Database = {
namespace?: string,
): Promise<DbEntityResponse | undefined>;
entityById(tx: unknown, id: string): Promise<DbEntityResponse | undefined>;
entityByUid(tx: unknown, uid: string): Promise<DbEntityResponse | undefined>;
removeEntity(tx: unknown, uid: string): Promise<void>;
@@ -18,7 +18,7 @@ import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { wrapInTestApp } from '@backstage/test-utils';
import { CatalogFilter, CatalogFilterGroup } from './CatalogFilter';
import { FilterGroupItem } from '../../types';
import { EntityFilterType } from '../../data/filters';
describe('Catalog Filter', () => {
it('should render the different groups', async () => {
@@ -41,11 +41,11 @@ describe('Catalog Filter', () => {
name: 'Test Group 1',
items: [
{
id: FilterGroupItem.ALL,
id: EntityFilterType.ALL,
label: 'First Label',
},
{
id: FilterGroupItem.STARRED,
id: EntityFilterType.STARRED,
label: 'Second Label',
},
],
@@ -68,12 +68,12 @@ describe('Catalog Filter', () => {
name: 'Test Group 1',
items: [
{
id: FilterGroupItem.ALL,
id: EntityFilterType.ALL,
label: 'First Label',
count: 100,
},
{
id: FilterGroupItem.STARRED,
id: EntityFilterType.STARRED,
label: 'Second Label',
count: 400,
},
@@ -97,12 +97,12 @@ describe('Catalog Filter', () => {
name: 'Test Group 1',
items: [
{
id: FilterGroupItem.ALL,
id: EntityFilterType.ALL,
label: 'First Label',
count: 100,
},
{
id: FilterGroupItem.STARRED,
id: EntityFilterType.STARRED,
label: 'Second Label',
count: 400,
},
@@ -136,12 +136,12 @@ describe('Catalog Filter', () => {
name: 'Test Group 1',
items: [
{
id: FilterGroupItem.ALL,
id: EntityFilterType.ALL,
label: 'First Label',
count: () => <b>BACKSTAGE!</b>,
},
{
id: FilterGroupItem.STARRED,
id: EntityFilterType.STARRED,
label: 'Second Label',
count: 400,
},
@@ -25,9 +25,9 @@ import {
makeStyles,
} from '@material-ui/core';
import type { IconComponent } from '@backstage/core';
import { FilterGroupItem } from '../../types';
import { EntityFilterType } from '../../data/filters';
export type CatalogFilterItem = {
id: FilterGroupItem;
id: EntityFilterType;
label: string;
icon?: IconComponent;
count?: number | React.FC;
@@ -15,7 +15,7 @@
*/
import { Entity, LOCATION_ANNOTATION } from '@backstage/catalog-model';
import { Progress, useApi } from '@backstage/core';
import { Progress, useApi, alertApiRef } from '@backstage/core';
import {
Button,
Dialog,
@@ -32,7 +32,6 @@ import React, { FC } from 'react';
import { useAsync } from 'react-use';
import { AsyncState } from 'react-use/lib/useAsync';
import { catalogApiRef } from '../../api/types';
import { alertApiRef } from '../../../../../packages/core-api/src/apis/definitions/AlertApi';
type ComponentRemovalDialogProps = {
open: boolean;
@@ -63,6 +62,17 @@ export const ComponentRemovalDialog: FC<ComponentRemovalDialogProps> = ({
const catalogApi = useApi(catalogApiRef);
const alertApi = useApi(alertApiRef);
const removeEntity = async () => {
const uid = entity.metadata.uid;
try {
await catalogApi.removeEntityByUid(uid!);
} catch (err) {
alertApi.post({ message: err.message });
}
onConfirm();
};
return (
<Dialog fullScreen={fullScreen} open={open} onClose={onClose}>
<DialogTitle id="responsive-dialog-title">
@@ -93,7 +103,7 @@ export const ComponentRemovalDialog: FC<ComponentRemovalDialogProps> = ({
<Typography component="div">
<ul>
<li>
{entities[0]?.metadata?.annotations?.[LOCATION_ANNOTATION]}
{entities[0]?.metadata.annotations?.[LOCATION_ANNOTATION]}
</li>
</ul>
</Typography>
@@ -109,20 +119,7 @@ export const ComponentRemovalDialog: FC<ComponentRemovalDialogProps> = ({
</Button>
<Button
disabled={!!(loading || error)}
onClick={async () => {
const uid = entity.metadata?.uid;
if (uid) {
try {
await catalogApi.removeEntityByUid(uid);
} catch (err) {
alertApi.post({ message: err.message });
}
} else {
alertApi.post({ message: `No entity with UID ${uid}` });
}
onConfirm();
}}
onClick={removeEntity}
color="secondary"
>
Unregister
+18 -7
View File
@@ -21,20 +21,26 @@ import SettingsIcon from '@material-ui/icons/Settings';
import StarIcon from '@material-ui/icons/Star';
import { StarredCount } from '../components/CatalogFilter/StarredCount';
import { AllServicesCount } from '../components/CatalogFilter/AllServicesCount';
import { FilterGroupItem } from '../types';
import { Entity } from '@backstage/catalog-model';
export enum EntityFilterType {
ALL = 'ALL',
STARRED = 'STARRED',
OWNED = 'OWNED',
}
export const filterGroups: CatalogFilterGroup[] = [
{
name: 'Personal',
items: [
{
id: FilterGroupItem.OWNED,
id: EntityFilterType.OWNED,
label: 'Owned',
count: 0,
icon: SettingsIcon,
},
{
id: FilterGroupItem.STARRED,
id: EntityFilterType.STARRED,
label: 'Starred',
count: StarredCount,
icon: StarIcon,
@@ -46,7 +52,7 @@ export const filterGroups: CatalogFilterGroup[] = [
name: 'Company',
items: [
{
id: FilterGroupItem.ALL,
id: EntityFilterType.ALL,
label: 'All Services',
count: AllServicesCount,
},
@@ -54,10 +60,15 @@ export const filterGroups: CatalogFilterGroup[] = [
},
];
type EntityFilter = (entity: Entity) => boolean;
type EntityFilterOptions = {
isStarredEntity: EntityFilter;
};
export const entityFilters = {
[FilterGroupItem.OWNED]: () => () => false,
[FilterGroupItem.ALL]: () => () => true,
[FilterGroupItem.STARRED]: ({ isStarredEntity }: { isStarredEntity: any }) =>
[EntityFilterType.OWNED]: (): EntityFilter => () => false,
[EntityFilterType.ALL]: (): EntityFilter => () => true,
[EntityFilterType.STARRED]: ({ isStarredEntity }: EntityFilterOptions) =>
isStarredEntity,
};
-6
View File
@@ -157,9 +157,3 @@ export type KindParser = {
envelope: DescriptorEnvelope,
): Promise<DescriptorEnvelope | undefined>;
};
export enum FilterGroupItem {
ALL = 'ALL',
STARRED = 'STARRED',
OWNED = 'OWNED',
}