Skip to content

Commit

Permalink
Create document directory table with checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynzhang18 committed Mar 8, 2024
1 parent cbafe5f commit 9b7ed82
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
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;
114 changes: 114 additions & 0 deletions frontend/src/components/tables/DocumentDirectoryTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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));
};

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}
onChange={changeAllCheckboxes}
/>
</Th>
<Th>NAME</Th>
<Th>CREATED ON</Th>
<Th>TYPE</Th>
<Th>COMMENTS</Th>
</Tr>
</Thead>
<Tbody>
{documents.map((document) => {
const isChecked = selectedDocumentIds.has(document.id);
return (
<Tr key={document.id}>
<Td>
<Checkbox
id={document.id}
isChecked={isChecked}
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 107 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;

0 comments on commit 9b7ed82

Please sign in to comment.