-
Notifications
You must be signed in to change notification settings - Fork 886
refactor(site): generalize UserCell component #484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7a6df41
feat(site): add UserCell component
greyscaled 4fe1431
fixup! feat(site): add UserCell component
greyscaled f006d0b
document code
greyscaled c0a7360
fixup! document code
greyscaled e71346c
Merge origin/main
greyscaled d05c7e3
reorder type
greyscaled ec4ac6d
fixup! reorder type
greyscaled File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ComponentMeta, Story } from "@storybook/react" | ||
import React from "react" | ||
import { MockUser, MockUserAgent } from "../../../test_helpers" | ||
import { UserCell, UserCellProps } from "./UserCell" | ||
|
||
export default { | ||
title: "Table/Cells/UserCell", | ||
component: UserCell, | ||
} as ComponentMeta<typeof UserCell> | ||
|
||
const Template: Story<UserCellProps> = (args) => <UserCell {...args} /> | ||
|
||
export const AuditLogExample = Template.bind({}) | ||
AuditLogExample.args = { | ||
Avatar: { | ||
username: MockUser.username, | ||
}, | ||
caption: MockUserAgent.ip_address, | ||
primaryText: MockUser.email, | ||
onPrimaryTextSelect: () => { | ||
return | ||
}, | ||
} | ||
|
||
export const AuditLogEmptyUserExample = Template.bind({}) | ||
AuditLogEmptyUserExample.args = { | ||
Avatar: { | ||
username: MockUser.username, | ||
}, | ||
caption: MockUserAgent.ip_address, | ||
primaryText: "Deleted User", | ||
onPrimaryTextSelect: undefined, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { MockUser, MockUserAgent, WrapperComponent } from "../../../test_helpers" | ||
import { UserCell, UserCellProps } from "./UserCell" | ||
import React from "react" | ||
import { fireEvent, render, screen } from "@testing-library/react" | ||
|
||
namespace Helpers { | ||
export const Props: UserCellProps = { | ||
Avatar: { | ||
username: MockUser.username, | ||
}, | ||
caption: MockUserAgent.ip_address, | ||
primaryText: MockUser.username, | ||
onPrimaryTextSelect: jest.fn(), | ||
} | ||
|
||
export const Component: React.FC<UserCellProps> = (props) => ( | ||
<WrapperComponent> | ||
<UserCell {...props} /> | ||
</WrapperComponent> | ||
) | ||
} | ||
|
||
describe("UserCell", () => { | ||
// callbacks | ||
it("calls onPrimaryTextSelect when primaryText is clicked", () => { | ||
// Given | ||
const onPrimaryTextSelectMock = jest.fn() | ||
const props: UserCellProps = { | ||
...Helpers.Props, | ||
onPrimaryTextSelect: onPrimaryTextSelectMock, | ||
} | ||
|
||
// When - click the user's email address | ||
render(<Helpers.Component {...props} />) | ||
fireEvent.click(screen.getByText(props.primaryText)) | ||
|
||
// Then - callback was fired once | ||
expect(onPrimaryTextSelectMock).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
// primaryText | ||
it("renders primaryText as a link when onPrimaryTextSelect is defined", () => { | ||
// Given | ||
const props: UserCellProps = Helpers.Props | ||
|
||
// When | ||
render(<Helpers.Component {...props} />) | ||
const primaryTextNode = screen.getByText(props.primaryText) | ||
|
||
// Then | ||
expect(primaryTextNode.tagName).toBe("A") | ||
}) | ||
it("renders primaryText without a link when onPrimaryTextSelect is undefined", () => { | ||
// Given | ||
const props: UserCellProps = { | ||
...Helpers.Props, | ||
onPrimaryTextSelect: undefined, | ||
} | ||
|
||
// When | ||
render(<Helpers.Component {...props} />) | ||
const primaryTextNode = screen.getByText(props.primaryText) | ||
|
||
// Then | ||
expect(primaryTextNode.tagName).toBe("P") | ||
}) | ||
|
||
// caption | ||
it("renders caption", () => { | ||
// Given | ||
const caption = "definitely a caption" | ||
const props: UserCellProps = { | ||
...Helpers.Props, | ||
caption, | ||
} | ||
|
||
// When | ||
render(<Helpers.Component {...props} />) | ||
|
||
// Then | ||
expect(screen.getByText(caption)).toBeDefined() | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Box from "@material-ui/core/Box" | ||
import Link from "@material-ui/core/Link" | ||
import { makeStyles } from "@material-ui/core/styles" | ||
import Typography from "@material-ui/core/Typography" | ||
import React from "react" | ||
import { UserAvatar, UserAvatarProps } from "../../User" | ||
|
||
export interface UserCellProps { | ||
Avatar: UserAvatarProps | ||
/** | ||
* primaryText is rendered beside the avatar | ||
*/ | ||
primaryText: string /* | React.ReactNode <-- if needed */ | ||
/** | ||
* caption is rendered beneath the avatar and primaryText | ||
*/ | ||
caption?: string /* | React.ReactNode <-- if needed */ | ||
/** | ||
* onPrimaryTextSelect, if defined, is called when the primaryText is clicked | ||
*/ | ||
onPrimaryTextSelect?: () => void | ||
} | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
primaryText: { | ||
color: theme.palette.text.primary, | ||
fontFamily: theme.typography.fontFamily, | ||
fontSize: "16px", | ||
lineHeight: "15px", | ||
marginBottom: "5px", | ||
}, | ||
})) | ||
|
||
/** | ||
* UserCell is a single cell in an audit log table row that contains user-level | ||
* information | ||
*/ | ||
export const UserCell: React.FC<UserCellProps> = ({ Avatar, caption, primaryText, onPrimaryTextSelect }) => { | ||
const styles = useStyles() | ||
|
||
return ( | ||
<Box alignItems="center" display="flex" flexDirection="row"> | ||
<Box display="flex" margin="auto 14px auto 0"> | ||
<UserAvatar {...Avatar} /> | ||
</Box> | ||
|
||
<Box display="flex" flexDirection="column"> | ||
{onPrimaryTextSelect ? ( | ||
<Link className={styles.primaryText} onClick={onPrimaryTextSelect}> | ||
{primaryText} | ||
</Link> | ||
) : ( | ||
<Typography className={styles.primaryText}>{primaryText}</Typography> | ||
)} | ||
|
||
{caption && ( | ||
<Typography color="textSecondary" variant="caption"> | ||
{caption} | ||
</Typography> | ||
)} | ||
</Box> | ||
</Box> | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,12 @@ | ||
import Avatar from "@material-ui/core/Avatar" | ||
import React from "react" | ||
import { UserResponse } from "../../api/types" | ||
import { firstLetter } from "../../util/first-letter" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome! |
||
|
||
export interface UserAvatarProps { | ||
user: UserResponse | ||
className?: string | ||
username: string | ||
} | ||
|
||
export const UserAvatar: React.FC<UserAvatarProps> = ({ user, className }) => { | ||
return <Avatar className={className}>{firstLetter(user.username)}</Avatar> | ||
} | ||
|
||
/** | ||
* `firstLetter` extracts the first character and returns it, uppercased | ||
* | ||
* If the string is empty or null, returns an empty string | ||
*/ | ||
export const firstLetter = (str: string): string => { | ||
if (str && str.length > 0) { | ||
return str[0].toLocaleUpperCase() | ||
} | ||
|
||
return "" | ||
export const UserAvatar: React.FC<UserAvatarProps> = ({ username, className }) => { | ||
return <Avatar className={className}>{firstLetter(username)}</Avatar> | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ export const UserProfileCard: React.FC<UserProfileCardProps> = ({ user }) => { | |
return ( | ||
<div className={styles.root}> | ||
<div className={styles.avatarContainer}> | ||
<UserAvatar className={styles.avatar} user={user} /> | ||
<UserAvatar className={styles.avatar} username={user.username} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great choice |
||
</div> | ||
<Typography className={styles.userName}>{user.username}</Typography> | ||
<Typography className={styles.userEmail}>{user.email}</Typography> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { firstLetter } from "./first-letter" | ||
|
||
describe("first-letter", () => { | ||
it.each<[string, string]>([ | ||
["", ""], | ||
["User", "U"], | ||
["test", "T"], | ||
])(`firstLetter(%p) returns %p`, (input, expected) => { | ||
expect(firstLetter(input)).toBe(expected) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* firstLetter extracts the first character and returns it, uppercased. | ||
*/ | ||
export const firstLetter = (str: string): string => { | ||
if (str.length > 0) { | ||
return str[0].toLocaleUpperCase() | ||
} | ||
|
||
return "" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@presleyp - do you like this organization (Table > Cells) ? Another possibility is a new directory "TableCells". I could go either way, just wanna find the one we all think is best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this seems good! Having a manageable number of top level categories will be nice.