Skip to content

Commit

Permalink
2022-04-25
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaaaaann committed Apr 25, 2022
1 parent 16f2a08 commit de1bd6a
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 7 deletions.
2 changes: 1 addition & 1 deletion components/common/PageTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TemplateContainer = styled.div`
`;

const TemplateChildrenWrap = styled.div`
width: 1130px;
width: 1052px;
min-height: calc(100vh - 74px);
height: auto;
display: flex;
Expand Down
2 changes: 1 addition & 1 deletion components/header/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const HeaderContainer = styled.div`
`;

export const HeaderWrap = styled.div`
width: 1130px;
width: 1052px;
height: 100%;
display: flex;
align-items: center;
Expand Down
2 changes: 2 additions & 0 deletions components/main/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Coin } from "../../types/common/common.type";
import MainCardList from "./mainCardList/MainCardList";
import MainFavoriteCardList from "./mainFavoriteCardList/MainFavoriteCardList";
import { MainContainer } from "./style";

type Props = {
Expand All @@ -10,6 +11,7 @@ const Main = ({ data }: Props) => {
return (
<MainContainer>
<MainCardList data={data === null ? null : data} />
<MainFavoriteCardList />
</MainContainer>
);
};
Expand Down
30 changes: 27 additions & 3 deletions components/main/mainCardList/mainCard/MainCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,45 @@ import { Coin } from "../../../../types/common/common.type";
import {
MainCardContainer,
MainCardDetailButton,
MainCardFavIcon,
MainCardTitle,
MainCardTitleWrap,
} from "./style";
import { FiArrowRight } from "@react-icons/all-files/fi/FiArrowRight";
import { FaRegStar } from "@react-icons/all-files/fa/FaRegStar";
import { FaStar } from "@react-icons/all-files/fa/FaStar";
import Link from "next/link";
import useFavorites from "../../../../hooks/main/useFavorites";
import { useRecoilValue } from "recoil";
import { mainFavoriteAtom } from "../../../../store/main/main.store";
import { useMemo, useState } from "react";

type Props = {
data: Coin;
};

const MainCard = ({ data }: Props) => {
const favorites = useRecoilValue(mainFavoriteAtom);
const isPick = useMemo(() => {
return favorites.find((prev) => prev === data.market) === undefined
? false
: true;
}, [data.market, favorites]);

const [pick, setPick] = useState(isPick);

const { handlePick } = useFavorites({ pick, setPick });

return (
<MainCardContainer>
<MainCardTitle>
{data.korean_name}({data.english_name})
</MainCardTitle>
<MainCardTitleWrap>
<MainCardTitle>
{data.korean_name}({data.english_name})
</MainCardTitle>
<MainCardFavIcon onClick={() => handlePick(data.market)}>
{pick ? <FaStar /> : <FaRegStar />}
</MainCardFavIcon>
</MainCardTitleWrap>
<MainCardDetailButton>
<Link href={`/coinDetail/${data.market}`}>
<a>
Expand Down
15 changes: 15 additions & 0 deletions components/main/mainCardList/mainCard/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@ export const MainCardContainer = styled.div`
padding: 20px;
`;

export const MainCardTitleWrap = styled.div`
display: flex;
align-items: center;
justify-content: space-between;
`;

export const MainCardTitle = styled.h1`
font-size: 14px;
line-height: 1.86;
font-weight: 700;
color: ${({ theme }) => theme.contrast};
`;

export const MainCardFavIcon = styled.div`
width: 18px;
height: 18px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
`;

export const MainCardDetailButton = styled.button`
background: none;
border: 0px;
Expand Down
12 changes: 12 additions & 0 deletions components/main/mainFavoriteCardList/MainFavoriteCardList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Coin } from "../../../types/common/common.type";
import { MainFavCardListContainer } from "./style";

type Props = {
data: Coin[] | null;
};

const MainFavoriteCardList = () => {
return <MainFavCardListContainer></MainFavCardListContainer>;
};

export default MainFavoriteCardList;
9 changes: 9 additions & 0 deletions components/main/mainFavoriteCardList/style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import styled from "styled-components";

export const MainFavCardListContainer = styled.div`
width: 516px;
display: flex;
flex-wrap: wrap;
column-gap: 20px;
row-gap: 20px;
`;
3 changes: 2 additions & 1 deletion components/main/style.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import styled from "styled-components";

export const MainContainer = styled.div`
background-color: ${({ theme }) => theme.backgroundColor};
display: flex;
justify-content: space-between;
`;
1 change: 1 addition & 0 deletions constants/localStorage.contants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LOCAL_FAVORITES_KEY = "favorites" as const;
2 changes: 1 addition & 1 deletion constants/product.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const COINS_MAX_NUM = 200 as const;

export const COIN_DETAIL_CATEGORY = [
{ title: "일별시세" },
{ title: "기업정보" },
{ title: "화폐정보" },
] as const;

export const COIN_TICKERS_START_DATE = "14" as const;
Expand Down
47 changes: 47 additions & 0 deletions hooks/main/useFavorites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Dispatch, SetStateAction, useEffect, useState } from "react";
import { useRecoilState } from "recoil";
import { LOCAL_FAVORITES_KEY } from "../../constants/localStorage.contants";
import { mainFavoriteAtom } from "../../store/main/main.store";
import local from "../../util/local";

type Props = {
pick: boolean;
setPick: Dispatch<SetStateAction<boolean>>;
};

const useFavorites = ({ pick, setPick }: Props) => {
const [favorites, setFavorites] = useRecoilState(mainFavoriteAtom);

const handlePick = (coinid: string) => {
handleFavorites(coinid, pick);
setPick((prev) => !prev);
};

const handleFavorites = (coinid: string, pick: boolean) => {
const copyFav = favorites;

if (!pick) {
const isOverlap = favorites.find((prev) => prev === coinid);

if (isOverlap !== undefined) {
return;
}

const addFav = copyFav.concat(coinid);

setFavorites(addFav);
local.set(LOCAL_FAVORITES_KEY, JSON.stringify(addFav));
} else {
const removeFav = copyFav.filter((prev) => prev !== coinid);
setFavorites(removeFav);
local.set(LOCAL_FAVORITES_KEY, JSON.stringify(removeFav));
}
};

return {
pick,
handlePick,
};
};

export default useFavorites;
11 changes: 11 additions & 0 deletions store/main/main.store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { atom } from "recoil";
import { LOCAL_FAVORITES_KEY } from "../../constants/localStorage.contants";
import local from "../../util/local";

export const mainFavoriteAtom = atom<string[]>({
key: "mainFavoriteAtom",
default:
local.get(LOCAL_FAVORITES_KEY) === null
? []
: JSON.parse(local.get(LOCAL_FAVORITES_KEY) as string),
});

0 comments on commit de1bd6a

Please sign in to comment.