Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pooriaset committed May 19, 2024
2 parents 8874722 + 32fafa5 commit 18f5038
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 6 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"Swipeable",
"swiper",
"usehooks",
"Woocommerce",
"Yekan",
"چنده"
]
Expand Down
8 changes: 3 additions & 5 deletions src/components/VariableProductItem/VariableProductItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ export interface ProductItemProps {
}

const VariableProductItem: FC<ProductItemProps> = ({ data }) => {
const { isMobile } = useAppContext();

const size = isMobile ? 120 : 240;
const { isMobile, variantImageSize } = useAppContext();

const profitMarginPercentage = getProfitPercentage(
extractNumbers(getMinOfRangePrice(data.price)),
Expand Down Expand Up @@ -61,8 +59,8 @@ const VariableProductItem: FC<ProductItemProps> = ({ data }) => {
}}
>
<Image
height={size}
width={size}
height={variantImageSize}
width={variantImageSize}
src={data.image?.sourceUrl!}
alt="Product Image"
style={{
Expand Down
63 changes: 63 additions & 0 deletions src/components/VariableProductItem/VariableProductItemSkeleton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use client';

import { useAppContext } from '@/hooks/useAppContext';
import { Box, Card, CardContent, Skeleton } from '@mui/material';

const VariableProductItemSkeleton = () => {
const { variantImageSize, isMobile } = useAppContext();

return (
<Card
variant="outlined"
sx={{
display: 'block',
height: '100%',
}}
>
<CardContent
sx={{
display: 'flex',
flexDirection: 'column',
}}
>
<Box
sx={{
display: 'flex',
flexDirection: isMobile ? 'row' : 'column',
gap: 1,
}}
>
<Skeleton height={variantImageSize} width="100%" />
{!isMobile && <Box mt={3} />}

<Box
sx={{
display: 'flex',
flexDirection: 'column',
justifyContent: 'space-between',
gap: 1,
width: '100%',
}}
>
<Skeleton variant="text" width={90} />

<Box
sx={{
display: 'flex',
alignItems: 'top',
justifyContent: 'space-between',
mt: 1,
}}
>
<Box>
<Skeleton variant="text" width={40} />
</Box>
</Box>
</Box>
</Box>
</CardContent>
</Card>
);
};

export default VariableProductItemSkeleton;
1 change: 1 addition & 0 deletions src/components/VariableProductItem/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as VariableProductItem } from './VariableProductItem';
export { default as VariableProductItemSkeleton } from './VariableProductItemSkeleton';
export * from './types';
2 changes: 2 additions & 0 deletions src/config/images.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const MOBILE_PRODUCT_VARIANT_IMAGE_SIZE = 120;
export const DESKTOP_PRODUCT_VARIANT_IMAGE_SIZE = 240;
3 changes: 3 additions & 0 deletions src/contexts/appContext.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { DESKTOP_PRODUCT_VARIANT_IMAGE_SIZE } from '@/config/images';
import { createContext } from 'react';

export interface IAppContext {
isMobile: null | boolean;
variantImageSize: number;
}
export const appContext = createContext<IAppContext>({
isMobile: null,
variantImageSize: DESKTOP_PRODUCT_VARIANT_IMAGE_SIZE,
});
11 changes: 10 additions & 1 deletion src/providers/AppProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use client';

import {
DESKTOP_PRODUCT_VARIANT_IMAGE_SIZE,
MOBILE_PRODUCT_VARIANT_IMAGE_SIZE,
} from '@/config/images';
import { appContext } from '@/contexts/appContext';
import { Theme, useMediaQuery } from '@mui/material';
import { userAgent } from 'next/server';
Expand All @@ -15,10 +19,15 @@ const AppProvider: FC<PropsWithChildren<AppProviderProps>> = ({
theme.breakpoints.down('md'),
);

const isMobile = userAgent.device.type === 'mobile' || inMobileView;

return (
<appContext.Provider
value={{
isMobile: userAgent.device.type === 'mobile' || inMobileView,
isMobile,
variantImageSize: isMobile
? MOBILE_PRODUCT_VARIANT_IMAGE_SIZE
: DESKTOP_PRODUCT_VARIANT_IMAGE_SIZE,
}}
>
{children}
Expand Down

0 comments on commit 18f5038

Please sign in to comment.