Skip to content

Commit

Permalink
refactor product component
Browse files Browse the repository at this point in the history
  • Loading branch information
radekm2000 committed Apr 16, 2024
1 parent b710871 commit ff4f9c1
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 230 deletions.
19 changes: 0 additions & 19 deletions client/ecommerce/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
import { Toaster } from "react-hot-toast";
import { Login } from "./components/pages/Login";
import { Register } from "./components/pages/Register";
import { Redirect, Route, Switch } from "wouter";
import { MainPage } from "./components/pages/MainPage";
import { UserProvider } from "./contexts/UserContext";
import { AddProduct } from "./components/pages/AddProduct";
import { Product } from "./components/pages/Product";
import { Member } from "./components/pages/Member";
import { Followers } from "./components/pages/Followers";
import { Followings } from "./components/pages/Followings";
import { MenCatalog } from "./components/pages/MenCatalog";
import { WomenCatalog } from "./components/pages/WomenCatalog";
import { Inbox } from "./components/pages/Inbox";
import SearchTextResults from "./components/pages/SearchTextResults";
import { EditProfile } from "./components/pages/EditProfile";
import { ProductProvider } from "./contexts/ProductContext";
import { PaymentSuccess } from "./components/pages/PaymentSuccess";
import { PaymentCancel } from "./components/pages/PaymentCancel";
import { Elements } from "@stripe/react-stripe-js";
import { loadStripe } from "@stripe/stripe-js";
import { ChatNotificationsProvider } from "./contexts/ChatNotificationsContext";
import { ProductNotificationProvider } from "./contexts/ProductNotificationContext";
import { Layout } from "./components/Layout";
import { AdminDashboard } from "./components/pages/AdminDashboard";
import { RoutePath } from "./config/constants/navigation";
import { Routes } from "./routes/Routes";

const stripePromise = loadStripe(import.meta.env.VITE_STRIPE_KEY);
Expand Down
13 changes: 2 additions & 11 deletions client/ecommerce/src/components/DisplayUserProducts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,10 @@ import { calculateGridWidth } from "../utils/calculateGridWidth";

