Skip to content
This repository has been archived by the owner on Jul 31, 2018. It is now read-only.

Commit

Permalink
assemble redux stuffs, merge actions and reducers in a single file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaijun committed Jan 15, 2016
1 parent bbb211d commit 86da82e
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 68 deletions.
9 changes: 0 additions & 9 deletions client/actions/todos.jsx

This file was deleted.

2 changes: 1 addition & 1 deletion client/containers/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import Header from '../../components/Header'
import MainSection from '../../components/MainSection'
import * as TodoActions from '../../actions/todos'
import {actions as TodoActions} from '../../redux/modules/todos'
import style from './style.css'

class App extends Component {
Expand Down
4 changes: 2 additions & 2 deletions client/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import ReactDOM from 'react-dom'
import React from 'react'

import App from './containers/App'
import configure from './store'
import configureStore from './redux/configureStore'

const store = configure()
const store = configureStore()
const history = createHistory()

syncReduxAndRouter(history, store)
Expand Down
52 changes: 0 additions & 52 deletions client/reducers/todos.jsx

This file was deleted.

6 changes: 3 additions & 3 deletions client/store/index.jsx → client/redux/configureStore.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { createStore } from 'redux'
import rootReducer from '../reducers'
import rootReducer from './rootReducer'

export default function configure(initialState) {
const create = window.devToolsExtension
Expand All @@ -10,8 +10,8 @@ export default function configure(initialState) {
const store = create(rootReducer, initialState)

if (module.hot) {
module.hot.accept('../reducers', () => {
const nextReducer = require('../reducers')
module.hot.accept('./rootReducer', () => {
const nextReducer = require('./rootReducer')
store.replaceReducer(nextReducer)
})
}
Expand Down
File renamed without changes.
77 changes: 77 additions & 0 deletions client/redux/modules/todos.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {createAction, handleActions } from 'redux-actions'

const ADD_TODO = 'add todo'
const DELETE_TODO = 'delete todo'
const EDIT_TODO = 'edit todo'
const COMPLETE_TODO = 'complete todo'
const COMPLETE_ALL = 'complete all'
const CLEAR_COMPLETE = 'clear complete'

// Actions
const addTodo = createAction(ADD_TODO)
const deleteTodo = createAction(DELETE_TODO)
const editTodo = createAction(EDIT_TODO)
const completeTodo = createAction(COMPLETE_TODO)
const completeAll = createAction(COMPLETE_ALL)
const clearCompleted = createAction(CLEAR_COMPLETE)

// Exposed Actions
export const actions = {
addTodo,
deleteTodo,
editTodo,
completeTodo,
completeAll,
clearCompleted,
}

// Reducers
const initialState = [{
text: 'Use Redux',
completed: false,
id: 0
}]

export default handleActions({
[ADD_TODO] (state, action) {
return [{
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
completed: false,
text: action.payload
}, ...state]
},

[DELETE_TODO] (state, action) {
return state.filter(todo => todo.id !== action.payload )
},

[EDIT_TODO] (state, action) {
return state.map(todo => {
return todo.id === action.payload.id
? { ...todo, text: action.payload.text }
: todo
})
},

[COMPLETE_TODO] (state, action) {
return state.map(todo => {
return todo.id === action.payload
? { ...todo, completed: !todo.completed }
: todo
})
},

[COMPLETE_ALL] (state, action) {
const areAllMarked = state.every(todo => todo.completed)
return state.map(todo => {
return {
...todo,
completed: !areAllMarked
}
})
},

[CLEAR_COMPLETE] (state, action) {
return state.filter(todo => todo.completed === false)
}
}, initialState)
2 changes: 1 addition & 1 deletion client/reducers/index.jsx → client/redux/rootReducer.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { combineReducers } from 'redux'
import { routeReducer as routing } from 'redux-simple-router'
import todos from './todos'
import todos from './modules/todos'

export default combineReducers({
routing,
Expand Down

0 comments on commit 86da82e

Please sign in to comment.