Skip to content

Commit

Permalink
refactor(taxonomy-editor-frontend):create a component for nodes table…
Browse files Browse the repository at this point in the history
… used in nodes and search pages
  • Loading branch information
alice.juan committed Nov 16, 2023
1 parent 2b0c528 commit e94b440
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 44 deletions.
2 changes: 0 additions & 2 deletions taxonomy-editor-frontend/build/.empty

This file was deleted.

42 changes: 42 additions & 0 deletions taxonomy-editor-frontend/src/components/NodesTableBody.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
IconButton,
TableBody,
TableCell,
TableRow,
Typography,
} from "@mui/material";
import EditIcon from "@mui/icons-material/Edit";
import { Link } from "react-router-dom";

type Props = {
nodeIds: string[];
taxonomyName: string;
branchName: string;
};

const NodesTableBody = ({ nodeIds, taxonomyName, branchName }: Props) => {
return (
<>
<TableBody>
{nodeIds.map((nodeId) => (
<TableRow key={nodeId}>
<TableCell align="left" component="td" scope="row">
<Typography variant="subtitle1">{nodeId}</Typography>
</TableCell>
<TableCell align="left" component="td" scope="row">
<IconButton
component={Link}
to={`/${taxonomyName}/${branchName}/entry/${nodeId}`}
aria-label="edit"
>
<EditIcon color="primary" />
</IconButton>
</TableCell>
</TableRow>
))}
</TableBody>
</>
);
};

export default NodesTableBody;
33 changes: 12 additions & 21 deletions taxonomy-editor-frontend/src/pages/root-nodes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect } from "react";
import { Link, useParams } from "react-router-dom";
import { useParams } from "react-router-dom";

import {
Typography,
Expand All @@ -15,7 +15,6 @@ import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import EditIcon from "@mui/icons-material/Edit";
import AddBoxIcon from "@mui/icons-material/AddBox";
import Dialog from "@mui/material/Dialog";

Expand All @@ -24,6 +23,7 @@ import useFetch from "../../components/useFetch";
import { toTitleCase, createBaseURL } from "../../utils";
import { greyHexCode } from "../../constants";
import type { RootEntriesAPIResponse } from "../../backend-types/types";
import NodesTableBody from "../../components/NodesTableBody";

type RootNodesProps = {
addNavLinks: ({
Expand Down Expand Up @@ -55,6 +55,11 @@ const RootNodes = ({
errorMessage,
} = useFetch<RootEntriesAPIResponse>(`${baseUrl}rootentries`);

let nodeIds: string[] = [];
if (nodes && nodes.length > 0) {
nodeIds = nodes.map((node) => node[0].id);
}

useEffect(
function defineMainNavLinks() {
addNavLinks({ branchName, taxonomyName });
Expand Down Expand Up @@ -143,25 +148,11 @@ const RootNodes = ({
</TableCell>
</TableRow>
</TableHead>

<TableBody>
{nodes.map((node) => (
<TableRow key={node[0].id}>
<TableCell align="left" component="td" scope="row">
<Typography variant="subtitle1">{node[0].id}</Typography>
</TableCell>
<TableCell align="left" component="td" scope="row">
<IconButton
component={Link}
to={`/${taxonomyName}/${branchName}/entry/${node[0].id}`}
aria-label="edit"
>
<EditIcon color="primary" />
</IconButton>
</TableCell>
</TableRow>
))}
</TableBody>
<NodesTableBody
nodeIds={nodeIds}
taxonomyName={taxonomyName}
branchName={branchName}
/>
</Table>
</TableContainer>
</div>
Expand Down
27 changes: 6 additions & 21 deletions taxonomy-editor-frontend/src/pages/search/SearchResults.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState } from "react";
import { Link } from "react-router-dom";

import {
Typography,
Expand All @@ -13,12 +12,10 @@ import {
} from "@mui/material";
import Container from "@mui/material/Container";
import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import EditIcon from "@mui/icons-material/Edit";
import AddBoxIcon from "@mui/icons-material/AddBox";
import Dialog from "@mui/material/Dialog";

Expand All @@ -27,6 +24,7 @@ import { createBaseURL } from "../../utils";
import { greyHexCode } from "../../constants";
import type { SearchAPIResponse } from "../../backend-types/types";
import CreateNodeDialogContent from "../../components/CreateNodeDialogContent";
import NodesTableBody from "../../components/NodesTableBody";

type Props = {
query: string;
Expand Down Expand Up @@ -125,24 +123,11 @@ const SearchResults = ({ query, taxonomyName, branchName }: Props) => {
</TableCell>
</TableRow>
</TableHead>
<TableBody>
{(nodeIds ?? []).map((nodeId) => (
<TableRow key={nodeId}>
<TableCell align="left" component="td" scope="row">
<Typography variant="subtitle1">{nodeId}</Typography>
</TableCell>
<TableCell align="left" component="td" scope="row">
<IconButton
component={Link}
to={`/${taxonomyName}/${branchName}/entry/${nodeId}`}
aria-label="edit"
>
<EditIcon color="primary" />
</IconButton>
</TableCell>
</TableRow>
))}
</TableBody>
<NodesTableBody
nodeIds={nodeIds ?? []}
taxonomyName={taxonomyName}
branchName={branchName}
/>
</Table>
</TableContainer>

Expand Down

0 comments on commit e94b440

Please sign in to comment.