Skip to content

organized structure for better understanding #11

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 7 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
3 changes: 1 addition & 2 deletions src/actions.js → src/actions/todo/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export function addTodo(text) {
return {
type: 'ADD_TODO',
Expand All @@ -11,4 +10,4 @@ export function removeTodo(todo) {
type: 'REMOVE_TODO',
todo
};
}
}
7 changes: 4 additions & 3 deletions src/components/app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { h, Component } from 'preact';
import { connect } from 'preact-redux';
import reduce from '../reducers';
import * as actions from '../actions';
import reduce from '../reducers/reducers';
import * as actions from '../actions/todo';
import TodoItem from './todo-item';

@connect(reduce, actions)

export default class App extends Component {
addTodos = () => {
this.props.addTodo(this.state.text);
Expand Down Expand Up @@ -33,4 +34,4 @@ export default class App extends Component {
</div>
);
}
}
}
2 changes: 1 addition & 1 deletion src/components/todo-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export default class TodoItem extends Component {
</li>
);
}
}
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Provider } from 'preact-redux';
import store from './store';
import store from './reducers/store';
import App from './components/app';
import './style';

Expand All @@ -9,4 +9,4 @@ export default () => (
<App />
</Provider>
</div>
);
);
4 changes: 0 additions & 4 deletions src/reducers.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/reducers/reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const EMPTY = {};

export default store => store || EMPTY;
15 changes: 15 additions & 0 deletions src/reducers/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createStore } from 'redux';
import {ADD_TODO, REMOVE_TODO} from './todo'

const ACTIONS = {
ADD_TODO,
REMOVE_TODO
}

const INITIAL = {
todos: []
};

export default createStore( (state, action) => (
action && ACTIONS[action.type] ? ACTIONS[action.type](state, action) : state
), INITIAL, typeof devToolsExtension==='function' ? devToolsExtension() : undefined);
17 changes: 17 additions & 0 deletions src/reducers/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@


const ADD_TODO = ({ todos, ...state }, { text }) => ({
todos: [...todos, {
id: Math.random().toString(36).substring(2),
text
}],
...state
})

const REMOVE_TODO = ({ todos, ...state }, { todo }) => ({
todos: todos.filter( i => i!==todo ),
...state
})


export {ADD_TODO, REMOVE_TODO }
24 changes: 0 additions & 24 deletions src/store.js

This file was deleted.