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

[7주차 과제] 웹 비밀 편지 만들기 #6

Merged
merged 28 commits into from
Feb 25, 2023
Merged

[7주차 과제] 웹 비밀 편지 만들기 #6

merged 28 commits into from
Feb 25, 2023

Conversation

seojisoosoo
Copy link
Collaborator

@seojisoosoo seojisoosoo commented Dec 27, 2022

✨ 구현 기능 명세

  • 편지의 잠금모달 구현
  • 잠금모달 클릭 시, 힌트를 보고 비밀번호를 맞추는 체크모달 구현
  • 비밀번호를 맞추면 편지가 등장하도록 구현
  • 글쓰기 버튼을 누르면 편지를 쓸 수 있는 모달이 등장하도록 구현
  • 편지를 작성하면 동일하게 편지가 체크모달과 잠금모달로 감싸진 채 생성되도록 구현

🎁 PR Point

types/common.ts

  • 미리 지정할 수 있는 타입들을 지정해주었어요!!
export interface List {
    writer: string;
    message: string;
    password: string;
    hint: string;
}

export interface SecretList{
    hint:string; 
    password:string
}

mocks/handlers.ts

  • messageList라는 더미데이터를 만들고, msw로 모킹했어요! get과 post모두 msw로 구현했습니다.
  • get은 잘되는데 post에서 애를 많이 먹었어요. 자스에서도 post할 때 그냥 req.body를 받아오는 방식으로 했어서 이번에도 그렇게 했더니 에러가 나더라고요! req.bodyawait req.json() 으로 바꾸어주니 잘 되었습니다.
    rest.post<List>("/letters", async (req, res, ctx) => {
        await sleep(200);
        messageList.push(await req.json())
        return res(ctx.status(201), ctx.json(messageList));
    }),

components/CheckModal.tsx

  • props를 넘길 때 에러가 정말 많이 나서 이렇게 저렇게 다 해봤는데, 이런식으로 넘겨받는 props에도 타입을 지정해주어야한다는 사실을 깨달았습니다!
    function CheckModal({hint, password}:SecretList): JSX.Element

components/Message.tsx

  • 원래는 useRef를 이용해서, ref.current.value로 input값을 받아오려고 했는데요, 자꾸만 ref값이 null일 수 있다는 에러가 떠서, 다음과 같이 onChange 이벤트를 주고, useState를 이용했습니다.
    <input type="text" placeholder="글쓴이" onChange={({ target: { value } }) => setWriter(value)}/>
  • get, post 에 맞춰서 컴포넌트를 분리하고 싶었는데, 분리를 하니까 msw에서 post랑 get이 따로 놀더라고요(post했는데, get이 안되는 문제..), 그래서 get, post를 Message 컴포넌트 하나에서 모두 구현했습니다.

😭 소요 시간, 어려웠던 점

  • 10h
  • msw를 쓰는데 POST에서 body를 어떻게 주는지 몰라 헤맸어요
  • 타입스크립트 props를 넘기는 과정에서 타입을 주지 않아서 헤맸어요
  • 하지만 모두 해결!!! 햄복하다!

😎 구현 결과물

2022-12-28.4.18.47.mov
2022-12-28.4.29.02.mov

📚 레퍼런스

타입스크립트 초기세팅
타입스크립트 msw
타입스크립트 msw 타입 지정
타입스크립트 msw POST
타입스크립트 msw req.body 에러 해결방법
JSON.parse
POST fetch로 구현
css grid 사용하기
타입스크립트 이벤트 타입 지정

Copy link
Member

@Happhee Happhee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와넘 야무진데 ?!! 지슈슈 체고다!!


export default function Header() {
return (
<h1>💌웹 비밀 편지💌</h1>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넘 기여운데?! ㅎㅅㅎ 😄

@@ -0,0 +1,6 @@
export interface List {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export interface List {
export interface LetterInfo

로 조금더 인터페이스명이 명확했음 좋겠다!
그리구 types/common.ts로 했는데, 여기서는 공통으로 타입이 사용되는게 맞지만 만약 기능이 많다면, types/letter.ts 로 파일명 지정하는게 관리하기 편할거야!

<>
<Header/>
<button type='button'>글쓰기</button>
{messages.map(({writer, message},i) => (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i 보다는 idx 라고 지칭해주자! 너무 약어는 지양하자구>.<

Comment on lines 44 to 59
axios.post("/letters",
{
"writer": writer,
"message": message,
"password": password,
"hint": hint
}
).then((res) => {
console.log(res)
axios.get("/letters")
.then((res) => {
setMessage("")
setMessages(res.data);
});
});
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/lib폴더를 만들고, axios로 instance 만든다음에 그 객체를 사용해서 letterApi.ts 만들고 내부에서 post 함수 export해서 import하는 방법으로 코드를 짜보자!

in-sopt 서히꺼 참교!


<button disabled={!message}>추가</button>
</form>
<button disabled={!message}>추가</button>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

버튼 타입오디쪄!

Comment on lines 3 to 7
interface SecretList{
hint:string;
password:string
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
interface SecretList{
hint:string;
password:string
}
interface CheckModalProps{
hint:string;
password:string
}

로 사용하는게 적당하지아느까?! 그냥 나의 개인적인 의견!! 왜냠 컴포넌트의 props로 사용하고 이쓰니까아!!

Comment on lines 3 to 7
interface SecretList{
hint:string;
password:string
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
interface SecretList{
hint:string;
password:string
}
interface CheckModalProps{
hint:string;
password:string
}

로 사용하는게 적당하지아느까?! 그냥 나의 개인적인 의견!! 왜냠 컴포넌트의 props로 사용하고 이쓰니까아!!

)
}

const StCheckModalWrapper=styled.div`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

article은 어떄?!

Copy link
Member

@hayounSong hayounSong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생했어 지수야!! 너가 타스 걱정했던것 치곤...너무 잘했는데..? 고생많았어 정말 그리고 많이 배워가!! 앱잼도 화이팅!!!


export default function CheckModal({hint, password}:SecretList): JSX.Element {
const [visible, setVisible] = useState<boolean>(true)
const [text, setText]=useState<string>("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 빼먹지 않고 State에도 타입선언 다 해주는거 너무 좋다!!

worker.start();
}

const root = ReactDOM.createRoot(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 root에도 타입 선언이 있으면 좋지 않을까 하는 의견..!

fetchData();
}, []);

const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FormEvent를 이용해서 이런식으로 타입 선언을 해주는 구조구나.. 또 배워갑니다!!

@@ -0,0 +1,11 @@
export interface List {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 필요한 type들 외부에서 구분해주는거 좋은거같아!!

@seojisoosoo seojisoosoo merged commit 99fc714 into main Feb 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants