Skip to content

Commit

Permalink
Display children
Browse files Browse the repository at this point in the history
  • Loading branch information
chennisden committed Jul 30, 2024
1 parent b31e842 commit e37f769
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
11 changes: 11 additions & 0 deletions frontend/src/hooks/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ export class api {
});
}

public async getListingsBatch(
ids: string[]
): Promise<Listing[]> {
return this.callWrapper(async () => {
const response = await this.api.get("/listings/batch", {
params: { ids: ids.join(",") },
});
return response.data;
});
}

public async dumpListings(): Promise<Listing[]> {
return this.callWrapper(async () => {
const response = await this.api.get("/listings/dump");
Expand Down
31 changes: 30 additions & 1 deletion frontend/src/pages/ListingDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
Spinner,
} from "react-bootstrap";
import Markdown from "react-markdown";
import { useNavigate, useParams } from "react-router-dom";
import { Link, useNavigate, useParams } from "react-router-dom";

interface ListingDetailsResponse {
name: string;
Expand All @@ -26,6 +26,11 @@ interface ListingDetailsResponse {
child_ids: string[];
}

interface Child {
id: string;
name: string;
}

const RenderListing = ({
listing,
id,
Expand All @@ -44,6 +49,7 @@ const RenderListing = ({
const [ownerEmail, setOwnerEmail] = useState<string | null>(null);
const [imageIndex, setArtifactIndex] = useState(0);
const [showDelete, setShowDelete] = useState(false);
const [children, setChildren] = useState<Child[] | null>(null);

const handleClose = () => setShow(false);
const handleShow = () => setShow(true);
Expand Down Expand Up @@ -78,6 +84,20 @@ const RenderListing = ({
fetchListing();
}, [user_id]);

useEffect(() => {
const fetchChildren = async () => {
try {
const children = await auth_api.getListingsBatch(listing.child_ids)
setChildren(children.map((child) => ({ id: child.id, name: child.name })))
} catch (err) {
addAlert(humanReadableError(err), "error");
}
}
if (listing.child_ids.length > 0) {
fetchChildren();
}
}, [])

return (
<>
<Row className="mt-3">
Expand All @@ -92,7 +112,16 @@ const RenderListing = ({
<a href={"mailto:" + ownerEmail}>{ownerEmail}</a>.
</em>
</Col>

</Row>
{children && <Row><Col>
<p>This listing depends on the following listings:</p>
<ul>
{children.map((child) => <li>
<Link to={"/listing/" + child.id}>{child.name}</Link>
</li>)}
</ul>
</Col></Row>}
<hr />
<Row>
<Col>
Expand Down
8 changes: 8 additions & 0 deletions store/app/routers/listings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ async def list_listings(
return await crud.get_listings(page, search_query=search_query)


@listings_router.get("/batch")
async def batch(
crud: Annotated[Crud, Depends(Crud.get)],
ids: list[str] = Query(description="List of part ids"),
) -> list[Listing]:
return await crud._get_item_batch(ids, Listing)


@listings_router.get("/dump")
async def dump_listings(
crud: Annotated[Crud, Depends(Crud.get)],
Expand Down

0 comments on commit e37f769

Please sign in to comment.