Skip to content

Lab 4 #47

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 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
2 changes: 1 addition & 1 deletion backend/todo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

urlpatterns = [
path('', views.index),
path('<int:id>', views.index),
path('<int:id>/', views.index),
]
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
"name": "swppfront",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:8000",
"dependencies": {
"axios": "^0.20.0",
"connected-react-router": "^6.8.0",
"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",
Expand Down
23 changes: 23 additions & 0 deletions redux-basics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const redux = require('redux');
const createStore = redux.createStore;
const initialState = { number: 0 };

const reducer = (state = initialState, action) => {
if (action.type == 'ADD') { return ({ ...state, number: state.number + 1}); }
else if (action.type == 'ADD_VALUE') {
return ({
...state, number: state.number + action.value
});
}
return state;
}

const store = createStore(reducer);
store.subscribe(() => {
console.log('[Subscription]', store.getState());
});

store.dispatch({ type: 'ADD' });
store.dispatch({ type: 'ADD_VALUE', value: 5 });

console.log(store.getState());
7 changes: 4 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import RealDetail from './containers/TodoList/RealDetail/RealDetail';
import NewTodo from './containers/TodoList/NewTodo/NewTodo';

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

function App() {
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,7 +20,7 @@ function App() {
<Route render={() => <h1>Not Found</h1>} />
</Switch>
</div >
</BrowserRouter>
</ConnectedRouter>
);
}

Expand Down
7 changes: 4 additions & 3 deletions src/components/Todo/Todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ 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>}
<button onClick={props.clickDone}>{(props.done) ? 'Undone' : 'Done'}</button>
<button onClick={props.clickDelete}>Delete</button>
</div>
);
};

export default Todo;
export default Todo;
21 changes: 13 additions & 8 deletions src/containers/TodoList/NewTodo/NewTodo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { Component } from 'react';

import { Redirect } from 'react-router-dom';
import { connect } from 'react-redux';
import * as actionCreators from '../../../store/actions/index';

import './NewTodo.css';

Expand All @@ -12,18 +14,14 @@ 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() {
let redirect = null;
if (this.state.submitted) {
redirect = <Redirect to='/todos' />
return redirect;
}
return (
<div className="NewTodo">
Expand All @@ -36,7 +34,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 +43,11 @@ 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);
34 changes: 29 additions & 5 deletions src/containers/TodoList/RealDetail/RealDetail.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
import React, { Component } from 'react';

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

import { connect } from 'react-redux';
import './RealDetail.css';

class RealDetail extends Component {
componentDidMount() {
// it’s safe to get state after component mounted
this.props.onGetTodo(this.props.match.params.id);
}

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

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);
40 changes: 33 additions & 7 deletions src/containers/TodoList/TodoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,44 @@ import React, { Component } from 'react';

import Todo from '../../components/Todo/Todo';
import TodoDetail from '../../components/TodoDetail/TodoDetail';
import * as actionTypes from '../../store/actions/actionTypes';
import * as actionCreators from '../../store/actions/index';

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

import './TodoList.css';

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.props.history.push('/todos/' + td.id);
this.setState({ ...this.state, selectedTodo: td });
}
}

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)}
/>
);
});
Expand All @@ -44,6 +51,7 @@ class TodoList extends Component {
content={this.state.selectedTodo.content}
/>
}

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

export default TodoList;
const mapDispatchToProps = dispatch => {
return {
onDeleteTodo: (id) =>
dispatch(actionCreators.deleteTodo(id)),

onGetAll: () => dispatch(actionCreators.getTodos()),

onToggleTodo: (id) =>
dispatch(actionCreators.toggleTodo(id)),
};
};

const mapStateToProps = state => {
return {
storedTodos: state.td.todos
};
};

export default connect(mapStateToProps, mapDispatchToProps)(withRouter(TodoList));
22 changes: 21 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@ import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
import { applyMiddleware } from 'redux';
import thunk from 'redux-thunk';

import { Provider } from 'react-redux';
import { createStore, combineReducers } from 'redux';

import todoReducer from './store/reducers/todo';

import { connectRouter, routerMiddleware } from 'connected-react-router';
import { createBrowserHistory } from 'history';

const history = createBrowserHistory();
const rootReducer = combineReducers({
td: todoReducer, router: connectRouter(history)
});

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

ReactDOM.render(<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
6 changes: 6 additions & 0 deletions src/store/actions/actionTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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';

4 changes: 4 additions & 0 deletions src/store/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
getTodos, postTodo, deleteTodo,
toggleTodo, getTodo
} from './todo';
Loading