- implementing immutable.js

This commit is contained in:
2019-07-17 03:21:23 -04:00
parent 8ecf036cc4
commit 434a1ded24
39 changed files with 1123 additions and 187 deletions

View File

@@ -0,0 +1,12 @@
import { SET_ACTIVE_EVENT, UNSET_ACTIVE_EVENT } from '../constants/actionTypes.js';
export const activeEvent = (state = null, action) => {
switch (action.type) {
case SET_ACTIVE_EVENT:
return action.payload;
case UNSET_ACTIVE_EVENT:
return null;
default:
return state;
}
};

18
app/reducers/auctions.js Normal file
View File

@@ -0,0 +1,18 @@
import { Map } from 'immutable';
import { AUCTIONS_UPDATED, UPDATE_AUCTIONS } from '../constants/actionTypes.js';
export const auctions = (state = new Map(), action) => {
switch (action.type) {
case AUCTIONS_UPDATED:
return state.merge(
action.payload.toMap().mapEntries((entry) => {
const [, item] = entry;
return [`${item.id}`, item];
}),
);
case UPDATE_AUCTIONS:
default:
return state;
}
};

12
app/reducers/blockUI.js Normal file
View File

@@ -0,0 +1,12 @@
import { BLOCK_UI, UNBLOCK_UI } from '../constants/actionTypes.js';
export const blockUI = (state = false, action) => {
switch (action.type) {
case BLOCK_UI:
return true;
case UNBLOCK_UI:
return false;
default:
return state;
}
};

View File

@@ -1,16 +1,17 @@
import { Map } from 'immutable';
import { EVENTS_LOADED, GET_EVENTS } from '../constants/actionTypes.js';
export const events = (state = {}, action) => {
export const events = (state = new Map(), action) => {
switch (action.type) {
case GET_EVENTS:
return Object.assign({}, state, {
isFetching: true,
});
case EVENTS_LOADED:
return Object.assign({}, state, {
events: action.payload,
isFetching: false,
});
return state.merge(
action.payload.toMap().mapEntries((entry) => {
const [, event] = entry;
return [`${event.id}`, event];
}),
);
case GET_EVENTS:
default:
return state;
}

View File

@@ -1,17 +1,15 @@
import { combineReducers } from 'redux';
import { combineReducers } from 'redux-immutable';
import { activeEvent } from './activeEvent.js';
import { auctions } from './auctions.js';
import { blockUI } from './blockUI.js';
import { events } from './events.js';
import { items } from './items.js';
const initialState = {
auction: {},
cart: [],
isFetching: false,
items: [],
profile: {},
};
const rootReducer = combineReducers({
activeEvent,
auctions,
blockUI,
events,
items,
});

View File

@@ -1,16 +1,19 @@
import { ITEMS_LOADED, GET_ITEMS } from '../constants/actionTypes.js';
import { Map } from 'immutable';
export const items = (state = {}, action) => {
import {
GET_ITEMS,
ITEMS_LOADED,
} from '../constants/actionTypes.js';
export const items = (state = new Map(), action) => {
switch (action.type) {
case GET_ITEMS:
return Object.assign({}, state, {
isFetching: true,
});
case ITEMS_LOADED:
return Object.assign({}, state, {
items: action.payload,
isFetching: false,
const mapped = action.payload.toMap().mapEntries((entry) => {
const [, item] = entry;
return [`${item.id}`, item];
});
return state.merge(mapped);
case GET_ITEMS:
default:
return state;
}

17
app/reducers/profile.js Normal file
View File

@@ -0,0 +1,17 @@
import { Map } from 'immutable';
import {
SET_PROFILE,
UPDATE_PROFILE,
} from '../constants/actionTypes.js';
export const profile = (state = new Map(), action) => {
switch (action.type) {
case SET_PROFILE:
return action.payload;
case UPDATE_PROFILE:
return action.payload;
default:
return state;
}
};