Skip to content

Redux Tutorial #51

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
5 changes: 5 additions & 0 deletions .idea/.gitignore

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

6 changes: 6 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

12 changes: 12 additions & 0 deletions .idea/swpp-redux-tutorial.iml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

Binary file modified backend/db.sqlite3
Binary file not shown.
13,967 changes: 13,967 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

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
33 changes: 17 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ 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';
import { Route, Redirect, Switch } from 'react-router-dom';
import { ConnectedRouter } from 'connected-react-router';

function App() {
return (
<BrowserRouter>
<div className="App" >
<Switch>
<Route path='/todos' exact render={() => <TodoList title="My TODOs!" />} />
<Route path='/todos/:id' exact component={RealDetail} />
<Route path='/new-todo' exact component={NewTodo} />
<Redirect exact from='/' to='todos' />
<Route render={() => <h1>Not Found</h1>} />
</Switch>
</div >
</BrowserRouter>
);
function App(props) {
return (
<ConnectedRouter history={props.history}>
<div className="App" >
<Switch>
<Route path='/todos' exact render={() => <TodoList title="My TODOs!" />} />
<Route path='/todos/:id' exact component={RealDetail} />
<Route path='/new-todo' exact component={NewTodo} />
<Redirect exact from='/' to='todos' />
<Route render={() => <h1>Not Found</h1>} />
</Switch>
</div >
</ConnectedRouter>
);
}

export default App;
export default App;
22 changes: 12 additions & 10 deletions src/components/Todo/Todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import './Todo.css';

const Todo = (props) => {

return (
<div className="Todo">
<div
className={`text ${props.done && 'done'}`}
onClick={props.clicked}>
{props.title}
</div>
{props.done && <div className="done-mark">&#x2713;</div>}
</div>
);
return (
<div className="Todo">
<div
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;
44 changes: 22 additions & 22 deletions src/components/TodoDetail/TodoDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ import React from 'react';
import './TodoDetail.css';

class TodoDetail extends React.Component {
render() {
return (
<div className="TodoDetail" >
<div className="row">
<div className="left">
Name:
</div>
<div className="right">
{this.props.title}
</div>
</div>
<div className="row">
<div className="left">
Content:
</div>
<div className="right">
{this.props.content}
</div>
</div>
</div>
);
}
render() {
return (
<div className="TodoDetail" >
<div className="row">
<div className="left">
Name:
</div>
<div className="right">
{this.props.title}
</div>
</div>
<div className="row">
<div className="left">
Content:
</div>
<div className="right">
{this.props.content}
</div>
</div>
</div>
);
}
};

export default TodoDetail;
75 changes: 37 additions & 38 deletions src/containers/TodoList/NewTodo/NewTodo.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
import React, { Component } from 'react';

import { Redirect } from 'react-router-dom';

import './NewTodo.css';

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

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

render() {
let redirect = null;
if (this.state.submitted) {
redirect = <Redirect to='/todos' />
state = {
title: '',
content: '',
}

postTodoHandler = () => {
this.props.onStoreTodo(this.state.title, this.state.content);
}
return (
<div className="NewTodo">
<h1>Add a New Todo!</h1>
<label>Title</label>
<input
type="text"
value={this.state.title}
onChange={(event) => this.setState({ title: event.target.value })}
></input>
<label>Content</label>
<textarea rows="4" type="text" value={this.state.content}
onChange={(event) => this.setState({ content: event.target.content })}
>

render() {
return (
<div className="NewTodo">
<h1>Add a New Todo!</h1>
<label>Title</label>
<input
type="text"
value={this.state.title}
onChange={(event) => this.setState({ title: event.target.value })}
></input>
<label>Content</label>
<textarea rows="4" type="text" value={this.state.content}
onChange={(event) => this.setState({ content: event.target.value })}
>
</textarea>
<button onClick={() => this.postTodoHandler()}>Submit</button>
</div>
);
}
<button onClick={() => this.postTodoHandler()}>Submit</button>
</div>
);
}
}

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

export default connect(null, mapDispatchToProps)(NewTodo);
71 changes: 50 additions & 21 deletions src/containers/TodoList/RealDetail/RealDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,56 @@ import React, { Component } from 'react';

import './RealDetail.css';

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

class RealDetail extends Component {
render() {
return (
<div className="RealDetail" >
<div className="row">
<div className="left">
Name:
</div>
<div className="right">
</div>
</div>
<div className="row">
<div className="left">
Content:
</div>
<div className="right">
</div>
</div>
</div>
);
}

componentDidMount() {
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">
{title}
</div>
</div>
<div className="row">
<div className="left">
Content:
</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);
Loading