Skip to content

Commit

Permalink
feat: add checkout box
Browse files Browse the repository at this point in the history
  • Loading branch information
pooriaset committed May 24, 2024
1 parent 3228a6a commit 3db0ac8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Stack } from '@mui/material';
import React, { FC, ReactNode } from 'react';

export interface CheckoutBoxProps {
items: { key: ReactNode; value: ReactNode }[];
}

const CheckoutBox: FC<CheckoutBoxProps> = ({ items }) => {
return (
<Stack spacing={1}>
{items.map((item, index) => {
return (
<Stack
key={index}
justifyContent="space-between"
direction="row"
alignItems="center"
>
{item.key}
{item.value}
</Stack>
);
})}
</Stack>
);
};

export default CheckoutBox;
55 changes: 54 additions & 1 deletion src/app/[locale]/(main)/(container)/cart/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import Image from 'next/image';
import Header from './components/Header';
import CartItemSkeleton from '@/components/CartItem/components/CartItemSkeleton';
import OutOfStock from '@/components/common/OutOfStock';
import CheckoutBox, { CheckoutBoxProps } from './components/CheckoutBox';
import PriceLabel from '@/components/common/PriceLabel';

const Page = () => {
const t = useTranslations();
Expand Down Expand Up @@ -135,6 +137,55 @@ const Page = () => {
);
}

const checkoutBoxItems: CheckoutBoxProps['items'] = [
{
key: (
<Typography variant="body2" color="gray" sx={{ fontWeight: 600 }}>
قیمت کالاها ({cart?.contents?.itemCount})
</Typography>
),
value: (
<PriceLabel
value={cart.subtotal}
TypographyProps={{
fontWeight: 600,
color: 'gray',
}}
/>
),
},
{
key: (
<Typography variant="body2" sx={{ fontWeight: 600 }}>
جمع سبد خرید
</Typography>
),
value: (
<PriceLabel
value={cart.total}
TypographyProps={{
fontWeight: 600,
}}
/>
),
},
{
key: (
<Typography color="error" variant="body2" sx={{ fontWeight: 600 }}>
سود شما از خرید
</Typography>
),
value: (
<PriceLabel
value={cart.discountTotal}
TypographyProps={{
fontWeight: 600,
}}
/>
),
},
];

return (
<>
<Grid container spacing={2}>
Expand Down Expand Up @@ -200,7 +251,9 @@ const Page = () => {
<Grid item lg={3} md={6} xs={12}>
<Stack spacing={2}>
<Card variant="outlined">
<CardContent></CardContent>
<CardContent>
<CheckoutBox items={checkoutBoxItems} />
</CardContent>
<CardActions>
<Button
fullWidth
Expand Down
12 changes: 8 additions & 4 deletions src/components/common/PriceLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { extractNumbers, getMinOfRangePrice } from '@/utils/price';
import { useTranslations } from 'next-intl';

export interface PriceLabelProps {
value?: string | null;
value?: string | number | null;
TypographyProps?: Partial<TypographyProps>;
}

Expand All @@ -19,11 +19,15 @@ const PriceLabel: FC<PriceLabelProps> = ({
},
}) => {
const t = useTranslations();

const _value =
typeof value === 'string'
? extractNumbers(getMinOfRangePrice(value))
: value;

return (
<Box display="flex" alignItems="center">
<Typography {...TypographyProps}>
{extractNumbers(getMinOfRangePrice(value))?.toLocaleString()}
</Typography>
<Typography {...TypographyProps}>{_value?.toLocaleString()}</Typography>
<PriceUnit title={t('units.price')} />
</Box>
);
Expand Down

0 comments on commit 3db0ac8

Please sign in to comment.