-
Notifications
You must be signed in to change notification settings - Fork 928
feat(site): add ActionCell component #500
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
Closed
greyscaled
wants to merge
4
commits into
vapurrmaid/472-init-audit-log
from
vapurrmaid/472-action-cell
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ComponentMeta, Story } from "@storybook/react" | ||
import React from "react" | ||
import { ActionCell, ActionCellProps } from "./ActionCell" | ||
|
||
export default { | ||
title: "AuditLog/Cells/ActionCell", | ||
component: ActionCell, | ||
} as ComponentMeta<typeof ActionCell> | ||
|
||
const Template: Story<ActionCellProps> = (args) => <ActionCell {...args} /> | ||
|
||
export const Success = Template.bind({}) | ||
Success.args = { | ||
action: "create", | ||
statusCode: 200, | ||
} | ||
|
||
export const Failure = Template.bind({}) | ||
Failure.args = { | ||
action: "create", | ||
statusCode: 500, | ||
} |
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,94 @@ | ||
import { ActionCell, ActionCellProps, LANGUAGE } from "./ActionCell" | ||
import React from "react" | ||
import { render, screen } from "@testing-library/react" | ||
|
||
namespace Helpers { | ||
export const Props: ActionCellProps = { | ||
action: "create", | ||
statusCode: 200, | ||
} | ||
export const Component: React.FC<ActionCellProps> = (props) => <ActionCell {...props} /> | ||
} | ||
|
||
describe("ActionCellProps", () => { | ||
it.each<[ActionCellProps, ActionCellProps, boolean]>([ | ||
[{ action: "Create", statusCode: 200 }, { action: "Create", statusCode: 200 }, false], | ||
[{ action: " Create ", statusCode: 400 }, { action: "Create", statusCode: 400 }, false], | ||
[{ action: "", statusCode: 200 }, { action: "", statusCode: 200 }, true], | ||
])(`validate(%p) throws: %p`, (props, expected, throws) => { | ||
const validate = () => { | ||
return ActionCellProps.validate(props) | ||
} | ||
|
||
if (throws) { | ||
expect(validate).toThrowError() | ||
} else { | ||
expect(validate()).toStrictEqual(expected) | ||
} | ||
}) | ||
it.each<[number, boolean]>([ | ||
// success cases | ||
[200, true], | ||
[201, true], | ||
[302, true], | ||
// failure cases | ||
[400, false], | ||
[404, false], | ||
[500, false], | ||
])(`isSuccessStatus(%p) returns %p`, (statusCode, expected) => { | ||
expect(ActionCellProps.isSuccessStatus(statusCode)).toBe(expected) | ||
}) | ||
}) | ||
|
||
describe("ActionCell", () => { | ||
// action cases | ||
it("renders the action", () => { | ||
// Given | ||
const props = Helpers.Props | ||
|
||
// When | ||
render(<Helpers.Component {...props} />) | ||
|
||
// Then | ||
expect(screen.getByText(props.action)).toBeDefined() | ||
}) | ||
it("throws when action is an empty string", () => { | ||
// Given | ||
const props: ActionCellProps = { | ||
...Helpers.Props, | ||
action: "", | ||
} | ||
|
||
// When | ||
const shouldThrow = () => { | ||
render(<Helpers.Component {...props} />) | ||
} | ||
|
||
// Then | ||
expect(shouldThrow).toThrowError() | ||
}) | ||
|
||
// statusCode cases | ||
it.each<[number, string]>([ | ||
// Success cases | ||
[200, LANGUAGE.statusCodeSuccess], | ||
[201, LANGUAGE.statusCodeSuccess], | ||
[302, LANGUAGE.statusCodeSuccess], | ||
// Failure cases | ||
[400, LANGUAGE.statusCodeFail], | ||
[404, LANGUAGE.statusCodeFail], | ||
[500, LANGUAGE.statusCodeFail], | ||
])("renders %p when statusCode is %p", (statusCode, expected) => { | ||
// Given | ||
const props: ActionCellProps = { | ||
...Helpers.Props, | ||
statusCode, | ||
} | ||
|
||
// When | ||
render(<Helpers.Component {...props} />) | ||
|
||
// Then | ||
expect(screen.getByText(expected)).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,68 @@ | ||
import Box from "@material-ui/core/Box" | ||
import Typography from "@material-ui/core/Typography" | ||
import makeStyles from "@material-ui/core/styles/makeStyles" | ||
import React from "react" | ||
|
||
export const LANGUAGE = { | ||
statusCodeFail: "failure", | ||
statusCodeSuccess: "success", | ||
} | ||
|
||
export interface ActionCellProps { | ||
action: string | ||
statusCode: number | ||
} | ||
export namespace ActionCellProps { | ||
/** | ||
* validate that the received props are valid | ||
* | ||
* @throws Error if invalid | ||
*/ | ||
export const validate = (props: ActionCellProps): ActionCellProps => { | ||
const sanitizedAction = props.action.trim() | ||
|
||
if (!sanitizedAction) { | ||
throw new Error(`invalid action: '${props.action}'`) | ||
} | ||
|
||
return { | ||
action: sanitizedAction, | ||
statusCode: props.statusCode, | ||
} | ||
} | ||
|
||
export const isSuccessStatus = (statusCode: ActionCellProps["statusCode"]): boolean => { | ||
return statusCode >= 100 && statusCode < 400 | ||
} | ||
} | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
statusText: (isSuccess: boolean) => ({ | ||
color: isSuccess ? theme.palette.primary.main : theme.palette.error.main, | ||
}), | ||
})) | ||
|
||
/** | ||
* ActionCell is a single cell in an audit log table row that contains | ||
* information about an action that was taken on a resource. | ||
* | ||
* @remarks | ||
* | ||
* Some common actions are CRUD, Open, signing in etc. | ||
*/ | ||
export const ActionCell: React.FC<ActionCellProps> = (props) => { | ||
const { action, statusCode } = ActionCellProps.validate(props) | ||
const isSuccess = ActionCellProps.isSuccessStatus(statusCode) | ||
const styles = useStyles(isSuccess) | ||
|
||
return ( | ||
<Box alignItems="center" display="flex" flexDirection="column"> | ||
<Typography color="textSecondary" variant="h6"> | ||
{action} | ||
</Typography> | ||
<Typography className={styles.statusText} variant="caption"> | ||
{isSuccess ? LANGUAGE.statusCodeSuccess : LANGUAGE.statusCodeFail} | ||
</Typography> | ||
</Box> | ||
) | ||
} |
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.
Interesting pattern! Is the idea that static typechecking isn't enough because we're getting the action from the backend? Looks like it's just checking that the action is non-empty, is that right?
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.
Yea, that's the idea (not sure if good usage here). The v2 RFC states that we should be throwing errors if we are missing information, so that we find it immediately. The idea is that previously there were bugs whereby a partial audit log was produced either from the FE missing a mapping or, on the BE, the resource, action or target was incorrect. Because of the nature of what audit logs represent, the idea is fail fast.
I don't know if I did it correctly here yet, as the BE port is not even started - my hope is that these designs will need minimal adjustment once the BE port is available. I have an idea of what it will look like, but there is not yet an interface/contract/stub to work from.