-
Notifications
You must be signed in to change notification settings - Fork 0
Enhancement lastUpdated cell styling #135
Merged
+235
−20
Merged
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6e6492b
Added background state varible and created lastUpdatedCell component …
a6d8132
Removed uncesscary function and variables. Changed name of the state …
ab75000
Implemented logic for changing cell color based on last updated time.
6b67902
implemented logic that sets color of lastUpdated cell based on time o…
c418a5a
Forammting fixes, removed uneccessary overrides, fixed date received…
4c57b61
Created ItemTableCell and LastUpdated cell components for refactoring…
wrigh393 ae03e13
Fixed background color not displaying in LastUpdatedCell component
wrigh393 8ac1d92
Update Python requirements to fix Flask_JWT_Extended and PyJWT
campb303 7110cda
Update venv-manager to allow 60 second for requirement installation
campb303 f36ac8e
Moved ternary statements to swtich statement for readability
campb303 375b679
Formatting
campb303 55de973
Rename reactTableProps to TableCellProps
campb303 fc608a6
Renamed folder and update exports
campb303 0c3f1bb
Formatting
campb303 813438a
Remove unused backgroundColor prop
campb303 672b0f9
Refactir timeToBackgroundColor and inject background color into props
campb303 86b75a1
Rename reactTableProps to ItemTableCellProps
campb303 e3724de
Make time prop required
campb303 9df197e
Add docs
campb303 246e4d9
Add docs
campb303 1ed9daa
Update docs to show what values the time prop can be
campb303 1084d86
Merge branch 'staging' into enhancement-lastupdated-styling
campb303 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 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,15 +1,47 @@ | ||
import React, { useState } from "react"; | ||
import PropTypes from "prop-types"; | ||
import { useTable, useFilters, useFlexLayout, useSortBy } from "react-table"; | ||
import { Table, TableBody, TableCell, TableHead, TableRow, TableContainer, Paper, Grid, ButtonGroup, IconButton, makeStyles, useTheme } from "@material-ui/core"; | ||
import { Table, TableBody, TableCell, TableHead, TableRow, TableContainer, Paper, Grid, ButtonGroup, IconButton, makeStyles, useTheme, } from "@material-ui/core"; | ||
import { red } from '@material-ui/core/colors'; | ||
import { useHistory } from "react-router-dom"; | ||
import RelativeTime from "react-relative-time"; | ||
import ItemTableFilter from "../ItemTableFilter/" | ||
import { ArrowDownward, ArrowUpward } from "@material-ui/icons"; | ||
|
||
export default function ItemTable({ data }) { | ||
const [selectedRow, setSelecetedRow] = useState({ queue: null, number: null }); | ||
|
||
const LastUpdatedCell = ({ time, reactTableCellProps, classes }) => { | ||
const lastUpdated = new Date(time).getTime(); | ||
const now = new Date().getTime(); | ||
const timeDelta = now - lastUpdated; | ||
const day = 24 * 60 * 60 * 1000; | ||
const week = day * 7; | ||
const month = week * 4; | ||
|
||
let backgroundColor = "white"; | ||
if (timeDelta > day && timeDelta <= week) { | ||
backgroundColor = red[100] | ||
} | ||
else if (timeDelta > week && timeDelta <= month) { | ||
backgroundColor = red[300] | ||
} | ||
else if (timeDelta > month) { | ||
backgroundColor = red[500] | ||
} | ||
|
||
return ( | ||
<TableCell | ||
classes={classes} | ||
// Expands table cell props to allow access to style props. | ||
{...reactTableCellProps} | ||
// Expands style prop of table cell to allow addtion of custom styling without losing default styling. | ||
style={{ ...reactTableCellProps.style, backgroundColor: backgroundColor }} | ||
> | ||
<RelativeTime value={time} /> | ||
</TableCell> | ||
); | ||
}; | ||
|
||
const theme = useTheme(); | ||
const useStyles = makeStyles({ | ||
// Fully visible for active icons | ||
|
@@ -34,7 +66,7 @@ export default function ItemTable({ data }) { | |
borderLeftWidth: "1px", | ||
borderLeftStyle: "solid", | ||
borderColor: theme.palette.type === "light" ? theme.palette.grey[300] : theme.palette.grey[500] | ||
} | ||
}, | ||
}); | ||
const classes = useStyles(); | ||
|
||
|
@@ -48,10 +80,10 @@ export default function ItemTable({ data }) { | |
{ Header: 'Subject', accessor: 'subject' }, | ||
{ Header: 'Status', accessor: 'status', }, | ||
{ Header: 'Priority', accessor: 'priority' }, | ||
{ Header: 'Last Updated', accessor: 'lastUpdated', Cell: ({ value }) => <RelativeTime value={value} /> }, | ||
{ Header: 'Last Updated', accessor: 'lastUpdated', }, | ||
{ Header: 'Department', accessor: 'department' }, | ||
{ Header: 'Building', accessor: 'building' }, | ||
{ Header: 'Date Received', accessor: 'dateReceived', Cell: ({ value }) => <RelativeTime value={value} /> }, | ||
{ Header: 'Date Received', accessor: 'dateReceived', }, | ||
], []); | ||
|
||
const tableInstance = useTable( | ||
|
@@ -66,7 +98,7 @@ export default function ItemTable({ data }) { | |
onChange={(event) => setFilter(event.target.value)} | ||
/> | ||
); | ||
} | ||
}, | ||
}, | ||
}, | ||
useFilters, useFlexLayout, useSortBy, | ||
|
@@ -75,7 +107,11 @@ export default function ItemTable({ data }) { | |
|
||
return ( | ||
<TableContainer component={Paper} > | ||
<Table {...getTableProps} aria-label="Table of Queue Items" > | ||
<Table | ||
{...getTableProps} | ||
aria-label="Table of Queue Items" | ||
size="small" | ||
> | ||
<TableHead> | ||
{headerGroups.map(headerGroup => ( | ||
<TableRow {...headerGroup.getHeaderGroupProps()}> | ||
|
@@ -133,11 +169,33 @@ export default function ItemTable({ data }) { | |
}} | ||
classes={{ root: isSelected ? classes.rowSelected : classes.bandedRows }} | ||
selected={isSelected} | ||
{...row.getRowProps()} > | ||
{...row.getRowProps()} | ||
> | ||
{row.cells.map(cell => ( | ||
<TableCell classes={{ root: classes.columnBorders }} {...cell.getCellProps()}> | ||
{cell.render("Cell")} | ||
</TableCell> | ||
|
||
cell.render( | ||
// Conditonally renders custom cell component based on cell.column.id prop value. | ||
cell.column.id === "lastUpdated" | ||
? (<LastUpdatedCell | ||
time={cell.value} | ||
reactTableCellProps={cell.getCellProps()} | ||
classes={{ root: classes.columnBorders }} | ||
/>) : | ||
cell.column.id === "dateReceived" | ||
? (<TableCell | ||
{...cell.getCellProps()} | ||
classes={{ root: classes.columnBorders }} | ||
> | ||
<RelativeTime value={cell.value} /> | ||
</TableCell>) | ||
: | ||
<TableCell | ||
{...cell.getCellProps()} | ||
classes={{ root: classes.columnBorders }} | ||
> | ||
{cell.value} | ||
</TableCell> | ||
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. We want to avoid WET (writing everything twice) code and keep it DRY (don't repeat yourself). We can do that here by making a component for a TableCell and giving it the CSS override equivalent to Something like this could also be extended by the LastUpdatedCell so we're not duplicating logic. |
||
) | ||
))} | ||
</TableRow> | ||
); | ||
|
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.
Logic is good. This should be its own component with docs. It should also extend an ItemTableCell (see comments below about ItemTableCell.)