Skip to content

Commit

Permalink
feat : todo add, del, check
Browse files Browse the repository at this point in the history
  • Loading branch information
parkqjunseo committed Oct 10, 2023
1 parent 6160ca2 commit ccce4db
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 34 deletions.
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@
"sourceType": "module"
},
"plugins": ["react"],
"rules": {}
"rules": {
"prettier/prettier": {
"warn": {
"endOfLine": "auto"
}
}
}
}
28 changes: 23 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"gh-pages": "^6.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.1.2",
"react-scripts": "5.0.1",
"react-icons": "^4.11.0",
"react-redux": "^8.1.3",
"react-scripts": "^5.0.1",
"redux": "^4.2.1",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
24 changes: 0 additions & 24 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,10 @@
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file removed public/logo192.png
Binary file not shown.
Binary file removed public/logo512.png
Binary file not shown.
10 changes: 9 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import React from 'react';
import InputForm from 'components/InputForm';
import TodoList from 'components/TodoList';

function App() {
return <h1 className="text-3xl font-bold underline">Hello, World!</h1>;
return (
<div>
<h1>니할일이뭔데그래서...</h1>
<InputForm />
<TodoList />
</div>
);
}

export default App;
30 changes: 30 additions & 0 deletions src/common/actions.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const ADD = 'ADD_TODO';
export const DELETE = 'DELETE_TODO';
export const TOGGLE = 'TOGGLE_TODO';

let prevId = 0;

export const addTodo = (todo) => {
return {
type: ADD,
todo: {
id: prevId++,
text: todo.text,
done: todo.done,
},
};
};

export const deleteTodo = (id) => {
return {
type: DELETE,
id,
};
};

export const toggleTodo = (id) => {
return {
type: TOGGLE,
id,
};
};
28 changes: 28 additions & 0 deletions src/common/reducer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ADD, DELETE, TOGGLE } from './actions';

const initialState = {
todos: [],
};

export const reducer = (state = initialState, action) => {
switch (action.type) {
case ADD:
return {
todos: [...state.todos, action.todo],
};

case TOGGLE:
return {
...state,
todos: state.todos.map((todo) => (todo.id === action.id ? { ...todo, done: !todo.done } : todo)),
};

case DELETE:
return {
todos: [...state.todos.filter((todo) => todo.id !== action.id)],
};

default:
return state;
}
};
47 changes: 47 additions & 0 deletions src/components/InputForm.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { useState } from 'react';
import { useDispatch } from 'react-redux';
import { addTodo } from 'common/actions';

const InputForm = () => {
const dispatch = useDispatch();

const [content, setContent] = useState('');

const handleChange = (e) => {
const { value } = e.target;
setContent(value);
};

const handleClick = () => {
const todo = {
text: content,
done: false,
};

dispatch(addTodo(todo));
setContent('');
};

const handleKeypress = (e) => {
if (e.key === 'Enter') {
handleClick();
}
};

return (
<div>
<input
type="text"
placeholder="할 일이 먼데"
onChange={handleChange}
value={content}
onKeyDown={handleKeypress}
/>
<button onClick={handleClick} type="submit">
+
</button>
</div>
);
};

export default InputForm;
45 changes: 45 additions & 0 deletions src/components/TodoItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';
import { useDispatch } from 'react-redux';
import PropTypes from 'prop-types';
import { MdCheckBox, MdCheckBoxOutlineBlank } from 'react-icons/md';
import { deleteTodo, toggleTodo } from 'common/actions';

const TodoItem = ({ todo }) => {
const dispatch = useDispatch();

const { id, text, done } = todo;

const handleClick = () => {
dispatch(deleteTodo(id));
};

const handleCheckboxChange = () => {
dispatch(toggleTodo(id));
};

return (
<div>
<div className="check-box">
{done ? (
<MdCheckBox onClick={handleCheckboxChange} />
) : (
<MdCheckBoxOutlineBlank onClick={handleCheckboxChange} />
)}
</div>
<span>{text}</span>
<button onClick={handleClick} type="button">
안해!!!
</button>
</div>
);
};

TodoItem.propTypes = {
todo: PropTypes.shape({
id: PropTypes.number.isRequired,
text: PropTypes.string.isRequired,
done: PropTypes.bool.isRequired,
}).isRequired,
};

export default TodoItem;
17 changes: 17 additions & 0 deletions src/components/TodoList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import { useSelector } from 'react-redux';
import TodoItem from './TodoItem';

const TodoList = () => {
const todos = useSelector((state) => state.todos);

return (
<div>
{todos.map((todo) => (
<TodoItem key={todo.id} todo={todo} />
))}
</div>
);
};

export default TodoList;
12 changes: 11 additions & 1 deletion src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import React from 'react';
import { createStore } from 'redux';
import ReactDOM from 'react-dom/client';

import './index.css';
import { Provider } from 'react-redux';
import { reducer } from './common/reducer';
import App from './App';

const reduxStore = createStore(reducer);
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);

root.render(
<Provider store={reduxStore}>
<App />
</Provider>,
);

0 comments on commit ccce4db

Please sign in to comment.