Skip to content

Enhancement lastUpdated cell styling #135

Merged
merged 22 commits into from
Jan 22, 2021
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 …
Nov 23, 2020
a6d8132
Removed uncesscary function and variables. Changed name of the state …
Nov 25, 2020
ab75000
Implemented logic for changing cell color based on last updated time.
Dec 2, 2020
6b67902
implemented logic that sets color of lastUpdated cell based on time o…
Dec 3, 2020
c418a5a
Forammting fixes, removed uneccessary overrides, fixed date received…
Dec 3, 2020
4c57b61
Created ItemTableCell and LastUpdated cell components for refactoring…
wrigh393 Dec 16, 2020
ae03e13
Fixed background color not displaying in LastUpdatedCell component
wrigh393 Dec 17, 2020
8ac1d92
Update Python requirements to fix Flask_JWT_Extended and PyJWT
campb303 Dec 30, 2020
7110cda
Update venv-manager to allow 60 second for requirement installation
campb303 Dec 30, 2020
f36ac8e
Moved ternary statements to swtich statement for readability
campb303 Jan 20, 2021
375b679
Formatting
campb303 Jan 20, 2021
55de973
Rename reactTableProps to TableCellProps
campb303 Jan 20, 2021
fc608a6
Renamed folder and update exports
campb303 Jan 20, 2021
0c3f1bb
Formatting
campb303 Jan 20, 2021
813438a
Remove unused backgroundColor prop
campb303 Jan 21, 2021
672b0f9
Refactir timeToBackgroundColor and inject background color into props
campb303 Jan 21, 2021
86b75a1
Rename reactTableProps to ItemTableCellProps
campb303 Jan 21, 2021
e3724de
Make time prop required
campb303 Jan 21, 2021
9df197e
Add docs
campb303 Jan 21, 2021
246e4d9
Add docs
campb303 Jan 21, 2021
1ed9daa
Update docs to show what values the time prop can be
campb303 Jan 21, 2021
1084d86
Merge branch 'staging' into enhancement-lastupdated-styling
campb303 Jan 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 69 additions & 11 deletions src/components/ItemTable/ItemTable.js
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>
);
};
Copy link
Collaborator

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.)


const theme = useTheme();
const useStyles = makeStyles({
// Fully visible for active icons
Expand All @@ -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();

Expand All @@ -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(
Expand All @@ -66,7 +98,7 @@ export default function ItemTable({ data }) {
onChange={(event) => setFilter(event.target.value)}
/>
);
}
},
},
},
useFilters, useFlexLayout, useSortBy,
Expand All @@ -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()}>
Expand Down Expand Up @@ -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>
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 classes.columnBorders. It could be called something like ItemTableCell. Then we'd have one and only one place where we need to make styling changes.

Something like this could also be extended by the LastUpdatedCell so we're not duplicating logic.

)
))}
</TableRow>
);
Expand Down