DEV Community

JaredHarbison
JaredHarbison

Posted on

3

DRAGnet Reducer

Rails + React + Redux - Pt 11


The queens_reducer.js was the first reducer I wrote for DRAGnet. It was a natural choice as the simplest.


Let's get started!


For UPDATE_QUEEN I defined idx as the id for the queen being updated by the user. In the case of UPDATE_QUEEN, the return is the data for the updated queen spliced from the complete array of drag_queens.

import { LOADING_QUEENS, FETCH_QUEENS, UPDATE_QUEEN } from '../actions/types';
import {initialState} from './initialState';
export default function queens_reducer(state = initialState, action) {
console.log(action)
let idx
let tidx
switch (action.type) {
case LOADING_QUEENS:
return Object.assign({}, state, { loading: true })
case FETCH_QUEENS:
return { loading: false, drag_queens: action.payload }
case UPDATE_QUEEN:
idx = state.drag_queens.indexOf(state.drag_queens.find(q => q.id == action.payload.queen_id))
return {...state,
drag_queens: [...state.drag_queens.slice(0, idx),
{...state.drag_queens[idx]},
...state.drag_queens.slice(idx)]
}
default:
return state
}
}

That's all folks!

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay