Commit 882f594f authored by Afid's avatar Afid

baru tambah

parent 0881d4c0
...@@ -22,6 +22,8 @@ const ActionType = { ...@@ -22,6 +22,8 @@ const ActionType = {
SET_CHANGE_CAMERA_ID:'SET_CHANGE_CAMERA_ID', SET_CHANGE_CAMERA_ID:'SET_CHANGE_CAMERA_ID',
SET_CHANGE_OUTLET:'SET_CHANGE_OUTLET', SET_CHANGE_OUTLET:'SET_CHANGE_OUTLET',
SET_CHANGE_SHOP:'SET_CHANGE_SHOP', SET_CHANGE_SHOP:'SET_CHANGE_SHOP',
ADD_TO_CHART:'ADD_TO_CHART',
REMOVE_FROM_CHART:'REMOVE_FROM_CHART',
} }
......
...@@ -53,6 +53,10 @@ const globalState = { ...@@ -53,6 +53,10 @@ const globalState = {
quantity:0, quantity:0,
orders :0, orders :0,
cart_shop:[], cart_shop:[],
order_item: [],
order_quantity: 0,
order_total: 0
} }
const rootReducer = (state = globalState, action) => { const rootReducer = (state = globalState, action) => {
...@@ -192,16 +196,8 @@ const rootReducer = (state = globalState, action) => { ...@@ -192,16 +196,8 @@ const rootReducer = (state = globalState, action) => {
} }
} }
case ActionType.SET_CHANGE_SHOP:{ case ActionType.SET_CHANGE_SHOP:{
let is_exists = cart_shop.find(item => item.id === action.data.id)
if (is_exists) {
// update
} else {
// add
}
return{ return{
...state
} }
} }
case ActionType.SET_LOGOUT:{ case ActionType.SET_LOGOUT:{
...@@ -210,6 +206,26 @@ const rootReducer = (state = globalState, action) => { ...@@ -210,6 +206,26 @@ const rootReducer = (state = globalState, action) => {
session_id:action.data.session_id, session_id:action.data.session_id,
} }
} }
case ActionType.ADD_TO_CHART:{
let addedItem = action.data.order_item
let is_exist = cart_shop.find(item => addedItem.id == item.id)
if (is_exist) {
// update
addedItem.quantity += 1
} else {
// add
addedItem.quantity = 1
}
let quantity = order_quantity + 1
let total = order_total + addedItem.price
return{
...state,
order_item: [...state.order_item, addedItem],
order_quantity: quantity,
order_total: total
}
}
default: default:
......
...@@ -79,25 +79,13 @@ class MenuSelection extends React.Component { ...@@ -79,25 +79,13 @@ class MenuSelection extends React.Component {
} }
handleAdd(item, index) { handleAdd(item) {
const order_item = {
// const listMenu = [...this.state.listMenu]
// listMenu[index].qty += 1
const order = {
id: item.id, id: item.id,
name: item.name, name: item.name,
price: item.price, price: item.price,
quantity: 1
} }
this.state.list_orders.push(order); this.props.addToChart(order_item)
this.setState({
listMenu
})
let InsertCart = {
cart_shop: this.state.list_orders
}
this.props.setCartInsert(InsertCart)
} }
...@@ -128,7 +116,7 @@ class MenuSelection extends React.Component { ...@@ -128,7 +116,7 @@ class MenuSelection extends React.Component {
renderItem={({ item, index }) => ( renderItem={({ item, index }) => (
<Item navigation={this.props.navigation} <Item navigation={this.props.navigation}
item={item} item={item}
handleAdd={() => this.handleAdd(item, index)} handleAdd={() => this.handleAdd(item)}
handleRemove={() => this.handleRemove(item, index)} handleRemove={() => this.handleRemove(item, index)}
refreshing={this.state.isFetching} refreshing={this.state.isFetching}
/> />
...@@ -190,10 +178,16 @@ const mapStateToProps = (state) => { ...@@ -190,10 +178,16 @@ const mapStateToProps = (state) => {
const mapDispatchToProps = (dispacth) => { const mapDispatchToProps = (dispacth) => {
return { return {
setCartInsert: (InsertCart) => dispacth({ setCartInsert: (data) => dispacth({
type: ActionType.SET_CHANGE_SHOP, type: ActionType.SET_CHANGE_SHOP,
data: { data: {
cart_shop: InsertCart.cart_shop id: data.cart_shop
}
}),
addToChart: (order_item) => dispacth({
type: ActionType.ADD_TO_CHART,
data: {
order_item: params.order_item
} }
}), }),
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment