Skip to content

Instantly share code, notes, and snippets.

View kettanaito's full-sized avatar
🚀
Extremely busy. Open source activity paused.

Artem Zakharchenko kettanaito

🚀
Extremely busy. Open source activity paused.
View GitHub Profile
@kettanaito
kettanaito / initial-state.js
Last active May 22, 2018 07:45
Redux - Selector
import { Record } from 'immutable'
const CartInitialState = new Record({
items: [],
totalPrice: 0,
})
const initialState = new CartInitialState()
export default initialState
@kettanaito
kettanaito / cart-reducer.js
Last active May 22, 2018 07:34
Redux - Selector
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')
@kettanaito
kettanaito / cart-reducer.js
Last active May 22, 2018 07:40
Redux - Selector
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: {
@kettanaito
kettanaito / initial-state.js
Created May 22, 2018 07:46
Redux - Selector
import { Record } from 'immutable'
const CartInitialState = new Record({
items: [],
})
const initialState = new CartInitialState()
export default initialState
@kettanaito
kettanaito / selectors.js
Last active May 22, 2018 08:01
Redux - Selector
// cart/selectors.js
export const getTotalPrice = (cart) => {
return cart.get('items').reduce((acc, item) => {
return acc + item.get('price')
}, 0)
}
@kettanaito
kettanaito / Cart.jsx
Created May 22, 2018 07:53
Redux - Selector
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>
@kettanaito
kettanaito / initial-state.js
Last active June 5, 2018 11:01
Redux - Immutability
import { Record, List } from 'immutable'
const CartInitialState = new Record({
items: List()
})
const initialState = CartInitialState({
items: List()
})
@kettanaito
kettanaito / Header.jsx
Last active June 18, 2018 11:39
Atomic layout - Nested composition
import React from 'react'
import { Composition } from 'atomic-layout'
import SearchBar from './SearchBar'
const templateMobile = `
'logo'
'search'
`
const Header = () => (
@kettanaito
kettanaito / ResponsiveProp.jsx
Created June 21, 2018 13:21
Atomic layout - Responsive prop
<Composition
gutter={10}
gutterMdOnly={20} />
@kettanaito
kettanaito / getIntersection.js
Created June 22, 2018 09:33
Rectangles intersection
/**
* 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