Added edit button to UserProfileCard

Signed-off-by: Andre Wanlin <67169551+awanlin@users.noreply.github.com>
This commit is contained in:
Andre Wanlin
2022-08-15 15:27:21 -05:00
parent 4c38d2f5b8
commit ab6650ede9
3 changed files with 114 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-org': patch
---
Added an edit button to the `UserProfileCard` that is enabled when the `backstage.io/edit-url` is present, this matches how the `GroupProfileCard` works
@@ -73,3 +73,86 @@ describe('UserSummary Test', () => {
expect(rendered.getByText('Super awesome human')).toBeInTheDocument();
});
});
describe('Edit Button', () => {
it('Should default to disabled', async () => {
const userEntity: UserEntity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'User',
metadata: {
name: 'calum.leavy',
description: 'Super awesome human',
},
spec: {
profile: {
displayName: 'Calum Leavy',
email: 'calum-leavy@example.com',
},
memberOf: ['ExampleGroup'],
},
relations: [
{
type: 'memberOf',
targetRef: 'group:default/examplegroup',
},
],
};
const rendered = await renderWithEffects(
wrapInTestApp(
<EntityProvider entity={userEntity}>
<UserProfileCard variant="gridItem" />
</EntityProvider>,
{
mountedRoutes: {
'/catalog/:namespace/:kind/:name': entityRouteRef,
},
},
),
);
expect(rendered.getByRole('button')).toBeDisabled();
});
it('Should be enabled when edit URL annotation is present', async () => {
const annotations: Record<string, string> = {
'backstage.io/edit-url': 'https://example.com/user.yaml',
};
const userEntity: UserEntity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'User',
metadata: {
name: 'calum.leavy',
description: 'Super awesome human',
annotations,
},
spec: {
profile: {
displayName: 'Calum Leavy',
email: 'calum-leavy@example.com',
},
memberOf: ['ExampleGroup'],
},
relations: [
{
type: 'memberOf',
targetRef: 'group:default/examplegroup',
},
],
};
const rendered = await renderWithEffects(
wrapInTestApp(
<EntityProvider entity={userEntity}>
<UserProfileCard variant="gridItem" />
</EntityProvider>,
{
mountedRoutes: {
'/catalog/:namespace/:kind/:name': entityRouteRef,
},
},
),
);
expect(rendered.getByRole('button')).toBeEnabled();
});
});
@@ -14,7 +14,11 @@
* limitations under the License.
*/
import { RELATION_MEMBER_OF, UserEntity } from '@backstage/catalog-model';
import {
RELATION_MEMBER_OF,
UserEntity,
ANNOTATION_EDIT_URL,
} from '@backstage/catalog-model';
import {
EntityRefLinks,
getEntityRelations,
@@ -23,12 +27,14 @@ import {
import {
Box,
Grid,
IconButton,
List,
ListItem,
ListItemIcon,
ListItemText,
Tooltip,
} from '@material-ui/core';
import EditIcon from '@material-ui/icons/Edit';
import EmailIcon from '@material-ui/icons/Email';
import GroupIcon from '@material-ui/icons/Group';
import PersonIcon from '@material-ui/icons/Person';
@@ -56,6 +62,9 @@ export const UserProfileCard = (props: { variant?: InfoCardVariants }) => {
return <Alert severity="error">User not found</Alert>;
}
const entityMetadataEditUrl =
user.metadata.annotations?.[ANNOTATION_EDIT_URL];
const {
metadata: { name: metaName, description },
spec: { profile },
@@ -66,11 +75,27 @@ export const UserProfileCard = (props: { variant?: InfoCardVariants }) => {
kind: 'Group',
});
const infoCardAction = entityMetadataEditUrl ? (
<IconButton
aria-label="Edit"
title="Edit Metadata"
component={Link}
to={entityMetadataEditUrl}
>
<EditIcon />
</IconButton>
) : (
<IconButton aria-label="Edit" disabled title="Edit Metadata">
<EditIcon />
</IconButton>
);
return (
<InfoCard
title={<CardTitle title={displayName} />}
subheader={description}
variant={props.variant}
action={infoCardAction}
>
<Grid container spacing={3} alignItems="flex-start">
<Grid item xs={12} sm={2} xl={1}>