export const DisplayUserProducts = ({
products,
setLocation,
}: {
products: ProductWithImageAndUser[];
setLocation: (
to: string,
options?:
| {
replace?: boolean | undefined;
}
| undefined
) => void;
}) => {
const [, setLocation] = useLocation();
const handleOnProductClick = (productId: number, productTitle: string) => {
const url = `/products/${productId}-${productTitle}`;
setLocation(url, { replace: true });
Expand All @@ -36,7 +28,6 @@ export const DisplayUserProducts = ({

const below1050 = useMediaQuery(1050);
const below1200 = useMediaQuery(1200);
const below1600 = useMediaQuery(1600);
return (
<Box
sx={{
Expand Down Expand Up @@ -143,7 +134,7 @@ export const DisplayUserProducts = ({
sx={{ overflow: "hidden", textOverflow: "ellipsis" }}
>
<Typography sx={{ fontSize: "14px", color: "#171717" }}>
PLN {product.price}.00
USD {product.price}.00
</Typography>
</CardContent>
</Card>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import KeyboardArrowRightIcon from "@mui/icons-material/KeyboardArrowRight";
import { Link, useLocation } from "wouter";
import { useUserInfo } from "../../hooks/useUserInfo";
import { calculateMedian } from "../../utils/calculateMedian";
import { FallbackProgress } from "../../utils/FallbackProgress";
export const DisplayUserInfo = ({
userId,
product,
Expand All @@ -32,15 +33,13 @@ export const DisplayUserInfo = ({
setFollowButtonClicked(isFollowed(user));
}, [user]);
const mutation = useFollowUser(user?.id);
if (isMeUserInfoLoading) {
return "isLoading...";
if (isMeUserInfoLoading || isUserLoading) {
return <FallbackProgress />;
}
if (!product) {
return;
}
if (isUserLoading) {
return "userLoading...";
}

if (!user) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Box, Typography } from "@mui/material";
import { ProductWithImageAndUser } from "../../types/types";
import { DisplayUserProducts } from "../DisplayUserProducts";
import { useMediaQuery } from "../../hooks/useMediaQuery";

type ProductMainContentProps = {
product: ProductWithImageAndUser;
userProducts: ProductWithImageAndUser[] | undefined;
};

export const ProductMainContent = (props: ProductMainContentProps) => {
const below960 = useMediaQuery(960);
const below800 = useMediaQuery(800);

const productsWithoutMainOne = props.userProducts?.filter(
(p) => p.id != props.product.id
);
return (
<Box
sx={{
width: below960 ? "100%" : "77%",
display: "flex",
padding: "0px 10px",
justifyContent: "center",
flexDirection: "column",
alignItems: "flex-start",
gap: "20px",
height:
props.userProducts?.length == 1 ? (below800 ? "auto" : "auto") : "",
}}
>
<Box
component="img"
src={props.product?.images[0].imageUrl}
sx={{ height: "100%", alignSelf: "center" }}
></Box>
<Box
sx={{
display: below960 ? "none" : "inline",
marginTop: "40px",
padding: "20px 16px",
backgroundColor: "white",
width: "96%",
}}
>
<Typography
sx={{
color: "#757575",
fontSize: "14px",
borderBottom: "1px solid rgba(23, 23, 23, 0.15)",
paddingBottom: "5px",
}}
>
Member's items ({props.userProducts?.length})
</Typography>
</Box>
<Box sx={{ display: below960 ? "none" : "inline" }}>
<DisplayUserProducts
products={productsWithoutMainOne as ProductWithImageAndUser[]}
/>
</Box>
</Box>
);
};
156 changes: 156 additions & 0 deletions client/ecommerce/src/components/SpecificProduct/ProductSidebar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { Box, Button, Typography } from "@mui/material";
import { DisplayUserInfo } from "../ProductPage/DisplayUserInfo";
import { ProductWithImageAndUser } from "../../types/types";

type ProductProps = {
product: ProductWithImageAndUser;
below960: boolean;
productAuthorId: number;
userId: number;
handleCheckout: (
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
) => Promise<void>;
handleDeleteProduct: (
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
) => Promise<void>;
};

export const ProductSidebar = (props: ProductProps ) => {
return (
<Box
sx={{
width: props.below960 ? "100%" : "23%",
display: "flex",
padding: "0px 10px",
flexDirection: "column",
paddingTop: props.below960 ? "20px" : 0,
gap: "15px",
}}
>
<Box
sx={{
width: "100%",
flexDirection: "column",
display: "flex",
backgroundColor: "white",
padding: "16px",
maxHeight: "50%",
}}
>
<Box
sx={{
width: "auto",
display: "flex",
borderBottom: "1px solid rgba(23, 23, 23, 0.15)",
paddingBottom: "5px",
marginBottom: "8px",
}}
>
<Typography sx={{ fontSize: "22px" }}>
USD {props.product.price}.00
</Typography>
</Box>
<Box
sx={{
paddingBottom: "5px",
borderBottom: "1px solid rgba(23, 23, 23, 0.15)",
}}
>
<Box sx={{ display: "flex", padding: "2px" }}>
<Typography
sx={{ fontSize: "13px", color: "#757575", width: "50%" }}
>
BRAND
</Typography>
<Typography sx={{ fontSize: "13px", color: "#757575" }}>
{props.product.brand}
</Typography>
</Box>
<Box sx={{ display: "flex", padding: "2px" }}>
<Typography
sx={{ fontSize: "13px", color: "#757575", width: "50%" }}
>
CATEGORY
</Typography>
<Typography sx={{ fontSize: "13px", color: "#757575" }}>
{props.product.category.toUpperCase()}
</Typography>
</Box>
</Box>
<Box
component="div"
sx={{
marginTop: "20px",
display: "flex",
flexDirection: "column",
alignItems: "flex-start",
justifyContent: "flex-start",
width: props.below960 ? "50%" : "100%",
whiteSpace: "normal !important",
wordBreak: "break-word",
}}
>
<Typography sx={{ fontSize: "18px" }}>{props.product?.title}</Typography>
<Typography
sx={{
color: "#4D4D4D",
paddingTop: "8px",
}}
>
{props.product?.description}
</Typography>
</Box>
</Box>
<Box
sx={{
display: "flex",
width: "100%",
padding: "0px 16px",
}}
>
{props.productAuthorId === props.userId ? (
<Button
variant="outlined"
onClick={(e) => props.handleDeleteProduct(e)}
sx={{
display: "flex",
width: "100%",
textTransform: "none",
border: "1px solid red",
color: "red",
backgroundColor: "white",
justifyContent: "center",
alignItems: "center",
"&:hover": {
border: "1px solid red",
backgroundColor: "white",
},
}}
>
Delete
</Button>
) : (
<Button
onClick={(e) => props.handleCheckout(e)}
sx={{
display: "flex",
width: "100%",
textTransform: "none",
color: "white",
backgroundColor: "#007782",
justifyContent: "center",
alignItems: "center",
"&:hover": {
backgroundColor: "#007782",
},
}}
>
Buy now
</Button>
)}
</Box>

<DisplayUserInfo userId={props.productAuthorId} product={props.product} />
</Box>
);
};
1 change: 0 additions & 1 deletion client/ecommerce/src/components/pages/AddProduct.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import toast from "react-hot-toast";
import { Redirect } from "wouter";
import { useUserContext } from "../../contexts/UserContext";
import { useAddAdminNotification } from "../../hooks/useAddAdminNotification";
import axios from "axios";

//important note --------------
// change button component prop to label if you want to upload files
Expand Down
3 changes: 2 additions & 1 deletion client/ecommerce/src/components/pages/Followings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useEffect, useState } from "react";
import { UserWithFollows } from "../../types/types";
import { useUserContext } from "../../contexts/UserContext";
import { useFollowUser } from "../../utils/followUser";
import { FallbackProgress } from "../../utils/FallbackProgress";

export const Followings = () => {
const { user: meUser } = useUserContext();
Expand Down Expand Up @@ -42,7 +43,7 @@ export const Followings = () => {
};

if (isUserLoading) {
return "isLoading...";
return <FallbackProgress/>
}
if (!user) {
return;
Expand Down
Loading

0 comments on commit ff4f9c1

Please sign in to comment.