Skip to content
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

Create Table and Select All Checkbox #38

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions frontend/src/components/auth/PrivateRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const PrivateRoute: React.FC<PrivateRouteProps> = ({
? allowedRoles.has(authenticatedUser.role)
: true;
if (!userHasAllowedRole) {
// eslint-disable-next-line no-alert
alert(
"You don't have permission to access this page. Redirecting to home page.",
);
Expand Down
8 changes: 7 additions & 1 deletion frontend/src/components/pages/DocumentDirectory.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import React from "react";
import DocumentDirectoryTable from "../tables/DocumentDirectoryTable";

const DocumentDirectory = (): React.ReactElement => {
return <h1>Document Directory Page</h1>;
return (
<>
<h1>Document Directory Page</h1>
<DocumentDirectoryTable />
</>
);
};
export default DocumentDirectory;
113 changes: 113 additions & 0 deletions frontend/src/components/tables/DocumentDirectoryTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import React, { useState } from "react";
import {
Button,
Checkbox,
Table,
Thead,
Tbody,
Tr,
Th,
Td,
TableContainer,
} from "@chakra-ui/react";

// TODO: remove once connected to backend
const dummyData = [
{
id: "1",
name: "A",
created: "2024-03-01",
type: "Form",
comments: "blah blah",
},
{
id: "2",
name: "B",
created: "2024-03-02",
type: "Form",
comments: "blah blah",
},
{
id: "3",
name: "C",
created: "2024-03-03",
type: "Form",
comments: "blah blah",
},
];

const DocumentDirectoryTable = () => {
const documents = dummyData; // TODO: replace with call to backend

const [selectedDocumentIds, setSelectedDocumentIds] = useState<Set<string>>(
new Set(),
);

const changeRowCheckbox = (event: React.ChangeEvent<HTMLInputElement>) => {
const selectedId = event.target.id;
const isChecked = event.target.checked;
if (isChecked) {
selectedDocumentIds.add(selectedId);
} else {
selectedDocumentIds.delete(selectedId);
}
setSelectedDocumentIds((prev) => new Set(prev));
Copy link
Contributor Author

@carolynzhang18 carolynzhang18 Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing Set.add and Set.delete won't trigger a React state update because the object itself has not changed (even though its contents did).
To force a state update, I'm creating a new Set copied off the mutated previous Set.

};

const changeAllCheckboxes = (event: React.ChangeEvent<HTMLInputElement>) => {
const isChecked = event.target.checked;
if (isChecked) {
setSelectedDocumentIds(new Set(documents.map((document) => document.id)));
} else {
setSelectedDocumentIds(new Set());
}
};

return (
<>
<TableContainer>
<Table>
<Thead>
<Tr>
<Th>
<Checkbox
isChecked={selectedDocumentIds.size === documents.length}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This covers the case where someone selects all checkboxes, and then deselects one of them - we want the select all checkbox to be deselected in this case.

onChange={changeAllCheckboxes}
/>
</Th>
<Th>NAME</Th>
<Th>CREATED ON</Th>
<Th>TYPE</Th>
<Th>COMMENTS</Th>
</Tr>
</Thead>
<Tbody>
{documents.map((document) => {
return (
<Tr key={document.id}>
<Td>
<Checkbox
id={document.id}
isChecked={selectedDocumentIds.has(document.id)}
onChange={changeRowCheckbox}
/>
</Td>
<Td>{document.name}</Td>
<Td>{document.created}</Td>
<Td>{document.type}</Td>
<Td>{document.comments}</Td>
</Tr>
);
})}
</Tbody>
</Table>
</TableContainer>
<br />
<Button onClick={() => console.log(selectedDocumentIds)}>

Check warning on line 106 in frontend/src/components/tables/DocumentDirectoryTable.tsx

View workflow job for this annotation

GitHub Actions / run-lint

Unexpected console statement
Log selected document ids!
</Button>
</>
);
};

export default DocumentDirectoryTable;
Loading