diff --git a/package.json b/package.json
index 4488cb3..84d3985 100644
--- a/package.json
+++ b/package.json
@@ -2,12 +2,16 @@
"name": "swppfront",
"version": "0.1.0",
"private": true,
+ "proxy": "http://localhost:8000",
"dependencies": {
"react": "^16.9.0",
"react-dom": "^16.9.0",
+ "react-redux": "^7.2.1",
"react-router": "^5.0.1",
"react-router-dom": "^5.0.1",
- "react-scripts": "3.1.1"
+ "react-scripts": "3.1.1",
+ "redux": "^4.0.5",
+ "redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
diff --git a/src/components/Todo/Todo.js b/src/components/Todo/Todo.js
index 9c80927..22b5e28 100644
--- a/src/components/Todo/Todo.js
+++ b/src/components/Todo/Todo.js
@@ -8,10 +8,12 @@ const Todo = (props) => {
+ onClick={props.clickDetail}>
{props.title}
{props.done &&
✓
}
+
+
);
};
diff --git a/src/containers/TodoList/NewTodo/NewTodo.js b/src/containers/TodoList/NewTodo/NewTodo.js
index 59e7930..01a1aa3 100644
--- a/src/containers/TodoList/NewTodo/NewTodo.js
+++ b/src/containers/TodoList/NewTodo/NewTodo.js
@@ -1,6 +1,10 @@
import React, { Component } from 'react';
+import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom';
+import * as actionTypes from '../../../store/actions/actionTypes';
+import * as actionCreators from '../../../store/actions/index';
+//all of them are inside actionTypes
import './NewTodo.css';
@@ -8,23 +12,13 @@ class NewTodo extends Component {
state = {
title: '',
content: '',
- submitted: false
}
postTodoHandler = () => {
- const data =
- { title: this.state.title, content: this.state.content }
- alert('submitted' + data.title);
- // this.props.history.push('/todos');
- this.props.history.goBack();
- this.setState({ submitted: true });
+ this.props.onStoreTodo(this.state.title, this.state.content);
}
render() {
- let redirect = null;
- if (this.state.submitted) {
- redirect =
- }
return (
Add a New Todo!
@@ -36,7 +30,7 @@ class NewTodo extends Component {
>
@@ -45,4 +39,11 @@ class NewTodo extends Component {
}
}
-export default NewTodo;
\ No newline at end of file
+const mapDispatchToProps = dispatch => {
+ return {
+ onStoreTodo: (title, content) =>
+ dispatch(actionCreators.postTodo({ title: title, content: content })),
+ }
+};
+
+export default connect(null, mapDispatchToProps)(NewTodo);
diff --git a/src/containers/TodoList/RealDetail/RealDetail.js b/src/containers/TodoList/RealDetail/RealDetail.js
index 89500a3..1d87118 100644
--- a/src/containers/TodoList/RealDetail/RealDetail.js
+++ b/src/containers/TodoList/RealDetail/RealDetail.js
@@ -1,9 +1,22 @@
import React, { Component } from 'react';
-
+import * as actionCreators from '../../../store/actions/index';
import './RealDetail.css';
+import { connect } from 'react-redux';
+import * as actionTypes from '../../../store/actions/actionTypes';
+
class RealDetail extends Component {
+
+ componentDidMount() {
+ this.props.onGetTodo(parseInt(this.props.match.params.id));
+ }
+
render() {
+ let content = '', title='';
+ if (this.props.selectedTodo) {
+ title = this.props.selectedTodo.title;
+ content = this.props.selectedTodo.content;
+ }
return (
@@ -11,6 +24,7 @@ class RealDetail extends Component {
Name:
+ {title}
@@ -18,6 +32,7 @@ class RealDetail extends Component {
Content:
+ {content}
@@ -25,4 +40,16 @@ class RealDetail extends Component {
}
};
-export default RealDetail;
\ No newline at end of file
+const mapStateToProps = state => {
+ return {
+ selectedTodo: state.td.selectedTodo,
+ };
+};
+
+const mapDispatchToProps = dispatch => {
+ return {
+ onGetTodo: id =>
+ dispatch(actionCreators.getTodo(id)),
+ }
+}
+export default connect(mapStateToProps, mapDispatchToProps)(RealDetail);
diff --git a/src/containers/TodoList/TodoList.js b/src/containers/TodoList/TodoList.js
index 6cb72c1..6a52a36 100644
--- a/src/containers/TodoList/TodoList.js
+++ b/src/containers/TodoList/TodoList.js
@@ -1,7 +1,96 @@
+
import React, { Component } from 'react';
+import axios from 'axios';
+import Todo from '../../components/Todo/Todo';
+import TodoDetail from '../../components/TodoDetail/TodoDetail';
+
+import { NavLink } from 'react-router-dom';
+
+import './TodoList.css';
+
+import { connect } from 'react-redux';
+import { withRouter } from 'react-router';
+import * as actionTypes from '../../store/actions/actionTypes';
+import * as actionTypes from '../../store/actions/index';
+
+class TodoList extends Component {
+ componentDidMount() {
+ this.props.onGetAll();
+ }
+
+ state = {
+ todos: [],
+ selectedTodo: null,
+ }
+
+ clickTodoHandler = (td) => {
+ this.props.history.push(this.props.match.url + '/' + td.id);
+ }
+
+ render() {
+ const todos = this.props.storedTodos.map(td => {
+ return (
+ this.clickTodoHandler(td)}
+ clickDone={() => this.props.onToggleTodo(td.id)}
+ clickDelete={() => this.props.onDeleteTodo(td.id)}
+ />
+ );
+ });
+
+ let todo = null;
+ /*
+ if (this.state.selectedTodo) {
+ todo =
+ }
+ */
+ return (
+
+
+ {this.props.title}
+
+
+ {todos}
+
+ {todo}
+
New Todo
+
+ )
+ }
+}
+const mapStateToProps = state => {
+ return {
+ storedTodos: state.td.todos,
+ };
+}
+
+const mapDispatchToProps = dispatch => {
+ return {
+ onToggleTodo: (id) =>
+ dispatch(actionCreators.toggleTodo(id)),
+ onDeleteTodo: (id) =>
+ dispatch(actionCreators.deleteTodo(id)),
+ onGetAll: () =>
+ dispatch(actionCreators.getTodos())
+ }
+}
+
+export default connect(mapStateToProps, mapDispatchToProps)(withRouter(TodoList));
+
+/*
+import React, { Component } from 'react';
+import { connect } from 'react-redux';
+import {withRouter} from 'react-router';
import Todo from '../../components/Todo/Todo';
import TodoDetail from '../../components/TodoDetail/TodoDetail';
+import * as actionTypes from '../../store/actions/actionTypes';
import { NavLink } from 'react-router-dom';
@@ -18,30 +107,29 @@ class TodoList extends Component {
}
clickTodoHandler = (td) => {
- if (this.state.selectedTodo === td) {
- this.setState({ ...this.state, selectedTodo: null });
- } else {
- this.setState({ ...this.state, selectedTodo: td });
- }
+ this.props.history.push(`/todos/${td.id}`)
}
render() {
- const todos = this.state.todos.map(td => {
+ const todos = this.props.storedTodos.map(td => {
return (
this.clickTodoHandler(td)}
+ clickDetail={() => this.clickTodoHandler(td)}
+ clickDone={() => this.props.onToggleTodo(td.id)}
+ clickDelete={() => this.props.onDeleteTodo(td.id)}
/>
);
});
let todo = null;
- if (this.state.selectedTodo) {
+ if (this.props.selectedTodo) {
todo =
}
return (
@@ -59,4 +147,22 @@ class TodoList extends Component {
}
}
-export default TodoList;
\ No newline at end of file
+//func bringing info (first param)
+const mapStateToProps = state => { //global state
+ return {
+ storedTodos: state.td.todos,
+ selectedTodo: state.td.selectedTodo,
+ //td acts like a namespace
+ };
+}
+
+const mapDispatchToProps = dispatch => {
+ return {
+ onToggleTodo: (id) => dispatch({ type: actionTypes.TOGGLE_DONE, targetID: id }),
+ onDeleteTodo: (id) => dispatch({ type: actionTypes.DELETE_TODO, targetID: id }),
+ }
+}
+
+
+export default connect(mapStateToProps, mapDispatchToProps)(withRouter(TodoList));
+*/
\ No newline at end of file
diff --git a/src/index.js b/src/index.js
index 87d1be5..96dcbcb 100644
--- a/src/index.js
+++ b/src/index.js
@@ -3,8 +3,29 @@ import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
+import { Provider } from 'react-redux';
+import thunk from 'redux-thunk';
+import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
+//import thunk from 'redux-thunk';
+//import { connectRouter, routerMiddleware } from 'connected-react-router';
+import { createBrowserHistory } from 'history';
+import todoReducer from './store/reducers/todo';
-ReactDOM.render(, document.getElementById('root'));
+const rootReducer = combineReducers({
+ td: todoReducer, //td: used as name space
+ });
+
+
+const store = createStore(rootReducer,applyMiddleware(thunk));
+
+
+
+ReactDOM.render(
+
+
+ ,
+ document.getElementById('root')
+);
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
diff --git a/src/store/actions/actionTypes.js b/src/store/actions/actionTypes.js
new file mode 100644
index 0000000..cdeee47
--- /dev/null
+++ b/src/store/actions/actionTypes.js
@@ -0,0 +1,5 @@
+export const GET_ALL = 'GET_ALL';
+export const ADD_TODO = 'ADD_TODO';
+export const TOGGLE_DONE = 'TOGGLE_DONE';
+export const DELETE_TODO = 'DELETE_TODO';
+export const GET_TODO = 'GET_TODO';
\ No newline at end of file
diff --git a/src/store/actions/index.js b/src/store/actions/index.js
new file mode 100644
index 0000000..f0d56e1
--- /dev/null
+++ b/src/store/actions/index.js
@@ -0,0 +1 @@
+export {getTodos, postTodo, deleteTodo, toggleTodo,getTodo} from './todo';
\ No newline at end of file
diff --git a/src/store/actions/todo.js b/src/store/actions/todo.js
new file mode 100644
index 0000000..4e02f88
--- /dev/null
+++ b/src/store/actions/todo.js
@@ -0,0 +1,72 @@
+import * as actionTypes from './actionTypes';
+import axios from 'axios';
+
+export const getTodos_ = (todos) => {
+ return { type: actionTypes.GET_ALL, todos: todos };
+ };
+
+export const getTodos = () => {
+ return dispatch => {
+ return axios.get('/api/todo/')
+ .then(res => dispatch(getTodos_(res.data)));
+ };
+};
+
+export const getTodo_ = (todo) => {
+ return { type: actionTypes.GET_TODO, target: todo };
+ };
+
+ export const getTodo = (id) => {
+ return dispatch => {
+ return axios.get('/api/todo/' + id)
+ .then(res => {
+ dispatch(getTodo_(res.data))
+ });
+ };
+ };
+
+export const postTodo_ = (td) => {
+ return {
+ type: actionTypes.ADD_TODO,
+ id: td.id,
+ title: td.title,
+ content: td.content,
+ };
+ };
+
+export const postTodo = (td) => {
+ return (dispatch) => {
+ return axios.post('/api/todo/', td)
+ .then(res => {
+ dispatch(postTodo_(res.data));
+ });
+ };
+ };
+
+ export const deleteTodo_ = (id) => {
+ return {
+ type: actionTypes.DELETE_TODO,
+ targetID: id
+ };
+ };
+
+ export const deleteTodo = (id) => {
+ return dispatch => {
+ return axios.delete('/api/todo/' + id)
+ .then(res => dispatch(deleteTodo_(id)));
+ };
+ };
+
+ export const toggleTodo_ = (id) => {
+ return {
+ type: actionTypes.TOGGLE_DONE,
+ targetID: id
+ };
+ };
+
+ export const toggleTodo = (id) => {
+ return dispatch => {
+ return axios.put('/api/todo/' + id)
+ .then(res => dispatch(toggleTodo_(id)));
+ };
+ };
\ No newline at end of file
diff --git a/src/store/reducers/todo.js b/src/store/reducers/todo.js
new file mode 100644
index 0000000..8384001
--- /dev/null
+++ b/src/store/reducers/todo.js
@@ -0,0 +1,48 @@
+import * as actionTypes from '../actions/actionTypes';
+
+const initialState = {
+ todos: [
+ { id: 1, title: 'SWPP', content: 'take swpp class', done: true },
+ { id: 2, title: 'Movie', content: 'watch movie', done: false },
+ { id: 3, title: 'Dinner', content: 'eat dinner', done: false }
+ ],
+ selectedTodo: null,
+};
+
+const reducer = (state = initialState, action) => {
+ switch (action.type) {
+ case actionTypes.ADD_TODO:
+ const newTodo = {
+ id: action.id,
+ title: action.title,
+ content: action.content,
+ done: action.done,
+ };
+ return { ...state, todos: state.todos.concat(newTodo) };
+ // concat has the same meaning
+ case actionTypes.TOGGLE_DONE:
+ const modified = state.todos.map((td) => {
+ if (td.id === action.targetID) {
+ return { ...td, done: !td.done };
+ } else {
+ return { ...td };
+ }
+ });
+ return { ...state, todos: modified };
+ case actionTypes.DELETE_TODO:
+ const deletedTodos = state.todos.filter((todo) => {
+ return todo.id !== action.targetID;
+ });
+ return { ...state, todos: deletedTodos };
+ case actionTypes.GET_TODO:
+ return { ...state, selectedTodo: action.target };
+ case actionTypes.GET_ALL:
+ return { ...state, todos: action.todos };
+ default:
+ break;
+ }
+ return state;
+};
+
+export default reducer;
+//you can import this name as any name you want in other js files. (cuz it's exported as default)
\ No newline at end of file
diff --git a/yarn.lock b/yarn.lock
index 54cccc1..685e32e 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -770,6 +770,13 @@
dependencies:
regenerator-runtime "^0.13.2"
+"@babel/runtime@^7.5.5":
+ version "7.11.2"
+ resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.11.2.tgz#f549c13c754cc40b87644b9fa9f09a6a95fe0736"
+ integrity sha512-TeWkU52so0mPtDcaCTxNBI/IHiz0pZgr8VEFqXFtZWpYD08ZB6FaSwVAS8MKRQAP3bYKiVjwysOJgMFY28o6Tw==
+ dependencies:
+ regenerator-runtime "^0.13.4"
+
"@babel/template@^7.1.0", "@babel/template@^7.4.0", "@babel/template@^7.4.4":
version "7.4.4"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.4.4.tgz#f4b88d1225689a08f5bc3a17483545be9e4ed237"
@@ -4429,6 +4436,13 @@ hoist-non-react-statics@^3.1.0:
dependencies:
react-is "^16.7.0"
+hoist-non-react-statics@^3.3.0:
+ version "3.3.2"
+ resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
+ integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
+ dependencies:
+ react-is "^16.7.0"
+
hosted-git-info@^2.1.4:
version "2.8.4"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.4.tgz#44119abaf4bc64692a16ace34700fed9c03e2546"
@@ -7944,6 +7958,22 @@ react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.1, react-is@^16.8.4:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.9.0.tgz#21ca9561399aad0ff1a7701c01683e8ca981edcb"
integrity sha512-tJBzzzIgnnRfEm046qRcURvwQnZVXmuCbscxUO5RWrGTXpon2d4c8mI0D8WE6ydVIm29JiLB6+RslkIvym9Rjw==
+react-is@^16.9.0:
+ version "16.13.1"
+ resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
+ integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
+
+react-redux@^7.2.1:
+ version "7.2.1"
+ resolved "https://registry.yarnpkg.com/react-redux/-/react-redux-7.2.1.tgz#8dedf784901014db2feca1ab633864dee68ad985"
+ integrity sha512-T+VfD/bvgGTUA74iW9d2i5THrDQWbweXP0AVNI8tNd1Rk5ch1rnMiJkDD67ejw7YBKM4+REvcvqRuWJb7BLuEg==
+ dependencies:
+ "@babel/runtime" "^7.5.5"
+ hoist-non-react-statics "^3.3.0"
+ loose-envify "^1.4.0"
+ prop-types "^15.7.2"
+ react-is "^16.9.0"
+
react-router-dom@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/react-router-dom/-/react-router-dom-5.0.1.tgz#ee66f4a5d18b6089c361958e443489d6bab714be"
@@ -8122,6 +8152,19 @@ recursive-readdir@2.2.2:
dependencies:
minimatch "3.0.4"
+redux-thunk@^2.3.0:
+ version "2.3.0"
+ resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622"
+ integrity sha512-km6dclyFnmcvxhAcrQV2AkZmPQjzPDjgVlQtR0EQjxZPyJ0BnMf3in1ryuR8A2qU0HldVRfxYXbFSKlI3N7Slw==
+
+redux@^4.0.5:
+ version "4.0.5"
+ resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.5.tgz#4db5de5816e17891de8a80c424232d06f051d93f"
+ integrity sha512-VSz1uMAH24DM6MF72vcojpYPtrTUu3ByVWfPL1nPfVRb5mZVTve5GnNCUV53QM/BZ66xfWrm0CTWoM+Xlz8V1w==
+ dependencies:
+ loose-envify "^1.4.0"
+ symbol-observable "^1.2.0"
+
regenerate-unicode-properties@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/regenerate-unicode-properties/-/regenerate-unicode-properties-8.1.0.tgz#ef51e0f0ea4ad424b77bf7cb41f3e015c70a3f0e"
@@ -8144,6 +8187,11 @@ regenerator-runtime@^0.11.0:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
integrity sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==
+regenerator-runtime@^0.13.4:
+ version "0.13.7"
+ resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.7.tgz#cac2dacc8a1ea675feaabaeb8ae833898ae46f55"
+ integrity sha512-a54FxoJDIr27pgf7IgeQGxmqUNYrcV338lf/6gH456HZ/PhX+5BcwHXG9ajESmwe6WRO0tAzRUrRmNONWgkrew==
+
regenerator-transform@^0.14.0:
version "0.14.1"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.14.1.tgz#3b2fce4e1ab7732c08f665dfdb314749c7ddd2fb"
@@ -9115,6 +9163,11 @@ svgo@^1.0.0, svgo@^1.2.2:
unquote "~1.1.1"
util.promisify "~1.0.0"
+symbol-observable@^1.2.0:
+ version "1.2.0"
+ resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"
+ integrity sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==
+
symbol-tree@^3.2.2:
version "3.2.4"
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2"