Skip to content

Commit

Permalink
refactor into ducks
Browse files Browse the repository at this point in the history
  • Loading branch information
Createdd committed Jun 19, 2017
1 parent 5c7e7dd commit 4e7b107
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Provider } from 'react-redux';
import { createStore } from 'redux';

import Scoreboard from './src/containers/Scoreboard';
import QuestionReducer from './src/reducers/question';
import QuestionReducer from './src/ducks/question';

const store = createStore(
QuestionReducer,
Expand Down
22 changes: 0 additions & 22 deletions src/actions/question.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/actiontypes/question.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/containers/Scoreboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import AddQuestionForm from '../components/AddQuestionForm';
import Header from '../components/Header';
import Question from '../components/Question';
import QuestionDetail from '../components/QuestionDetail';
import * as QuestionActionCreators from '../actions/question';
import * as QuestionActionCreators from '../ducks/question';

class Scoreboard extends React.Component {
static PropTypes = {
Expand Down
36 changes: 29 additions & 7 deletions src/reducers/question.js → src/ducks/question.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as QuestionActionTypes from '../actiontypes/question';
// Actions
const ADD_QUESTION = 'question/ADD_QUESTION';
const REMOVE_QUESTION = 'question/REMOVE_QUESTION';
const UPDATE_QUESTION_SCORE = 'question/UPDATE_QUESTION_SCORE';
const SELECT_QUESTION = 'question/SELECT_QUESTION';

// Reducers
const initialState = {
questions: [
{
Expand Down Expand Up @@ -27,7 +32,7 @@ const initialState = {
export default function Question(state = initialState, action) {
const date = `${new Date().getHours()}:00`;
switch (action.type) {
case QuestionActionTypes.ADD_QUESTION:
case ADD_QUESTION:
const addQuestionList = [
...state.questions,
{
Expand All @@ -40,7 +45,7 @@ export default function Question(state = initialState, action) {
...state,
questions: addQuestionList,
};
case QuestionActionTypes.REMOVE_QUESTION:
case REMOVE_QUESTION:
const removeQuestionList = [
...state.questions.slice(0, action.index),
...state.questions.slice(action.index + 1),
Expand All @@ -49,8 +54,7 @@ export default function Question(state = initialState, action) {
...state,
questions: removeQuestionList,
};

case QuestionActionTypes.UPDATE_QUESTION_SCORE:
case UPDATE_QUESTION_SCORE:
const updateQuestionList = state.questions.map((question, index) => {
if (index === action.index) {
return {
Expand All @@ -65,13 +69,31 @@ export default function Question(state = initialState, action) {
...state,
questions: updateQuestionList,
};
case QuestionActionTypes.SELECT_QUESTION:
case SELECT_QUESTION:
return {
...state,
selectedQuestionIndex: action.index,
};

default:
return state;
}
}

// ActionCreators
export const addQuestion = name => ({
type: ADD_QUESTION,
name,
});
export const removeQuestion = index => ({
type: REMOVE_QUESTION,
index,
});
export const updateQuestionScore = (index, score) => ({
type: UPDATE_QUESTION_SCORE,
index,
score,
});
export const selectQuestion = index => ({
type: SELECT_QUESTION,
index,
});

0 comments on commit 4e7b107

Please sign in to comment.