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회차 과제 - 구준혁 #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

/public
package-lock.json
package.json
README.md
4 changes: 0 additions & 4 deletions README.md

This file was deleted.

18,535 changes: 0 additions & 18,535 deletions package-lock.json

This file was deleted.

39 changes: 0 additions & 39 deletions package.json

This file was deleted.

18 changes: 0 additions & 18 deletions public/index.html

This file was deleted.

3 changes: 0 additions & 3 deletions public/robots.txt

This file was deleted.

78 changes: 45 additions & 33 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,73 @@
import styled from "styled-components";
import TodoList from "./TodoList";
import { useState } from "react";
import styled from 'styled-components';
import TodoList from './TodoList';
import { useState } from 'react';

const fake = [
{
id : 1,
body : "우유 사기"
id: 1,
body: '우유 사기',
complete: false,
},
{
id : 2,
body : "공부하기"
id: 2,
body: '공부하기',
complete: false,
},
{
id : 3,
body : "집가고싶다"
}
]
id: 3,
body: '집가고싶다',
complete: false,
},
];

function App() {

const [todoList, setTodoList] = useState(fake)
const [inputString, setInputString] = useState("")


const [todoList, setTodoList] = useState(fake);
const [inputString, setInputString] = useState('');

const appendTodo = (event) => {
event.preventDefault()
const new_Todo = [...todoList, {id: Date.now(), body: inputString}];
event.preventDefault();
const new_Todo = [...todoList, { id: Date.now(), body: inputString, complete: false }];
setTodoList(new_Todo);
setInputString('')
}
setInputString('');
};

const handleInput = (event) => {
setInputString(event.target.value)
}
setInputString(event.target.value);
};

const handleDelete = (id) => {
const new_Todo = todoList.filter((el) => {
if (el.id === id) {
return false
return false;
} else {
return true
return true;
}
})
setTodoList(new_Todo)
}
});
setTodoList(new_Todo);
};

const handleComplete = (id) => {
const new_Todo = todoList.map((el) => {
if (el.id === id) {
if (el.complete === false) {
el.complete = true;
} else {
el.complete = false;
}
return el;
} else {
return el;
}
});
setTodoList(new_Todo);
};
Copy link

Choose a reason for hiding this comment

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

토글 상태를 true->false , false -> true로 바뀌면서 완료했던 버튼이 다시 되돌아오는 것이군요!!

Copy link
Author

Choose a reason for hiding this comment

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

맞습니다 !! 다만 연산자 활용을 좀 더 잘 했다면 코드를 더 줄일 수 있겠다는 생각이 듭니다!


return (
<Container>
<form onSubmit={appendTodo}>
<TodoInput onChange={handleInput} value={inputString}></TodoInput>
</form>
<TodoList todoList={todoList} handleDelete={handleDelete}></TodoList>
<TodoList todoList={todoList} handleDelete={handleDelete} handleComplete={handleComplete}></TodoList>
</Container>
);
}
Expand All @@ -61,7 +76,7 @@ const Container = styled.div`
width: 70%;
height: 100vh;
margin: auto;
`
`;

const TodoInput = styled.input`
width: 100%;
Expand All @@ -70,9 +85,6 @@ const TodoInput = styled.input`
border-radius: 10px;
box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
box-sizing: border-box;
`

`;

export default App;


28 changes: 18 additions & 10 deletions src/TodoList.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import React from 'react'
import styled from 'styled-components'
import TodoListItem from './TodoListItem'
import React from 'react';
import styled from 'styled-components';
import TodoListItem from './TodoListItem';

const TodoList = ({todoList, handleDelete}) => {
const TodoList = ({ todoList, handleDelete, handleComplete }) => {
return (
<Box>
<h1>할 일 목록</h1>
{todoList.map((el) => {
return <TodoListItem handleDelete={handleDelete} key={el.id} id={el.id} body={el.body}></TodoListItem>
return (
<TodoListItem
handleDelete={handleDelete}
handleComplete={handleComplete}
key={el.id}
id={el.id}
body={el.body}
complete={el.complete}
></TodoListItem>
);
})}
</Box>
)
}
);
};

const Box = styled.div`
margin-top: 50px;
background: #eeeeee;
padding: 50px 20px;
box-sizing: border-box;
border-radius: 15px;
`;

`

export default TodoList
export default TodoList;
45 changes: 33 additions & 12 deletions src/TodoListItem.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import React from 'react'
import styled from 'styled-components'

const TodoListItem = ({id, body, handleDelete}) => {


import React from 'react';
import styled from 'styled-components';

const TodoListItem = ({ id, body, complete, handleDelete, handleComplete }) => {
return (
<Box>
<p>{body}</p>
<button onClick={() => {handleDelete(id)}}>삭제</button>
{complete === false ? <p>{body}</p> : <IsCompleted>{body}</IsCompleted>}
Copy link

Choose a reason for hiding this comment

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

app.js/ const handleComplete = (id) => {....} 이 함수안 에서도 이 부분이 있는거같은데 코드가 중복으로 쓰이는거 아닌가요 ?

Copy link
Author

Choose a reason for hiding this comment

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

app.js의 해당 코드는 complete값을 바꿔주는 코드입니다!
현재 파일의 코드는 complete 값에 따라 다른 컴포넌트로 {body}를 출력하는 코드입니다!

<ButtonBox>
<button
onClick={() => {
handleDelete(id);
}}
>
삭제
</button>
<button
onClick={() => {
handleComplete(id);
}}
>
완료
</button>
</ButtonBox>
</Box>
)
}
);
};

const Box = styled.div`
background-color: white;
Expand All @@ -22,6 +34,15 @@ const Box = styled.div`
border-radius: 5px;
display: flex;
justify-content: space-between;
`
`;
const ButtonBox = styled.div`
display: flex;
gap: 10px;
`;

const IsCompleted = styled.p`
color: gray;
text-decoration: line-through;
`;

export default TodoListItem
export default TodoListItem;