Skip to content
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: 0 additions & 2 deletions src/actions/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {createAction} from 'redux-actions';

export const setCheckbox = createAction('SET_CHECKBOX');

export default {setCheckbox};
4 changes: 4 additions & 0 deletions src/actions/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {createAction} from 'redux-actions';

export const createTodo = createAction('CREATE_TODO');
export const toggleTodoStatus = createAction('TOGGLE_TODO_STATUS');
21 changes: 9 additions & 12 deletions src/containers/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import React, { Component } from 'react';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { setCheckbox } from '../actions/app';
import { Title } from './App.styles';
import {connect} from 'react-redux';

import Todo from './Todo';
import {setCheckbox} from '../actions/app';
import {Title} from './App.styles';

const mapStateToProps = state => ({
checked: state.app.checkbox
checked: state.app.checkbox,
});

class App extends Component {
static propTypes = {
checked: PropTypes.bool
checked: PropTypes.bool,
};

handleCheckChange = event => {
Expand All @@ -20,12 +22,7 @@ class App extends Component {
render() {
return (
<div>
<Title>Hello World</Title>
<input
type="checkbox"
checked={this.props.checked}
onChange={this.handleCheckChange}
/>
<Todo />
</div>
);
}
Expand Down
62 changes: 62 additions & 0 deletions src/containers/Todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';

import {createTodo, toggleTodoStatus} from '../actions/todo';
import {
InputWrapper,
Item,
StatusIndicatior,
SubmitButton,
} from './Todo.styles';

const mapStateToProps = state => ({
items: state.todo.items,
});

class Todo extends Component {
static propTypes = {
items: PropTypes.array,
};

state = {inputText: ''};

onInputChange = event => {
this.setState({inputText: event.target.value});
};

onInputSubmit = () => {
this.props.dispatch(
createTodo({text: this.state.inputText, status: false})
);
this.setState({inputText: ''});
};

render() {
return (
<div>
<InputWrapper>
<SubmitButton onClick={this.onInputSubmit}> Submit </SubmitButton>
<input
type="text"
value={this.state.inputText}
onChange={this.onInputChange}
/>
</InputWrapper>
{this.props.items.map(item => {
return (
<Item key={item.text}>
<StatusIndicatior
complete={item.status}
onClick={() => this.props.dispatch(toggleTodoStatus(item))}
/>
{item.text}
</Item>
);
})}
</div>
);
}
}

export default connect(mapStateToProps)(Todo);
26 changes: 26 additions & 0 deletions src/containers/Todo.styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import styled from 'react-emotion';

export const Item = styled.div`
display: flex;
margin-bottom: 5px;
`;

export const StatusIndicatior = styled.div`
height: 20px;
width: 20px;
background-color: ${props => (props.complete ? 'green' : 'red')};
margin-right: 5px;
cursor: pointer;
`;

export const InputWrapper = styled.div`
display: flex;
margin-bottom: 20px;
`;

export const SubmitButton = styled.div`
height: 20px;
margin-right: 10px;
background-color: grey;
border: 1px solid black;
`;
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import {Provider} from 'react-redux';
import store from './store';
import App from './containers/App';

Expand Down
3 changes: 2 additions & 1 deletion src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import appReducer from './app';
import todoReducer from './todo';
import {combineReducers} from 'redux';

export default combineReducers({app: appReducer});
export default combineReducers({app: appReducer, todo: todoReducer});
32 changes: 32 additions & 0 deletions src/reducers/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {handleActions} from 'redux-actions';
import {createTodo, toggleTodoStatus} from '../actions/todo';

const initialState = {
items: [
{
text: 'Complete this assignment',
status: false,
},
],
};

const todoReducer = handleActions(
{
[createTodo]: (state, action) => ({
...state,
items: [...state.items, action.payload],
}),
[toggleTodoStatus]: (state, action) => ({
...state,
items: state.items.map(
item =>
item.text === action.payload.text
? {text: item.text, status: !item.status}
: item
),
}),
},
initialState
);

export default todoReducer;