This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Record } from 'immutable' | |
const CartInitialState = new Record({ | |
items: [], | |
totalPrice: 0, | |
}) | |
const initialState = new CartInitialState() | |
export default initialState |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import initialState from './initial-state' | |
import { ADD_ITEM, REMOVE_ITEM } from './action-types' | |
export default function cart(state = initialState, action) { | |
switch (action.type) { | |
case ADD_ITEM: { | |
const { item } = action | |
const nextItems = state.update('items', items => items.push(item)) | |
const nextTotalPrice = nextItems.reduce((acc, item) => { | |
return acc + item.get('price') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import initialState from './initial-state' | |
import { ADD_ITEM, REMOVE_ITEM } from './action-types' | |
const getTotalPrice = items => items.reduce((acc, item) => { | |
return acc + item.get('price') | |
}, 0) | |
export default function cart(state = initialState, action) { | |
switch (action.type) { | |
case ADD_ITEM: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Record } from 'immutable' | |
const CartInitialState = new Record({ | |
items: [], | |
}) | |
const initialState = new CartInitialState() | |
export default initialState |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cart/selectors.js | |
export const getTotalPrice = (cart) => { | |
return cart.get('items').reduce((acc, item) => { | |
return acc + item.get('price') | |
}, 0) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { connect } from 'react-redux' | |
import { getTotalPrice } from './cart/selectors' | |
class Cart extends React.Component { | |
render() { | |
const { items, totalPrice } = this.props | |
return ( | |
<div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Record, List } from 'immutable' | |
const CartInitialState = new Record({ | |
items: List() | |
}) | |
const initialState = CartInitialState({ | |
items: List() | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { Composition } from 'atomic-layout' | |
import SearchBar from './SearchBar' | |
const templateMobile = ` | |
'logo' | |
'search' | |
` | |
const Header = () => ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Composition | |
gutter={10} | |
gutterMdOnly={20} /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns the intersection area (in px) based on the intersection | |
* between two given rectangles (i.e. ClientRect). | |
*/ | |
export default function getIntersection(rectA, rectB) { | |
const intersectionX = Math.max(0, Math.min(rectA.right, rectB.right) - Math.max(rectA.left, rectB.left)) | |
const intersectionY = Math.max(0, Math.min(rectA.bottom, rectB.bottom) - Math.max(rectA.top, rectB.top)) | |
const intersectionArea = intersectionX * intersectionY | |
return intersectionArea |