Skip to content

Instantly share code, notes, and snippets.

@imjul1an
Created February 13, 2016 21:56
Show Gist options
  • Save imjul1an/8402475d1071704dde1b to your computer and use it in GitHub Desktop.
Save imjul1an/8402475d1071704dde1b to your computer and use it in GitHub Desktop.
import * as types from '../constants';
import combineTypes from '../utils';
const item = (state, action) => {
const actions = {
[types.UPDATE_ITEM_SIZE_REQUEST]: () => ({
...state,
isUpdatingSize: true
}),
[types.UPDATE_ITEM_SIZE_SUCCESS]: () => action.payload,
[types.UPDATE_ITEM_SIZE_FAILURE]: () => ({
...state,
isUpdatingSize: false,
error: action.payload.error
}),
[types.DELETE_ITEM_REQUEST]: () => ({
...state,
isDeleting: true,
isDeletingTimerOn: true
}),
[types.DELETE_ITEM_SUCCESS]: () => ({
...state,
isDeleting: false,
isDeletingTimerOn: false,
isDeleted: true
}),
[types.DELETE_ITEM_FAILURE]: () => ({
...state,
isDeleting: true,
isDeletingTimerOn: true,
error: action.payload.error
}),
[types.ADD_ITEM_TO_CART_REQUEST]: () => ({
...state,
isUpdatingCart: true
}),
[types.ADD_ITEM_TO_CART_SUCCESS]: () => ({
...state,
isAddingToCart: false,
article: {
isOnCart: true
}
}),
[types.ADD_ITEM_TO_CART_FAILURE]: () => ({
...state,
isAddingToCart: false,
error: action.payload.error
}),
default: () => state
};
return (actions[action.type] || actions.default)();
};
const items = (state, action) => {
const itemId = action.payload.itemId;
const actions = {
[combineTypes({
by: action.type,
with: [
types.UPDATE_ITEM_SIZE_REQUEST,
types.UPDATE_ITEM_SIZE_SUCCESS,
types.UPDATE_ITEM_SIZE_FAILURE,
types.DELETE_ITEM_REQUEST,
types.DELETE_ITEM_SUCCESS,
types.DELETE_ITEM_FAILURE,
types.ADD_ITEM_TO_CART_REQUEST,
types.ADD_ITEM_TO_CART_SUCCESS,
types.ADD_ITEM_TO_CART_FAILURE
]
})]: () => ({
...state,
[itemId]: item(state[itemId], action)
}),
default: () => state
};
return (actions[action.type] || actions.default)();
};
const collectionData = (state, action) => {
const { collectionId, itemCount } = action.payload;
const actions = {
[combineTypes({
by: action.type,
with: [
types.UPDATE_ITEM_SIZE_REQUEST,
types.UPDATE_ITEM_SIZE_SUCCESS,
types.UPDATE_ITEM_SIZE_FAILURE,
types.DELETE_ITEM_REQUEST,
types.DELETE_ITEM_SUCCESS,
types.DELETE_ITEM_FAILURE,
types.ADD_ITEM_TO_CART_REQUEST,
types.ADD_ITEM_TO_CART_SUCCESS,
types.ADD_ITEM_TO_CART_FAILURE
]
})]: () => ({
...state,
items: items(state[collectionId], action)
}),
[types.SHOW_MORE_ITEMS]: () => ({
...state,
visibleItems: itemCount
}),
default: () => state
};
return (actions[action.type] || actions.default)();
};
const itemsByCollection = (state, action) => {
const { collectionId } = action.payload;
const actions = {
[combineTypes({
by: action.type,
with: [
types.SHOW_MORE_ITEMS,
types.UPDATE_ITEM_SIZE_REQUEST,
types.UPDATE_ITEM_SIZE_SUCCESS,
types.UPDATE_ITEM_SIZE_FAILURE,
types.DELETE_ITEM_REQUEST,
types.DELETE_ITEM_SUCCESS,
types.DELETE_ITEM_FAILURE,
types.ADD_ITEM_TO_CART_REQUEST,
types.ADD_ITEM_TO_CART_SUCCESS,
types.ADD_ITEM_TO_CART_FAILURE
]
})]: () => ({
...state,
[collectionId]: collectionData(state[collectionId], action)
}),
default: () => state
};
return (actions[action.type] || actions.default)();
};
export default itemsByCollection;
@imjul1an
Copy link
Author

I believe it was included to JavaScript for the same reason as Brendon Eich did it with the initial name of JavaScript which was Mocha then it was LiveScript and finally became JavaScript. It was a marketing strategy - to attract Java developers to switch to JavaScript. The same story with a new operator, var julian = new Person() - it's just about business and nothing about real philosophy of JavaScript :)

Let's be more Object'ish :)

@imjul1an
Copy link
Author

I'm not really trying to push with object-literals I'm just really upset because Flux started using it and Redux just follow them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment