Skip to content

Commit

Permalink
Commonize create/update actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslansavenok committed Mar 23, 2018
1 parent 3b56d06 commit 989a841
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 83 deletions.
13 changes: 6 additions & 7 deletions assets/js/react/containers/pages/All.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import socket from 'socket'
import { joinChannel as joinCategoriesChannel } from '~/store/actions/categories'
import { joinChannel as joinProductsChannel } from '~/store/actions/products'
import * as allActions from '~/store/actions/products/all'
import * as productActions from '~/store/actions/products'
import { ProductFormModal } from '~/components/modals'
import SearchContainer from '../common/SearchContainer'
import ProductsTableContainer from '../common/ProductsTableContainer'
Expand All @@ -16,7 +15,7 @@ class All extends PureComponent {
super(props)
socket.connect()
props.joinCategoriesChannel()
props.joinProductsChannel('all')
props.productActions.joinChannel('all')
}

state = {
Expand Down Expand Up @@ -86,8 +85,8 @@ class All extends PureComponent {
}
onSubmit={attrs => (
this.state.modalProduct === MODAL_PRODUCT_NEW
? this.props.allActions.create(attrs)
: this.props.allActions.update(this.state.modalProduct.id, attrs)
? this.props.productActions.create(attrs)
: this.props.productActions.update(this.state.modalProduct.id, attrs)
)}
show={this.state.modalProduct !== false}
onHide={() => {
Expand All @@ -107,8 +106,8 @@ const mapStateToProps = state => ({

const mapDispatchToProps = dispatch => {
return {
...bindActionCreators({ joinProductsChannel, joinCategoriesChannel }, dispatch),
allActions: bindActionCreators(allActions, dispatch)
...bindActionCreators({ joinCategoriesChannel }, dispatch),
productActions: bindActionCreators(productActions, dispatch)
}
}

Expand Down
15 changes: 8 additions & 7 deletions assets/js/react/containers/pages/Orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import socket from 'socket'
import { joinChannel as joinCategoriesChannel } from '~/store/actions/categories'
import { joinChannel as joinProductsChannel } from '~/store/actions/products'
import * as productActions from '~/store/actions/products'
import * as orderActions from '~/store/actions/products/orders'
import { ProductFormModal } from '~/components/modals'
import { VotesCell, OrderStatusCell } from '~/components/productsTable'
import * as orderActions from '~/store/actions/products/orders'
import SearchContainer from '../common/SearchContainer'
import ProductsTableContainer from '../common/ProductsTableContainer'

Expand All @@ -17,7 +17,7 @@ class Orders extends PureComponent {
super(props)
socket.connect()
props.joinCategoriesChannel()
props.joinProductsChannel('orders')
props.productActions.joinChannel('orders')
}

state = {
Expand Down Expand Up @@ -64,7 +64,7 @@ class Orders extends PureComponent {
product={product}
currUser={this.props.user}
onEdit={() => this.handleEdit(product)}
onChange={this.props.orderActions.update}
onChange={this.props.productActions.update}
/>
)
}
Expand All @@ -82,8 +82,8 @@ class Orders extends PureComponent {

handleSubmit = attrs => {
return this.state.modalProduct === MODAL_PRODUCT_NEW
? this.props.orderActions.create(attrs)
: this.props.orderActions.update(this.state.modalProduct.id, attrs)
? this.props.productActions.create(attrs)
: this.props.productActions.update(this.state.modalProduct.id, attrs)
}

handleHide = () => {
Expand Down Expand Up @@ -130,7 +130,8 @@ const mapStateToProps = state => ({

const mapDispatchToProps = dispatch => {
return {
...bindActionCreators({ joinProductsChannel, joinCategoriesChannel }, dispatch),
...bindActionCreators({ joinCategoriesChannel }, dispatch),
productActions: bindActionCreators(productActions, dispatch),
orderActions: bindActionCreators(orderActions, dispatch)
}
}
Expand Down
2 changes: 0 additions & 2 deletions assets/js/react/store/actionTypes/products/all.js

This file was deleted.

7 changes: 5 additions & 2 deletions assets/js/react/store/actionTypes/products/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ export const JOIN_CHANNEL = 'products/JOIN_CHANNEL'
export const JOIN_CHANNEL_SUCCESS = 'products/JOIN_CHANNEL_SUCCESS'

export const ALL_UPDATED = 'products/ALL_UPDATED'
export const UPDATE = 'products/UPDATE' // TODO: will be deleted

export const CREATE = 'products/CREATE'

export const UPDATE = 'products/UPDATE'
export const UPDATED = 'products/UPDATED'
export const DELETE = 'products/DELETE' // TODO: do we use thise

export const DELETED = 'products/DELETED'
2 changes: 0 additions & 2 deletions assets/js/react/store/actionTypes/products/orders.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export const CREATE = 'products/orders/CREATE'
export const UPDATE = 'products/orders/UPDATE'
export const UPVOTE = 'products/orders/UPVOTE'
export const DOWNVOTE = 'products/orders/DOWNVOTE'
30 changes: 0 additions & 30 deletions assets/js/react/store/actions/products/all.js

This file was deleted.

19 changes: 16 additions & 3 deletions assets/js/react/store/actions/products/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,22 @@ export const updateAll = items => ({
items
})

// TODO:
// This will be removed once we'll get rid from category
// select on every table

export const create = attrs => (dispatch, getState) => {
dispatch({
type: actionTypes.CREATE,
attrs
})

return new Promise((resolve, reject) => {
getState().products.channel
.push('create', { attrs })
.receive('ok', resolve)
.receive('error', reject)
})
}


export const update = (id, attrs) => (dispatch, getState) => {
dispatch({
type: actionTypes.UPDATE,
Expand Down
30 changes: 0 additions & 30 deletions assets/js/react/store/actions/products/orders.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
import * as actionTypes from '../../actionTypes/products/orders'

export const create = attrs => (dispatch, getState) => {
dispatch({
type: actionTypes.CREATE,
attrs
})

return new Promise((resolve, reject) => {
getState().products.channel
.push('create', { attrs })
.receive('ok', resolve)
.receive('error', reject)
})
}


export const update = (id, attrs) => (dispatch, getState) => {
dispatch({
type: actionTypes.UPDATE,
id, attrs
})

return new Promise((resolve, reject) => {
getState().products.channel
.push('update', { id, attrs })
.receive('ok', resolve)
.receive('error', reject)
})
}


export const upvote = id => (dispatch, getState) => {
dispatch({
type: actionTypes.UPVOTE,
Expand Down

0 comments on commit 989a841

Please sign in to comment.