Commit 9e0f0674 authored by Wahyu Adjie Prasetyo's avatar Wahyu Adjie Prasetyo
parents 09dc293c fdbf2451
...@@ -32,6 +32,7 @@ const ActionType = { ...@@ -32,6 +32,7 @@ const ActionType = {
SET_TRANS_TYPE : 'SET_TRANS_TYPE', SET_TRANS_TYPE : 'SET_TRANS_TYPE',
SET_RECALCULATE: 'SET_RECALCULATE', SET_RECALCULATE: 'SET_RECALCULATE',
SET_ORDER_FINISH: 'SET_ORDER_FINISH', SET_ORDER_FINISH: 'SET_ORDER_FINISH',
UPDATE_MENU: 'UPDATE_MENU'
} }
export default ActionType; export default ActionType;
\ No newline at end of file
...@@ -57,6 +57,8 @@ const globalState = { ...@@ -57,6 +57,8 @@ const globalState = {
orders: 0, orders: 0,
cart_shop: [], cart_shop: [],
menu_item: [],
order_item: [], order_item: [],
order_quantity: 0, order_quantity: 0,
order_total: 0, order_total: 0,
...@@ -305,8 +307,24 @@ const rootReducer = (state = globalState, action) => { ...@@ -305,8 +307,24 @@ const rootReducer = (state = globalState, action) => {
let quantity = state.order_quantity + 1 let quantity = state.order_quantity + 1
let total = parseInt(state.order_total) + parseInt(addedItem.price) let total = parseInt(state.order_total) + parseInt(addedItem.price)
let menu_item = []
for (let i = 0; i < state.menu_item.length; i++) {
const row = state.menu_item[i];
if (row.id == addedItem.id) {
let is_exist = order_item.find(item => addedItem.id == item.id)
if (is_exist) {
row.qty = is_exist.quantity
}
}
menu_item.push(row)
}
return { return {
...state, ...state,
menu_item: menu_item,
order_item: order_item, order_item: order_item,
order_quantity: quantity, order_quantity: quantity,
order_total: total order_total: total
...@@ -331,42 +349,64 @@ const rootReducer = (state = globalState, action) => { ...@@ -331,42 +349,64 @@ const rootReducer = (state = globalState, action) => {
update.quantity = 1 update.quantity = 1
} }
if (update.quantity === 0) { if (update.quantity == 0) {
update.quantity = 0 update.quantity = 0
} else {
if (update.quantity - 1 == 0) {
data = null
} else { } else {
update.quantity -= 1 update.quantity -= 1
console.log('Update : ' + JSON.stringify(update)) console.log('Update : ' + JSON.stringify(update))
data = { ...old_data, ...update } data = { ...old_data, ...update }
} }
} }
}
if (data != null) {
updated_data.push(data) updated_data.push(data)
} }
}
order_item = updated_data order_item = updated_data
} else { // } else {
// add // // add
addedItem.quantity = 1 // addedItem.quantity = 1
console.log('Baru : ' + JSON.stringify(addedItem)) // console.log('Baru : ' + JSON.stringify(addedItem))
order_item = [...state.order_item, addedItem] // order_item = [...state.order_item, addedItem]
} }
if (state.order_quantity === 0 && state.order_total === 0) { // if (state.order_quantity === 0 && state.order_total === 0) {
state.order_quantity = 0 // state.order_quantity = 0
state.order_total = 0 // state.order_total = 0
console.log("MASUK INI") // console.log("MASUK INI")
} // }
let quantity = state.order_quantity - 1 let quantity = state.order_quantity - 1
let total = parseInt(state.order_total) - parseInt(addedItem.price) let total = parseInt(state.order_total) - parseInt(addedItem.price)
let menu_item = []
for (let i = 0; i < state.menu_item.length; i++) {
const row = state.menu_item[i];
if (row.id == addedItem.id) {
let is_exist = order_item.find(item => addedItem.id == item.id)
if (is_exist) {
row.qty = is_exist.quantity
} else {
row.qty = 0
}
}
menu_item.push(row)
}
return { return {
...state, ...state,
menu_item: menu_item,
order_item: order_item, order_item: order_item,
order_quantity: quantity, order_quantity: quantity,
order_total: total order_total: total
...@@ -429,6 +469,64 @@ const rootReducer = (state = globalState, action) => { ...@@ -429,6 +469,64 @@ const rootReducer = (state = globalState, action) => {
} }
} }
case ActionType.UPDATE_MENU: {
const menu = action.data.menu;
let total = 0
let quantity = 0
let list_menu = []
let list_item = []
for (let i = 0; i < state.order_item.length; i++) {
const row = state.order_item[i];
let is_updated = state.menu_item.find(item => item.id == row.id)
// Update Price in Cart
if (is_updated) {
row.price = is_updated.price
}
quantity += parseInt(row.quantity)
total += parseInt(row.price) * parseInt(row.quantity)
list_item.push(row)
}
let last_category = ''
for (let i = 0; i < menu.length; i++) {
const row = menu[i];
// cari sudah ada belum di redux
let is_xist = list_item.find(item => row.id == item.id)
if (is_xist) {
row['qty'] = is_xist.quantity
} else {
row['qty'] = 0
}
if (last_category != row.category.name) {
let category = {
"id": row.category.id,
"name": row.category.name,
"code": "CATEGORY"
}
list_menu.push(category)
list_menu.push(row)
last_category = row.category.name
} else {
list_menu.push(row)
}
}
return {
...state,
menu_item: list_menu,
order_total: total,
order_quantity: quantity,
order_item: list_item,
}
}
default: default:
return state; return state;
......
...@@ -59,35 +59,37 @@ class MenuSelection extends React.Component { ...@@ -59,35 +59,37 @@ class MenuSelection extends React.Component {
indicator: false indicator: false
}) })
let last_category = '' // let last_category = ''
let list_menu = [] // let list_menu = []
for (let i = 0; i < data.menu.length; i++) { // for (let i = 0; i < data.menu.length; i++) {
const row = data.menu[i]; // const row = data.menu[i];
// cari sudah ada belum di redux // // cari sudah ada belum di redux
let is_xist = this.props.order_item.find(item => row.id == item.id) // let is_xist = this.props.order_item.find(item => row.id == item.id)
if (is_xist) { // if (is_xist) {
row['qty'] = is_xist.quantity // row['qty'] = is_xist.quantity
} else { // } else {
row['qty'] = 0 // row['qty'] = 0
} // }
if (last_category != row.category.name) { // if (last_category != row.category.name) {
let category = { // let category = {
"id": row.category.id, // "id": row.category.id,
"name": row.category.name, // "name": row.category.name,
"code": "CATEGORY" // "code": "CATEGORY"
} // }
list_menu.push(category) // list_menu.push(category)
list_menu.push(row) // list_menu.push(row)
last_category = row.category.name // last_category = row.category.name
} else { // } else {
list_menu.push(row) // list_menu.push(row)
} // }
} // }
this.setState({ // this.setState({
listMenu: list_menu // listMenu: list_menu
}) // })
this.props.updateMenu(data.menu)
}) })
...@@ -128,22 +130,22 @@ class MenuSelection extends React.Component { ...@@ -128,22 +130,22 @@ class MenuSelection extends React.Component {
let res = this.props.addToChart(order_item) let res = this.props.addToChart(order_item)
// console.log('After add : ' + JSON.stringify(res)) // console.log('After add : ' + JSON.stringify(res))
const list_order_item = this.props.order_item; // const list_order_item = this.props.order_item;
let is_xist = list_order_item.find(row => row.id == item.id) // let is_xist = list_order_item.find(row => row.id == item.id)
let quantity = 1 // let quantity = 1
if (is_xist) { // if (is_xist) {
console.log(is_xist.name + ' Item Sudah ada dengan quantity : ' + is_xist.quantity) // console.log(is_xist.name + ' Item Sudah ada dengan quantity : ' + is_xist.quantity)
quantity = is_xist.quantity // quantity = is_xist.quantity
} // }
const listMenu = [...this.state.listMenu] // const listMenu = [...this.state.listMenu]
listMenu[index].qty = quantity // listMenu[index].qty = quantity
console.log("HASIL : " + listMenu[index].qty) // console.log("HASIL : " + listMenu[index].qty)
this.setState({ // this.setState({
listMenu // listMenu
}) // })
} }
...@@ -176,13 +178,13 @@ class MenuSelection extends React.Component { ...@@ -176,13 +178,13 @@ class MenuSelection extends React.Component {
} }
this.props.reduceQuantityItem(order_item) this.props.reduceQuantityItem(order_item)
// let quantity = 0 // // let quantity = 0
console.log("INI TEST YA" + is_xist.quantity) // console.log("INI TEST YA" + is_xist.quantity)
const listMenu = [...this.state.listMenu] // const listMenu = [...this.state.listMenu]
listMenu[index].qty = is_xist.quantity // listMenu[index].qty = is_xist.quantity
this.setState({ // this.setState({
listMenu // listMenu
}) // })
} }
} }
} }
...@@ -206,7 +208,7 @@ class MenuSelection extends React.Component { ...@@ -206,7 +208,7 @@ class MenuSelection extends React.Component {
) : ( ) : (
<FlatList <FlatList
data={this.state.listMenu} data={this.props.menu_item}
renderItem={({ item, index }) => ( renderItem={({ item, index }) => (
<Item navigation={this.props.navigation} <Item navigation={this.props.navigation}
item={item} item={item}
...@@ -265,17 +267,16 @@ const mapStateToProps = (state) => { ...@@ -265,17 +267,16 @@ const mapStateToProps = (state) => {
order_total: state.order_total, order_total: state.order_total,
quantity: state.quantity, quantity: state.quantity,
address: state.address, address: state.address,
menu_item: state.menu_item
} }
} }
const mapDispatchToProps = (dispacth) => { const mapDispatchToProps = (dispacth) => {
return { return {
setCartInsert: (data) => dispacth({ updateMenu: (menu) => dispacth({
type: ActionType.SET_CHANGE_SHOP, type: ActionType.UPDATE_MENU,
data: { data: {
id: data.cart_shop menu: menu
} }
}), }),
addToChart: (item) => dispacth({ addToChart: (item) => dispacth({
...@@ -284,7 +285,6 @@ const mapDispatchToProps = (dispacth) => { ...@@ -284,7 +285,6 @@ const mapDispatchToProps = (dispacth) => {
item: item item: item
} }
}), }),
//REDUCE QUANTITY ITEM
reduceQuantityItem: (item) => dispacth({ reduceQuantityItem: (item) => dispacth({
type: ActionType.REDUCE_QUANTITY_ITEM, type: ActionType.REDUCE_QUANTITY_ITEM,
data: { data: {
......
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