Commit e1aa0de1 authored by Afid's avatar Afid

change quantity

parent 004e96f7
......@@ -32,7 +32,8 @@ const ActionType = {
SET_TRANS_TYPE : 'SET_TRANS_TYPE',
SET_RECALCULATE: 'SET_RECALCULATE',
SET_ORDER_FINISH: 'SET_ORDER_FINISH',
UPDATE_MENU: 'UPDATE_MENU'
UPDATE_MENU: 'UPDATE_MENU',
CHANGE_QUANTITY: 'CHANGE_QUANTITY'
}
export default ActionType;
\ No newline at end of file
......@@ -529,6 +529,65 @@ const rootReducer = (state = globalState, action) => {
}
}
case ActionType.CHANGE_QUANTITY: {
const item = action.data.item;
const new_quantity = action.data.new_quantity;
let total = 0
let quantity = 0
let list_menu = []
let list_item = []
// ini kalau sudah ada diupdate
if (!isNaN(parseInt(new_quantity))) {
exist = false
for (let i = 0; i < state.order_item.length; i++) {
const row = state.order_item[i];
if (row.id == item.id) {
// Update Price in Cart
row.quantity = parseInt(new_quantity)
exist = true
}
quantity += parseInt(row.quantity)
total += parseInt(row.price) * parseInt(row.quantity)
if (row.quantity != 0) {
list_item.push(row)
}
}
// kalo belum ada
if (!exist) {
list_item.push(item)
}
for (let i = 0; i < state.menu_item.length; i++) {
const row = state.menu_item[i];
console.log('MENU : ' + JSON.stringify(row))
// cari sudah ada belum di redux
let is_xist = list_item.find(it => it.id == row.id)
if (is_xist) {
row['qty'] = is_xist.quantity
} else {
row['qty'] = 0
}
list_menu.push(row)
}
return {
...state,
menu_item: list_menu,
order_total: total,
order_quantity: quantity,
order_item: list_item,
}
}
}
default:
return state;
}
......
import React from 'react';
import { View, Text, StyleSheet, ScrollView, TouchableOpacity, Image, TextInput } from 'react-native';
import { connect } from 'react-redux';
import ActionType from '../redux/globalActionType';
class Item extends React.Component {
handleChangeQuantity = (item, quantity) => {
this.props.changeQuantity({item: item, quantity: quantity})
}
render() {
const { item } = this.props
// console.log("ini id itemnya "+item.id)
......@@ -29,9 +35,8 @@ class Item extends React.Component {
</TouchableOpacity>
<TextInput
style={{ height: 25, padding: 2, textAlign: 'center', margin: 2 }}
autoCapitalize="none">
{item.qty}
</TextInput>
keyboardType={'numeric'}
onChangeText={(qty) => this.handleChangeQuantity(item, qty)}>{item.qty}</TextInput>
<TouchableOpacity onPress={() => this.props.handleAdd()}>
<Text style={{ fontSize: 20, margin: 2 }}> + </Text>
</TouchableOpacity>
......@@ -110,4 +115,16 @@ const mapStateToProps = (state) => {
}
}
export default connect(mapStateToProps)(Item)
const mapDispatchToProps = (dispacth) => {
return {
changeQuantity: (params) => dispacth({
type: ActionType.CHANGE_QUANTITY,
data: {
item: params.item,
new_quantity: params.quantity
}
}),
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Item)
import React from 'react';
import { View, Text, StyleSheet, ScrollView, TouchableOpacity, Image, TextInput } from 'react-native';
import { connect } from 'react-redux';
import ActionType from '../redux/globalActionType';
class ItemShoping extends React.Component {
handleChangeQuantity = (item, quantity) => {
console.log('CHange Quantity Shopping CHart : ' + JSON.stringify(item))
console.log('new quantity : ' + quantity)
this.props.changeQuantity({item: item, quantity: quantity})
}
render() {
const { item } = this.props
......@@ -25,9 +32,8 @@ class ItemShoping extends React.Component {
</TouchableOpacity>
<TextInput
style={{ height: 25, padding: 2, textAlign: 'center', margin: 2 }}
autoCapitalize="none">
{item.quantity}
</TextInput>
keyboardType={'numeric'}
onChangeText={(quantity) => this.handleChangeQuantity(item, quantity)}>{item.quantity}</TextInput>
<TouchableOpacity onPress={() => this.props.handleAdd()}>
<Text style={{ fontSize: 20, margin: 2 }}> + </Text>
</TouchableOpacity>
......@@ -103,4 +109,16 @@ const mapStateToProps = (state) => {
}
}
export default connect(mapStateToProps)(ItemShoping)
const mapDispatchToProps = (dispacth) => {
return {
changeQuantity: (params) => dispacth({
type: ActionType.CHANGE_QUANTITY,
data: {
item: params.item,
new_quantity: params.quantity
}
}),
}
}
export default connect(mapStateToProps, mapDispatchToProps)(ItemShoping)
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