Skip to content

Commit

Permalink
feat: 🎸 get orchid by id api
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoang-Nguyen-Huy committed Oct 22, 2024
1 parent c5189a4 commit 575aef9
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions src/components/OrchidDetail.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useState } from "react";
import { useContext, useEffect, useState } from "react";
import { useParams, useNavigate } from "react-router-dom";
import {
Box,
Expand All @@ -13,7 +13,6 @@ import {
IconButton,
} from "@mui/material";
import { Grid } from "@mui/joy";
import { Orchids } from "../ListOfOrchids";
import LocationOnIcon from "@mui/icons-material/LocationOn";
import CategoryIcon from "@mui/icons-material/Category";
import ColorLensIcon from "@mui/icons-material/ColorLens";
Expand All @@ -25,17 +24,31 @@ import { ThemeContext } from "../themes/ThemeContext";
import { AspectRatio } from "@mui/joy";
import { motion } from "framer-motion";
import { RelatedOrchidsSlider } from "./RelatedOrchidSlider";
import { getOrchidById } from "../apis/OrchidApi";

const MotionBox = motion(Box);

export default function OrchidDetail() {
const { id } = useParams();
const navigate = useNavigate();
const orchid = Orchids.find((o) => o.Id === id);
const [api, setApi] = useState({});
const { theme } = useContext(ThemeContext);
const [videoModalOpen, setVideoModalOpen] = useState(false);

if (!orchid) {
useEffect(() => {
const fetchOrchids = async () => {
try {
const data = await getOrchidById(id);
setApi(data);
} catch (error) {
console.error("Failed to fetch orchids:", error);
}
};

fetchOrchids();
}, [id]);

if (!api) {
return (
<Container
maxWidth='lg'
Expand Down Expand Up @@ -100,8 +113,8 @@ export default function OrchidDetail() {
<Box sx={{ position: "relative" }}>
<AspectRatio ratio='4/3'>
<img
src={orchid.image}
alt={orchid.name}
src={api.image}
alt={api.name}
style={{
width: "100%",
height: "100%",
Expand Down Expand Up @@ -135,12 +148,12 @@ export default function OrchidDetail() {
gutterBottom
sx={{ fontWeight: "bold", color: theme.text.primary }}
>
{orchid.name}
{api.name}
</Typography>
<Box sx={{ display: "flex", alignItems: "center", mb: 2 }}>
<Rating
name='read-only'
value={orchid.rating}
value={Number(api.rating)}
readOnly
precision={0.5}
emptyIcon={
Expand All @@ -154,7 +167,7 @@ export default function OrchidDetail() {
variant='body1'
sx={{ ml: 1, color: theme.text.secondary }}
>
({orchid.rating}/5)
({api.rating}/5)
</Typography>
</Box>
<Divider sx={{ my: 2 }} />
Expand All @@ -165,7 +178,7 @@ export default function OrchidDetail() {
<LocationOnIcon sx={{ mr: 1, color: theme.icon.color }} />
Origin:{" "}
<Chip
label={orchid.origin}
label={api.origin}
sx={{
ml: 1,
backgroundColor: theme.chip.backgroundColor,
Expand All @@ -180,7 +193,7 @@ export default function OrchidDetail() {
<CategoryIcon sx={{ mr: 1, color: theme.icon.color }} />
Category:{" "}
<Chip
label={orchid.category}
label={api.category}
sx={{
ml: 1,
backgroundColor: theme.chip.backgroundColor,
Expand All @@ -195,7 +208,7 @@ export default function OrchidDetail() {
<ColorLensIcon sx={{ mr: 1, color: theme.icon.color }} />
Color:{" "}
<Chip
label={orchid.color}
label={api.color}
sx={{
ml: 1,
backgroundColor: theme.chip.backgroundColor,
Expand All @@ -209,9 +222,9 @@ export default function OrchidDetail() {
paragraph
sx={{ color: theme.text.secondary, lineHeight: 1.6 }}
>
{orchid.detail}
{api.detail}
</Typography>
{orchid.isSpecial && (
{api.isSpecial && (
<Chip
icon={<StarIcon />}
label='Special Orchid'
Expand All @@ -225,7 +238,7 @@ export default function OrchidDetail() {
</Paper>
</MotionBox>

<RelatedOrchidsSlider currentOrchid={orchid} theme={theme} />
<RelatedOrchidsSlider currentOrchid={api} theme={theme} />
</Container>

<Modal
Expand Down Expand Up @@ -260,8 +273,8 @@ export default function OrchidDetail() {
<iframe
width='100%'
height='100%'
src={orchid.video}
title={orchid.name}
src={api.video}
title={api.name}
allow='accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture'
allowFullScreen
></iframe>
Expand Down

0 comments on commit 575aef9

Please sign in to comment.