-
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주차] 정대헌 미션 제출합니다. #21
base: master
Are you sure you want to change the base?
Changes from 1 commit
84b702d
97c1885
e34cea1
8873d43
4750dae
e99b667
0775348
4148872
d379344
6c4147b
b967efe
3e1d890
621b438
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from "react"; | ||
|
||
import { GlobalProvider } from "./context/GlobalContext"; | ||
import InputContainer from "./containers/InputContainer"; | ||
import ListItemContainer from "./containers/ListItemContainer"; | ||
import GlobalStyle from "./GlobalStyles"; | ||
import { ItemType } from "./Objects"; | ||
|
||
function App() { | ||
return ( | ||
<GlobalProvider> | ||
<GlobalStyle /> | ||
<div className="background"> | ||
<div className="container"> | ||
<InputContainer /> | ||
<ListItemContainer title={"해야할 일"} listType={ItemType.Todo} /> | ||
<ListItemContainer title={"완료한 일"} listType={ItemType.Done} /> | ||
</div> | ||
</div> | ||
</GlobalProvider> | ||
); | ||
} | ||
|
||
export default App; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,13 @@ | ||||||
export type Item = { | ||||||
id: string, | ||||||
type?: ItemType, | ||||||
content?: string, | ||||||
} | ||||||
|
||||||
export enum ItemType {Todo, Done} | ||||||
|
||||||
export type Action = {type: ActionType, payload: any} | ||||||
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. 액션은 정확하게 작성해주시는게 좋습니다. 각 action별로 payload의 타입이 전부 다를텐데, 이걸 컴파일 타임에 검사를 안해주면 런타임에 잘못된 코드가 들어왔을 때 대비가 안될 수 있죠. 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. 맞습니다, 제가 any로 두고 고치질 않았군요. 수정하겠습니다 :)
Suggested change
|
||||||
|
||||||
export enum ActionType {DELETE, ADD, MODIFY } | ||||||
|
||||||
export type State = Item[] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,10 @@ import React, { useContext } from "react"; | |
import { FaTrash } from "react-icons/fa"; | ||
import styled from "styled-components"; | ||
|
||
import { GlobalContext } from "../context/GlobalState"; | ||
import { DispatchContext} from "../context/GlobalContext"; | ||
import { Item, ActionType, ItemType } from "../Objects"; | ||
|
||
const Item = styled.div` | ||
const ItemDiv = styled.div` | ||
display: flex; | ||
width: "100%"; | ||
justify-content: space-between; | ||
|
@@ -70,11 +71,29 @@ const DeleteIcon = styled.p` | |
color: gray; | ||
`; | ||
|
||
const ListItem = ({ item }) => { | ||
const { deleteItem, moveItem } = useContext(GlobalContext); | ||
const ListItem = ({ item }: {item: Item}) => { | ||
const dispatch = useContext(DispatchContext); | ||
Comment on lines
+74
to
+75
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. 말씀하신 대로 수정해보았는데, prop를 2개를 받고 있어서 오류가 나는 것 같아요! |
||
|
||
const moveItem = (uid: string) => { | ||
dispatch({ | ||
type: ActionType.MODIFY, | ||
payload: { | ||
id: uid, | ||
}, | ||
}); | ||
}; | ||
|
||
const deleteItem = (uid: string) => { | ||
dispatch({ | ||
type: ActionType.DELETE, | ||
payload: { | ||
id: uid, | ||
}, | ||
}); | ||
}; | ||
|
||
return ( | ||
<Item> | ||
<ItemDiv> | ||
<ItemScrollDiv> | ||
<ItemFinishButton | ||
type="button" | ||
|
@@ -84,7 +103,7 @@ const ListItem = ({ item }) => { | |
> | ||
<ItemTitle> | ||
{/* listType의 done/todo에 따라 del 태그 삽입 */} | ||
{item.type === "done" ? <del>{item.content}</del> : item.content} | ||
{item.type === ItemType.Done ? <del>{item.content}</del> : item.content} | ||
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. 태그로 감싸는것도 새로운 방법이네요. 하지만 실제로는 css로 처리하는게 렌더링 성능에서 더 유리하다고 합니다! 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. 조언 감사합니다~ 저는 오히려 css로 처리하는 방법을 생각하지 못했네요, 참고하겠습니다. |
||
</ItemTitle> | ||
</ItemFinishButton> | ||
</ItemScrollDiv> | ||
|
@@ -98,7 +117,7 @@ const ListItem = ({ item }) => { | |
<FaTrash /> | ||
</DeleteIcon> | ||
</ItemDeleteButton> | ||
</Item> | ||
</ItemDiv> | ||
); | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import React, { useState, useContext } from "react"; | ||
import styled from "styled-components"; | ||
|
||
import { GlobalContext } from "../context/GlobalState"; | ||
import { DispatchContext } from "../context/GlobalContext"; | ||
import { ActionType, ItemType } from "../Objects"; | ||
|
||
const AppInput = styled.div` | ||
width: 28rem; | ||
|
@@ -58,15 +59,22 @@ const SubmitBtn = styled.button` | |
} | ||
`; | ||
|
||
const InputContainer = (props) => { | ||
const InputContainer = () => { | ||
const [text, setText] = useState(""); | ||
const { addItem } = useContext(GlobalContext); | ||
const dispatch = useContext(DispatchContext); | ||
|
||
const submitHandler = (e) => { | ||
const addItem = (uid:string, itemType: ItemType, itemContent: string) => { | ||
dispatch({ | ||
type: ActionType.ADD, | ||
payload: { id: uid, type: itemType, content: itemContent }, | ||
}); | ||
}; | ||
|
||
Comment on lines
+66
to
+72
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. 감사합니다! |
||
const submitHandler = (e:React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
if (text !== "") { | ||
const uid = new Date().getTime().toString(); | ||
addItem(uid, "todo", text); | ||
addItem(uid, ItemType.Todo, text); | ||
setText(""); | ||
} | ||
}; | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {Item, ItemType, Action, ActionType, State} from "../Objects" | ||
|
||
const AppReducer = (state: State, action: Action):State => { | ||
switch (action.type) { | ||
case ActionType.DELETE: | ||
return [...state.filter((item) => item.id !== action.payload.id)]; | ||
case ActionType.ADD: | ||
return [...state.concat([action.payload])]; | ||
case ActionType.MODIFY: | ||
return [ | ||
...state.map((item: Item) => | ||
item.id === action.payload.id | ||
? { ...item, type: item.type === ItemType.Todo ? ItemType.Done : ItemType.Todo } | ||
: item | ||
), | ||
]; | ||
Comment on lines
+9
to
+16
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. 순수함수라는건 외부에 함수를 정의하고 모듈로 불러오는 것을 말하시는 것인가요? |
||
default: | ||
return [...state]; | ||
} | ||
}; | ||
|
||
export default AppReducer; |
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.
구조가 깔끔해서 보기 좋네요
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.
감사합니다 :)