-
Notifications
You must be signed in to change notification settings - Fork 69
[클린코드 리액트 3기 한재성] 페이먼츠 미션 Step2 #140
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
Open
Han-D-Peter
wants to merge
6
commits into
next-step:han-d-peter
Choose a base branch
from
Han-D-Peter:step2
base: han-d-peter
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { v4 } from "uuid"; | ||
import { Card } from "./domains/RegisterPage/CardRegister/types"; | ||
|
||
export const initialCards: Card[] = [ | ||
{ | ||
uuid: v4(), | ||
cardType: "윤호", | ||
cardNumber: { | ||
firstNumber: "1234", | ||
secondNumber: "4321", | ||
thirdNumber: "2121", | ||
fourthNumber: "1111", | ||
}, | ||
cardHolder: "SUN", | ||
cvc: "111", | ||
holderName: "SUM", | ||
expiration: { month: "12", year: "12" }, | ||
createdAt: new Date(), | ||
}, | ||
{ | ||
uuid: v4(), | ||
cardType: "은규", | ||
cardNumber: { | ||
firstNumber: "2222", | ||
secondNumber: "2222", | ||
thirdNumber: "2222", | ||
fourthNumber: "2222", | ||
}, | ||
cardHolder: "SUN", | ||
cvc: "111", | ||
holderName: "KYU", | ||
expiration: { month: "11", year: "11" }, | ||
createdAt: new Date(), | ||
}, | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useNavigate, useSearchParams } from "react-router-dom"; | ||
import { initialCards } from "../../../constants"; | ||
import useLocalStorage from "../../../hooks/useLocalStorage"; | ||
import CardNaming, { | ||
NameQuery, | ||
} from "../../RegisterPage/CardNaming/CardNaming"; | ||
import { Card } from "../../RegisterPage/CardRegister/types"; | ||
import { omitObj } from "../../../utils"; | ||
|
||
export default function ModifyPage() { | ||
const [cards, setCards] = useLocalStorage<Card[]>("mycards", initialCards); | ||
const [param] = useSearchParams(); | ||
const navigate = useNavigate(); | ||
const uuid = param.get("uuid"); | ||
|
||
if (!uuid) throw new Error("index search 파라미터가 필수입니다."); | ||
|
||
function isThis(value: Card) { | ||
return value.uuid === uuid; | ||
} | ||
|
||
const targetCard = cards.find(isThis); | ||
|
||
if (!targetCard) return <div>존재하지 않은 카드입니다.</div>; | ||
|
||
function changeName(name: NameQuery) { | ||
const copied = [...cards]; | ||
const targetCard = copied.find(isThis); | ||
if (targetCard) targetCard["holderName"] = name.cardName; | ||
setCards(copied); | ||
navigate("/mycards"); | ||
} | ||
|
||
return ( | ||
<CardNaming | ||
card={omitObj<Omit<Card, "createdAt">>(targetCard, ["createdAt"])} | ||
onSubmit={changeName} | ||
/> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,53 @@ | ||
import { Link } from "react-router-dom"; | ||
import PlasticCard from "../component/PlaticCard/PlaticCard"; | ||
import styles from "./MyCards.module.css"; | ||
import Button from "../../components/Button/Button"; | ||
import useCards from "../../hooks/useCards"; | ||
|
||
export default function MyCards() { | ||
const { cards, setCards } = useCards({ | ||
sortByKey: "createdAt", | ||
sortMethod: "asc", | ||
}); | ||
|
||
const latest = cards.sort((a, b) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sort 메서드는 원본도 바꿉니다 사용에 유의해주세요 |
||
return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(); | ||
}); | ||
Comment on lines
+8
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 useCards에 sort option을 주고있어서 알아서 정렬을 할줄 알았는데 한 번 더 해야하나요? |
||
|
||
function deleteCard(uuid: string) { | ||
return function deleteCard() { | ||
const filtered = latest.filter((value) => uuid !== value.uuid); | ||
setCards(filtered); | ||
}; | ||
} | ||
|
||
return ( | ||
<section className={styles.cards__layout}> | ||
<div className={styles.cards__list}> | ||
<PlasticCard | ||
cardType="윤호" | ||
cardNumber={{ | ||
firstNumber: "1234", | ||
secondNumber: "1234", | ||
thirdNumber: "1234", | ||
fourthNumber: "1234", | ||
}} | ||
holderName="SUN" | ||
expiration={{ month: "12", year: "12" }} | ||
/> | ||
<PlasticCard | ||
cardType="은규" | ||
cardNumber={{ | ||
firstNumber: "1234", | ||
secondNumber: "1234", | ||
thirdNumber: "1234", | ||
fourthNumber: "1234", | ||
}} | ||
holderName="SUN" | ||
expiration={{ month: "12", year: "12" }} | ||
/> | ||
<Link to="/mycards/register"> | ||
<div className={styles.card__link}>+</div> | ||
</Link> | ||
{latest.map((card) => { | ||
return ( | ||
<div | ||
key={JSON.stringify(card.cardNumber)} | ||
className={styles.card__box} | ||
> | ||
<div className={styles.card__delBtn}> | ||
<Button onClick={deleteCard(card.uuid)}>지우기</Button> | ||
</div> | ||
<Link to={`modify?uuid=${card.uuid}`}> | ||
<PlasticCard | ||
cardType={card.cardType} | ||
cardNumber={card.cardNumber} | ||
holderName={card.cardHolder} | ||
expiration={card.expiration} | ||
/> | ||
</Link> | ||
<div>{card.holderName}</div> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</section> | ||
); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,37 @@ | ||
import { useEffect } from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { CardRegistration } from "../CardRegister/CardRegister"; | ||
import { NameQuery } from "../CardNaming/CardNaming"; | ||
import useLocalStorage from "../../../hooks/useLocalStorage"; | ||
import { Card } from "../CardRegister/types"; | ||
import { initialCards } from "../../../constants"; | ||
import { v4 } from "uuid"; | ||
|
||
interface CardResultProps { | ||
store: Record<string, unknown>; | ||
} | ||
|
||
export default function CardResult({ store }: CardResultProps) { | ||
const [cards, setCards] = useLocalStorage<Card[]>("mycards", initialCards); | ||
const navigate = useNavigate(); | ||
useEffect(() => { | ||
console.log(store); | ||
const registration = store["register"] as CardRegistration; | ||
const naming = store["naming"] as NameQuery; | ||
|
||
const newCard: Card = { | ||
uuid: v4(), | ||
...registration, | ||
holderName: naming.cardName || registration.cardType, | ||
createdAt: new Date(), | ||
expiration: registration.expirationDate, | ||
}; | ||
|
||
console.log("newCard", newCard); | ||
|
||
cards ? setCards([...cards, newCard]) : setCards([newCard]); | ||
|
||
navigate("/mycards"); | ||
}, []); | ||
}, [cards]); | ||
|
||
return <></>; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.card__container { | ||
position: relative; | ||
width: 185px; | ||
height: 115px; | ||
border-radius: 4px; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useMemo } from "react"; | ||
import { initialCards } from "../constants"; | ||
import { Card } from "../domains/RegisterPage/CardRegister/types"; | ||
import useLocalStorage from "./useLocalStorage"; | ||
|
||
type useCardsArgs = { | ||
sortByKey?: keyof Card; | ||
sortMethod?: "asc" | "dcs"; | ||
}; | ||
|
||
export default function useCards({ | ||
sortByKey = "createdAt", | ||
sortMethod = "asc", | ||
}: useCardsArgs) { | ||
const [cards, setCards] = useLocalStorage<Card[]>("mycards", initialCards); | ||
|
||
const sortedCards = useMemo(() => { | ||
const isAscended = sortMethod === "asc"; | ||
return cards.sort((a, b) => { | ||
const keyA = isAscended | ||
? a[sortByKey].toString() | ||
: b[sortByKey].toString(); | ||
const keyB = isAscended | ||
? b[sortByKey].toString() | ||
: a[sortByKey].toString(); | ||
|
||
if (keyA < keyB) return -1; | ||
if (keyA > keyB) return 1; | ||
|
||
return 0; | ||
}); | ||
}, [cards, sortByKey, sortMethod]); | ||
|
||
function deleteCard(uuid: string) { | ||
return function deleteCard() { | ||
const filtered = sortedCards.filter((value) => uuid !== value.uuid); | ||
setCards(filtered); | ||
}; | ||
} | ||
|
||
return { cards: sortedCards, setCards, deleteCard }; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
지금의 방식도 좋지만 Array.prototype.map같은 내장 메서드를 활용하면 더 선언적으로 작성할 수 있을 것 같아요!