Commit fdbf2451 authored by Afid's avatar Afid

menu selection set di global variabel agar mudah dalam manipulasi

parent f72d6870
......@@ -32,6 +32,7 @@ const ActionType = {
SET_TRANS_TYPE : 'SET_TRANS_TYPE',
SET_RECALCULATE: 'SET_RECALCULATE',
SET_ORDER_FINISH: 'SET_ORDER_FINISH',
UPDATE_MENU: 'UPDATE_MENU'
}
export default ActionType;
\ No newline at end of file
......@@ -57,6 +57,8 @@ const globalState = {
orders: 0,
cart_shop: [],
menu_item: [],
order_item: [],
order_quantity: 0,
order_total: 0,
......@@ -305,8 +307,24 @@ const rootReducer = (state = globalState, action) => {
let quantity = state.order_quantity + 1
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 {
...state,
menu_item: menu_item,
order_item: order_item,
order_quantity: quantity,
order_total: total
......@@ -331,42 +349,64 @@ const rootReducer = (state = globalState, action) => {
update.quantity = 1
}
if (update.quantity === 0) {
if (update.quantity == 0) {
update.quantity = 0
} else {
if (update.quantity - 1 == 0) {
data = null
} else {
update.quantity -= 1
console.log('Update : ' + JSON.stringify(update))
data = { ...old_data, ...update }
}
}
}
if (data != null) {
updated_data.push(data)
}
}
order_item = updated_data
} else {
// add
addedItem.quantity = 1
// } else {
// // add
// addedItem.quantity = 1
console.log('Baru : ' + JSON.stringify(addedItem))
order_item = [...state.order_item, addedItem]
// console.log('Baru : ' + JSON.stringify(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_total = 0
// state.order_quantity = 0
// state.order_total = 0
console.log("MASUK INI")
}
// console.log("MASUK INI")
// }
let quantity = state.order_quantity - 1
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 {
...state,
menu_item: menu_item,
order_item: order_item,
order_quantity: quantity,
order_total: total
......@@ -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:
return state;
......
......@@ -58,35 +58,37 @@ class MenuSelection extends React.Component {
indicator: false
})
let last_category = ''
let list_menu = []
for (let i = 0; i < data.menu.length; i++) {
const row = data.menu[i];
// cari sudah ada belum di redux
let is_xist = this.props.order_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)
}
}
this.setState({
listMenu: list_menu
})
// let last_category = ''
// let list_menu = []
// for (let i = 0; i < data.menu.length; i++) {
// const row = data.menu[i];
// // cari sudah ada belum di redux
// let is_xist = this.props.order_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)
// }
// }
// this.setState({
// listMenu: list_menu
// })
this.props.updateMenu(data.menu)
})
......@@ -127,22 +129,22 @@ class MenuSelection extends React.Component {
let res = this.props.addToChart(order_item)
// console.log('After add : ' + JSON.stringify(res))
const list_order_item = this.props.order_item;
let is_xist = list_order_item.find(row => row.id == item.id)
// const list_order_item = this.props.order_item;
// let is_xist = list_order_item.find(row => row.id == item.id)
let quantity = 1
if (is_xist) {
console.log(is_xist.name + ' Item Sudah ada dengan quantity : ' + is_xist.quantity)
quantity = is_xist.quantity
}
// let quantity = 1
// if (is_xist) {
// console.log(is_xist.name + ' Item Sudah ada dengan quantity : ' + is_xist.quantity)
// quantity = is_xist.quantity
// }
const listMenu = [...this.state.listMenu]
listMenu[index].qty = quantity
console.log("HASIL : " + listMenu[index].qty)
this.setState({
listMenu
})
// const listMenu = [...this.state.listMenu]
// listMenu[index].qty = quantity
// console.log("HASIL : " + listMenu[index].qty)
// this.setState({
// listMenu
// })
}
......@@ -175,13 +177,13 @@ class MenuSelection extends React.Component {
}
this.props.reduceQuantityItem(order_item)
// let quantity = 0
console.log("INI TEST YA" + is_xist.quantity)
const listMenu = [...this.state.listMenu]
listMenu[index].qty = is_xist.quantity
this.setState({
listMenu
})
// // let quantity = 0
// console.log("INI TEST YA" + is_xist.quantity)
// const listMenu = [...this.state.listMenu]
// listMenu[index].qty = is_xist.quantity
// this.setState({
// listMenu
// })
}
}
}
......@@ -205,7 +207,7 @@ class MenuSelection extends React.Component {
) : (
<FlatList
data={this.state.listMenu}
data={this.props.menu_item}
renderItem={({ item, index }) => (
<Item navigation={this.props.navigation}
item={item}
......@@ -264,17 +266,16 @@ const mapStateToProps = (state) => {
order_total: state.order_total,
quantity: state.quantity,
address: state.address,
menu_item: state.menu_item
}
}
const mapDispatchToProps = (dispacth) => {
return {
setCartInsert: (data) => dispacth({
type: ActionType.SET_CHANGE_SHOP,
updateMenu: (menu) => dispacth({
type: ActionType.UPDATE_MENU,
data: {
id: data.cart_shop
menu: menu
}
}),
addToChart: (item) => dispacth({
......@@ -283,7 +284,6 @@ const mapDispatchToProps = (dispacth) => {
item: item
}
}),
//REDUCE QUANTITY ITEM
reduceQuantityItem: (item) => dispacth({
type: ActionType.REDUCE_QUANTITY_ITEM,
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