Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Skeleton component #145

Merged
merged 2 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"react-app-rewire-typescript": "2.0.2",
"react-app-rewired": "^2.2.1",
"react-dom": "^17.0.2",
"react-loading-skeleton": "^3.0.3",
"react-redux": "^7.2.6",
"react-router-dom": "^6.2.2",
"react-scripts": "5.0.0",
Expand Down
129 changes: 81 additions & 48 deletions src/components/BestRestaurantItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import theme from '@/styles/theme';
import { BestRestaurantType } from '@/components/BestRestaurants';
import { useAppSelector } from '@/store/hooks';
import { getImageDocs } from '@/firebase/request';
import Skeleton from 'react-loading-skeleton';
import 'react-loading-skeleton/dist/skeleton.css';

interface BestRestaurantItemType {
restaurant: BestRestaurantType;
Expand All @@ -26,6 +28,7 @@ interface BestRestaurantItemType {
const BestRestaurantItem = ({ restaurant }: BestRestaurantItemType) => {
const { isUserLogin } = useAppSelector(({ user }) => user);
const [starState, setStarState] = useState(false);
const [loadState, setLoadState] = useState<boolean>(false);

const changeStar = (e: MouseEvent) => {
if (isUserLogin) {
Expand All @@ -38,56 +41,86 @@ const BestRestaurantItem = ({ restaurant }: BestRestaurantItemType) => {
const [imageSrc, setImageSrc] = useState<string>();

useEffect(() => {
getImageDocs(restaurant.images[0]).then((res: any) => setImageSrc(res));
getImageDocs(restaurant.images[0])
.then((res: any) => setImageSrc(res))
.then((res) => {
setTimeout(() => {
setLoadState(true);
}, 500);
});
}, []);

return (
<RestaurantItemLi>
<RestaurantItem>
<RestaurantImg to={`/restaurants/${restaurant.id}`}>
<img src={imageSrc} alt={imageSrc} />
</RestaurantImg>
<RestaurantInfo>
<Link to={`/restaurants/${restaurant.id}`}>
<RestaurantTitle>
{restaurant.name}
<RestaurantScore>{restaurant.score.toFixed(1)}</RestaurantScore>
</RestaurantTitle>
</Link>
<IconButton onClick={changeStar} message="가고싶다">
<Star fill={theme.colors[starState ? 'orange' : 'gray1000']} />
</IconButton>
<address>
{`${restaurant.address.city} ${restaurant.address.district} ${restaurant.address.detail}`}
</address>
<RestaurantSubInfo>
<small>영업시간</small>
<small>{restaurant.time} </small>
</RestaurantSubInfo>
<RestaurantSubInfo>
<small>대표메뉴</small>
<RestaurantMenu>
<p>
<span>{restaurant.menu[0]}</span>
<span>{restaurant.menu[1]}</span>
</p>
<p>
<span>{restaurant.menu[2]}</span>
<span>{restaurant.menu[3]}</span>
</p>
<p>
<span>{restaurant.menu[4]}</span>
<span>{restaurant.menu[5]}</span>
</p>
</RestaurantMenu>
</RestaurantSubInfo>
<Link to="/">
<RestaurantMore>{`${restaurant.name} 더보기 >`}</RestaurantMore>
</Link>
</RestaurantInfo>
</RestaurantItem>
</RestaurantItemLi>
);
if (loadState) {
return (
<RestaurantItemLi>
<RestaurantItem>
<RestaurantImg to={`/restaurants/${restaurant.id}`}>
<img src={imageSrc} alt={imageSrc} />
</RestaurantImg>
<RestaurantInfo>
<Link to={`/restaurants/${restaurant.id}`}>
<RestaurantTitle>
{restaurant.name}
<RestaurantScore>{restaurant.score.toFixed(1)}</RestaurantScore>
</RestaurantTitle>
</Link>
<IconButton onClick={changeStar} message="가고싶다">
<Star fill={theme.colors[starState ? 'orange' : 'gray1000']} />
</IconButton>
<address>
{`${restaurant.address.city} ${restaurant.address.district} ${restaurant.address.detail}`}
</address>
<RestaurantSubInfo>
<small>영업시간</small>
<small>{restaurant.time} </small>
</RestaurantSubInfo>
<RestaurantSubInfo>
<small>대표메뉴</small>
<RestaurantMenu>
<p>
<span>{restaurant.menu[0]}</span>
<span>{restaurant.menu[1]}</span>
</p>
<p>
<span>{restaurant.menu[2]}</span>
<span>{restaurant.menu[3]}</span>
</p>
<p>
<span>{restaurant.menu[4]}</span>
<span>{restaurant.menu[5]}</span>
</p>
</RestaurantMenu>
</RestaurantSubInfo>
<Link to="/">
<RestaurantMore>{`${restaurant.name} 더보기 >`}</RestaurantMore>
</Link>
</RestaurantInfo>
</RestaurantItem>
</RestaurantItemLi>
);
} else {
return (
<RestaurantItemLi>
<RestaurantItem>
<RestaurantImg to={`/restaurants/${restaurant.id}`}>
<Skeleton width={'100%'} height={'100%'} />
</RestaurantImg>
<RestaurantInfo>
<Link to={`/restaurants/${restaurant.id}`}>
<Skeleton width={'60%'} />
</Link>
<IconButton onClick={changeStar} message="">
<Skeleton width={'100%'} height={'100%'} />
</IconButton>
<address>
<Skeleton width={'70%'} height={'100%'} />
</address>
<Skeleton width={'100%'} count={5} />
</RestaurantInfo>
</RestaurantItem>
</RestaurantItemLi>
);
}
};

BestRestaurantItem.defaultProps = {
Expand Down
4 changes: 2 additions & 2 deletions src/components/BestRestaurants/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ const BestRestaurants = () => {
<>
<GrayContainer>
<ContainerTitle>
{categoryData ? categoryData.title : '맛집 베스트'}
{categoryData ? categoryData.title : ''}
</ContainerTitle>
<ContainerText>
{categoryData ? categoryData.description : '맛집 베스트'}
{categoryData ? categoryData.description : ''}
</ContainerText>
</GrayContainer>
<SortMiddel70>
Expand Down
8 changes: 8 additions & 0 deletions src/components/CategoryItem/category_item.styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ const CategoryImgBox = styled.div`
}
}
`;
const CategorySkeleton = styled.div`
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
`;

const CategoryImg = styled.img``;

Expand Down Expand Up @@ -60,4 +67,5 @@ export {
CategoryTitle,
CategoryText,
CategoryImgBox,
CategorySkeleton,
};
62 changes: 43 additions & 19 deletions src/components/CategoryItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import {
CategoryTitle,
CategoryText,
CategoryImgBox,
CategorySkeleton,
} from './category_item.styled';
import { categoryDataType } from '@/components/Carousel';
import { getImageDocs, getPostTitleDocs } from '@/firebase/request';
import { useState, useEffect } from 'react';
import Skeleton from 'react-loading-skeleton';
import 'react-loading-skeleton/dist/skeleton.css';

interface CategoryItemChecker {
categoryData: categoryDataType;
Expand All @@ -18,33 +21,54 @@ interface CategoryItemChecker {
const CategoryItem = ({ categoryData }: CategoryItemChecker) => {
const [imageSrc1, setImageSrc1] = useState<string>();
const [imageSrc2, setImageSrc2] = useState<string>();
const [loadState, setLoadState] = useState<boolean>(false);

useEffect(() => {
getPostTitleDocs(categoryData.list.slice(0, 2)).then((res) => {
getImageDocs(res[0]).then((res: any) => setImageSrc1(res));
getImageDocs(res[1]).then((res: any) => setImageSrc2(res));
});
getPostTitleDocs(categoryData.list.slice(0, 2))
.then((res) => {
getImageDocs(res[0]).then((res: any) => setImageSrc1(res));
getImageDocs(res[1]).then((res: any) => setImageSrc2(res));
})
.then((res) => {
setTimeout(() => {
setLoadState(true);
}, 1000);
});
}, []);

return (
<CategoryItemLi>
<CategoryLink
to={`./bestRestaurants/${categoryData.link}`}
state={categoryData}
>
if (loadState) {
return (
<CategoryItemLi>
<CategoryLink
to={`./bestRestaurants/${categoryData.link}`}
state={categoryData}
>
<figure>
<CategoryImgBox>
<img src={imageSrc1} alt={imageSrc1} />
<img src={imageSrc2} alt={imageSrc2} />
</CategoryImgBox>
<CategoryInfo>
<CategoryTitle>{categoryData.title}</CategoryTitle>
<CategoryText>{categoryData.description}</CategoryText>
</CategoryInfo>
</figure>
</CategoryLink>
</CategoryItemLi>
);
} else {
return (
<CategoryItemLi>
<figure>
<CategoryImgBox>
<img src={imageSrc1} alt={imageSrc1} />
<img src={imageSrc2} alt={imageSrc2} />
<CategorySkeleton>
<Skeleton width={'100%'} height={'100%'} />
</CategorySkeleton>
</CategoryImgBox>
<CategoryInfo>
<CategoryTitle>{categoryData.title}</CategoryTitle>
<CategoryText>{categoryData.description}</CategoryText>
</CategoryInfo>
</figure>
</CategoryLink>
</CategoryItemLi>
);
</CategoryItemLi>
);
}
};

export default CategoryItem;
Loading