Skip to content

finished #56

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

Open
wants to merge 1 commit 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
13 changes: 7 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React from 'react';
import './App.css';

import TodoList from './containers/TodoList/TodoList';
import RealDetail from './containers/TodoList/RealDetail/RealDetail';
import NewTodo from './containers/TodoList/NewTodo/NewTodo';

import { BrowserRouter, Route, Redirect, Switch } from 'react-router-dom';

function App() {
import { ConnectedRouter } from 'connected-react-router';

function App(props) {
return (
<BrowserRouter>
<ConnectedRouter history = {props.history}>
<div className="App" >
<Switch>
<Route path='/todos' exact render={() => <TodoList title="My TODOs!" />} />
Expand All @@ -19,8 +20,8 @@ function App() {
<Route render={() => <h1>Not Found</h1>} />
</Switch>
</div >
</BrowserRouter>
</ConnectedRouter>
);
}

export default App;
export default App;
7 changes: 4 additions & 3 deletions src/components/Todo/Todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const Todo = (props) => {
return (
<div className="Todo">
<div
className={`text ${props.done && 'done'}`}
onClick={props.clicked}>
className={`text ${props.done && 'done'}`} onClick={props.clickDetail}>
{props.title}
</div>
{props.done && <div className="done-mark">&#x2713;</div>}
{props.done && (<div className="done-mark">&#x2713;</div>)}
<button onClick={props.clickDone}>{(props.done) ? 'Undone' : 'Done'}</button>
<button onClick={props.clickDelete}>Delete</button>
</div>
);
};
Expand Down
1 change: 0 additions & 1 deletion src/components/TodoDetail/TodoDetail.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';

import './TodoDetail.css';

Expand Down
27 changes: 18 additions & 9 deletions src/containers/TodoList/NewTodo/NewTodo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React, { Component } from 'react';
//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';

import './NewTodo.css';

class NewTodo extends Component {
Expand All @@ -12,12 +16,9 @@ class NewTodo extends Component {
}

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() {
Expand All @@ -36,7 +37,7 @@ class NewTodo extends Component {
></input>
<label>Content</label>
<textarea rows="4" type="text" value={this.state.content}
onChange={(event) => this.setState({ content: event.target.content })}
onChange={(event) => this.setState({ content: event.target.value })}
>
</textarea>
<button onClick={() => this.postTodoHandler()}>Submit</button>
Expand All @@ -45,4 +46,12 @@ class NewTodo extends Component {
}
}

export default NewTodo;
const mapDispatchToProps = dispatch => {
return {
onStoreTodo: (title, content) =>
dispatch(actionCreators.postTodo({title: title, content: content})),
};
};


export default connect(null, mapDispatchToProps)(NewTodo);
29 changes: 28 additions & 1 deletion src/containers/TodoList/RealDetail/RealDetail.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as actionTypes from '../../../store/actions/actionTypes';

import './RealDetail.css';
import * as actionCreators from '../../../store/actions/index';

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 (
<div className="RealDetail" >
<div className="row">
<div className="left">
Name:
</div>
<div className="right">
{title}
</div>
</div>
<div className="row">
<div className="left">
Content:
</div>
<div className="right">
{content}
</div>
</div>
</div>
);
}
};

export default RealDetail;
export default RealDetail;
const mapStateToProps = state => {
return {
selectedTodo: state.td.selectedTodo,
};
};

const mapDispatchToProps = dispatch => {
return {
onGetTodo: id =>
dispatch(actionCreators.getTodo(id)),
};
};

export default connect(mapStateToProps, mapDispatchToProps)(RealDetail);
58 changes: 31 additions & 27 deletions src/containers/TodoList/TodoList.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,36 @@
import React, { Component } from 'react';

import Todo from '../../components/Todo/Todo';
import TodoDetail from '../../components/TodoDetail/TodoDetail';
import './TodoList.css';

import { NavLink } from 'react-router-dom';
import { withRouter } from 'react-router';
import { connect } from 'react-redux';

import './TodoList.css';
import * as actionTypes from '../../store/actions/actionTypes';
import * as actionCreators from '../../store/actions/index';

class TodoList extends Component {
state = {
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,
componentDidMount() {
this.props.onGetAll();
}

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 (
<Todo
key={td.id}
title={td.title}
done={td.done}
clicked={() => 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) {
todo = <TodoDetail
title={this.state.selectedTodo.title}
content={this.state.selectedTodo.content}
/>
}

return (
<div className="TodoList">
<div className='title'>
Expand All @@ -59,4 +46,21 @@ class TodoList extends Component {
}
}

export default TodoList;
const mapStateToProps = state => {
return {
storedTodos: state.td.todos,
selectedTodo : state.td.selectedTodo
};
};

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));
22 changes: 18 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
import {Provider} from 'react-redux';
import {combineReducers, createStore} from 'redux';
import { connectRouter, routerMiddleware } from 'connected-react-router' ;
import { createBrowserHistory } from 'history' ;
import { applyMiddleware } from 'redux';


import thunk from 'redux-thunk';
import todoReducer from './store/reducers/todo';
const history = createBrowserHistory();
const rootReducer = combineReducers({
td: todoReducer, router: connectRouter(history)
});

const store = createStore(rootReducer, applyMiddleware(thunk, routerMiddleware(history)));


<Provider store={store}> <App history={history} /> </Provider>,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.
Expand Down
5 changes: 5 additions & 0 deletions src/store/actions/actionsTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const GET_ALL = 'GET_ALL';
export const GET_TODO = 'GET_TODO';
export const TOGGLE_DONE = 'TOGGLE_DONE';
export const DELETE_TODO = 'DELETE_TODO';
export const ADD_TODO = 'ADD_TODO';
1 change: 1 addition & 0 deletions src/store/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {getTodos, postTodo, deleteTodo, toggleTodo, getTodo} from './todo'
73 changes: 73 additions & 0 deletions src/store/actions/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as actionTypes from './actionTypes';
import { push } from 'connected-react-router';
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 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)))
.then(() => dispatch(push('/todos/')))
};
};

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));
});
};
};

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));
});
};
};
Loading