-
Notifications
You must be signed in to change notification settings - Fork 13
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
[3주차] 전시원 미션 제출합니다. #23
base: master
Are you sure you want to change the base?
Changes from 1 commit
c6feae2
f5b938e
4427d83
4b54ec9
e8bbcba
6b36e58
28017c7
bb2753b
e9d358b
743fdc4
352336c
cd6291c
11967de
5274f66
544079f
b2cebe8
ac130c1
8438aa6
ac54062
e8a6881
0ad9ee6
ddcb16c
c3ef0e8
fe2b790
9b333ab
85b97cd
7554c72
f0f19a2
96519d5
1976c20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,33 @@ | ||
import React from 'react'; | ||
|
||
export interface ITodoList { | ||
contents: string; | ||
id: number; | ||
isDone: boolean; | ||
} | ||
export interface IYetListProps { | ||
list: []; | ||
yetNum: number; | ||
onToggle: (e: React.MouseEvent<HTMLButtonElement>) => void; | ||
onDelete: (e: React.MouseEvent<HTMLButtonElement>) => void; | ||
} | ||
|
||
export interface IInputFormProps { | ||
onSubmit: (e: React.SyntheticEvent) => void; | ||
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void; | ||
contents: string; | ||
} | ||
|
||
export interface IDoneList { | ||
list: []; | ||
onToggle: (e: React.MouseEvent<HTMLButtonElement>) => void; | ||
doneNum: number; | ||
} | ||
// styled-component에 들어가는 property 에 대한 정의 | ||
export interface IListBtn { | ||
onClick: (e: React.MouseEvent<HTMLButtonElement>) => void; | ||
} | ||
|
||
// 클릭 : MouseEvent | ||
// 입력 : ChangeEvent | ||
// 제출 : SyntheticEvent (선종님이 말씀하신 건데 맞는지 모르겠음) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,65 @@ | ||
import styled from 'styled-components'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import Title from '../Title'; | ||
import React, { useEffect, useState } from 'react'; | ||
import InputForm from '../InputForm'; | ||
import YetList from '../YetList'; | ||
import DoneList from '../DoneList'; | ||
import { ITodoList } from 'interface'; | ||
|
||
const Index = () => { | ||
const listText = 'list'; | ||
// 텍스트 이름과 변수 이름을 같게하면 좋을 것 같다는 피드백을 받았던 거 같은데 | ||
// list 를 다른 변수에 사용해 버렸음. 변수 이름 정하는 센스를 더 길러야 겠음 | ||
|
||
const localData = JSON.parse(localStorage.getItem(listText)) || []; | ||
const localData = JSON.parse(localStorage.getItem(listText) || '') || []; | ||
// localDate 를 처음에 가져와서 새로고침해도 데이터가 유지되게 설정 | ||
|
||
const [yetNum, setYetNum] = useState(0); | ||
const [doneNum, setDoneNum] = useState(0); | ||
const [contents, setContents] = useState(''); | ||
const [list, setList] = useState(localData); | ||
const onChange = (e) => setContents(e.target.value); | ||
const onSubmit = (e) => { | ||
|
||
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => | ||
setContents(e.target.value); | ||
const onSubmit = (e: React.SyntheticEvent) => { | ||
e.preventDefault(); | ||
const obj = { | ||
contents, | ||
id: Date.now(), | ||
isDone: false, | ||
}; | ||
setList((prev) => [...prev, obj]); | ||
setList((prev: []) => [...prev, obj]); | ||
setContents(''); | ||
}; | ||
const onToggle = (e) => { | ||
const onToggle = (e: React.MouseEvent<HTMLButtonElement>) => { | ||
// 클릭된 값 | ||
const text = e.target.textContent; | ||
|
||
const text = (e.target as HTMLElement).textContent; | ||
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. TS에 as라는 문법이 있는지 처음 알게되었습니다. 감사합니다! 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. 저도 구글에서 찾아보다 그냥 가져와서 알게됐어요 ㅎㅎ; 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. HTML 구조에 의존적인 로직보다는, 클릭한 요소의 id를 필터링해서 토글시키는 방법이 좋을 것 같습니다. |
||
// 로컬스토리지에 저장된 값 | ||
const data = JSON.parse(localStorage.getItem(listText)); | ||
const data = JSON.parse(localStorage.getItem(listText) || ''); | ||
// 두 값을 이용해서 클릭된 값만 isDone 상태 토글 | ||
let indexClicked = 0; | ||
let obj; | ||
data.map((item, index) => { | ||
data.map((item: ITodoList, index: number) => { | ||
if (item.contents === text) { | ||
obj = { ...item }; | ||
obj.isDone = !item.isDone; | ||
indexClicked = index; | ||
} else { | ||
// switch 에서 default 값 처럼 if 를 쓰면 else 도 써주는 게 좋다는 코멘트를 봤던 거 같은데 | ||
// 그럴 경우 여긴 뭘 넣어야 좋을까요? | ||
// null ???! | ||
} | ||
}); | ||
const updatedDate = [...data]; | ||
updatedDate[indexClicked] = obj; | ||
setList(updatedDate); | ||
localStorage.setItem(listText, JSON.stringify(list)); | ||
}; | ||
const onDelete = (e) => { | ||
const onDelete = (e: React.MouseEvent<HTMLButtonElement>) => { | ||
// 클릭된 텍스트 찾기 | ||
const text = e.target.parentNode.innerText.slice(0, -4); | ||
|
||
const text = ( | ||
(e.target as HTMLElement).parentNode as HTMLElement | ||
).innerText.slice(0, -4); | ||
Comment on lines
+61
to
+63
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. 맞습니다, 이상한 공백을 끌고 오더라구요 innerText하면 |
||
|
||
// 현재 데이터 가져오기 | ||
const data = JSON.parse(localStorage.getItem(listText)); | ||
const data = JSON.parse(localStorage.getItem(listText) || ''); | ||
|
||
// 데이터 삭제 | ||
const updatedDate = data.filter((item) => { | ||
const updatedDate = data.filter((item: ITodoList) => { | ||
return item.contents.trim() !== text.trim(); | ||
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. trim이라는 함수를 찾아보니까 문자열 양끝 공백을 사라지게 하는 함수군요 ! 디테일 배워갑니당 |
||
}); | ||
|
||
|
@@ -81,12 +79,12 @@ const Index = () => { | |
}, [list]); | ||
|
||
const findNum = () => { | ||
const data = JSON.parse(localStorage.getItem(listText)); | ||
const data = JSON.parse(localStorage.getItem(listText) || ''); | ||
let yetCtn = 0; | ||
let doneCtn = 0; | ||
|
||
// isDone === true, false 인 경우에 따라서 개수를 세어 줘야 함 | ||
data.map((item) => { | ||
data.map((item: ITodoList) => { | ||
if (item.isDone === false) { | ||
yetCtn += 1; | ||
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. (!item.isdone) 이런 식으로 바꾸어주면 훨씬 좋을 것 같아요! |
||
} else { | ||
|
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인지 좀더 정확하게...!