Skip to content

Commit

Permalink
connect customer api to react
Browse files Browse the repository at this point in the history
  • Loading branch information
swapnil-ahlawat committed Mar 28, 2021
1 parent 8fe58a9 commit 773eabc
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 70 deletions.
8 changes: 8 additions & 0 deletions Backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ const DUMMY_USERS = [
userType: 'Customer',
address: "Dwarka, New Delhi",
wallet: 500.10
},
{
name: 'Swapnil Ahlawat',
emailID: 'swapnil',
phoneNo: '9818284672',
password: 'treats',
userType: 'Warehouse',
address: "Dwarka, New Delhi",
}
]
connectDB();
Expand Down
9 changes: 5 additions & 4 deletions Backend/controllers/user-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ const placeOrder= async(req, res, next) =>{
identifiedPersonnel.orders.push({orderID: orderModel._id.toString()});

await identifiedPersonnel.save();
let identifiedUser = await User.findOne({phoneNo: customer.phoneNo}).exec().catch((error) => {
return next(error);
});
if(walletUsed){
let identifiedUser = await User.findOne({phoneNo: customer.phoneNo}).exec().catch((error) => {
return next(error);
});
identifiedUser.wallet = identifiedUser.wallet- parseFloat(total);
await identifiedUser.save();
}

res.json({
message: "OrderPlaced!"
message: "OrderPlaced!",
user: identifiedUser
});
}

Expand Down
4 changes: 2 additions & 2 deletions Backend/database/Order.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const order = new mongoose.Schema({
customerPhoneNo:{
type:String
},
reusablePackageSerialNumber:{
type: String
reusablePackageFlag:{
type:Boolean, default: false
},
foodItems: [{
quantity: {
Expand Down
58 changes: 42 additions & 16 deletions Frontend/screens/Cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,23 @@ import {
TouchableWithoutFeedback
} from "react-native";
import { ScrollView } from "react-native-gesture-handler";
import { Colors } from "react-native/Libraries/NewAppScreen";

import { icons, COLORS, SIZES, FONTS } from '../constants'

const Order = ({ route, navigation }) => {

const [restaurant, setRestaurant] = useState(null);
const [currentLocation, setCurrentLocation] = useState(null);
const [orderItems, setOrderItems] = useState([]);
const [reusablePackage, setReusablePackage] = useState(null);
const [checked, setChecked]= useState(true);
const [modalVisible, setModalVisible] = useState(false);


React.useEffect(() => {
let {restaurant, currentLocation, orderItems, reusablePackage} = route.params;
let {restaurant, orderItems, reusablePackage} = route.params;
if(restaurant){
setReusablePackage(reusablePackage);
setRestaurant(restaurant)
setCurrentLocation(currentLocation)
setOrderItems(orderItems)
}
})
Expand Down Expand Up @@ -88,9 +85,9 @@ const Order = ({ route, navigation }) => {
<View style={{margin: SIZES.padding * 2, marginBottom: SIZES.padding}}>
<Text style={{color: COLORS.white, ...FONTS.h2, textAlign:"center" }}>{restaurant?.name}</Text>
<Text style={{marginBottom: SIZES.padding*2, color: COLORS.darkgray, ...FONTS.body3, paddingTop: SIZES.padding,textAlign:"center" }}>Delivery Time: {restaurant?.duration}</Text>
<Text style={{color: COLORS.white, ...FONTS.body2}}>Swapnil Ahlawat</Text>
<Text style={{color: COLORS.white, ...FONTS.body3}}>{currentLocation}</Text>
<Text style={{color: COLORS.white, ...FONTS.body3}}>Phone Number : {global.id}</Text>
<Text style={{color: COLORS.white, ...FONTS.body2}}>{global.user.name}</Text>
<Text style={{color: COLORS.white, ...FONTS.body3}}>{global.user.address}</Text>
<Text style={{color: COLORS.white, ...FONTS.body3}}>Phone Number : {global.user.phoneNo}</Text>
<Text style={{marginTop: SIZES.padding, color: COLORS.white, ...FONTS.body2,fontWeight: 'bold'}}>Order Details</Text>
</View>
)
Expand All @@ -112,9 +109,9 @@ const Order = ({ route, navigation }) => {
justifyContent:"center"}}
>
<View style = {{flexDirection: "row", }}>
<Text style = {{ width:"10%",paddingLeft:SIZES.padding,...FONTS.body3, color: COLORS.black, justifyContent:"center"}}>{item.qty}x</Text>
<Text style = {{ width:"10%",paddingLeft:SIZES.padding,...FONTS.body3, color: COLORS.black, justifyContent:"center"}}>{item.quantity}x</Text>
<Text style = {{paddingLeft:SIZES.padding, width:"55%", ...FONTS.body3,color: COLORS.black}}>{restaurant["menu"].filter(a => a.menuId===item.menuId)[0].name}</Text>
<Text style={{ width:"35%", textAlign: "right", paddingRight:SIZES.padding,...FONTS.body3,color: COLORS.black}}>${item.total.toFixed(2)}</Text>
<Text style={{ width:"35%", textAlign: "right", paddingRight:SIZES.padding,...FONTS.body3,color: COLORS.black}}>${item.price.toFixed(2)}</Text>
</View>
</View>
)
Expand All @@ -131,8 +128,7 @@ const Order = ({ route, navigation }) => {
}

function sumOrder() {
let total = orderItems.reduce((a, b) => a + (b.total || 0), 0)

let total = orderItems.reduce((a, b) => a + (b.price|| 0), 0)
return total.toFixed(2)
}
function taxOrder() {
Expand Down Expand Up @@ -169,7 +165,7 @@ const Order = ({ route, navigation }) => {
</View>
<View style={{flexDirection: "row"}}>
<Text style={{width:"75%", color: COLORS.white, ...FONTS.body3, textAlign:"left"}}>Delivery Fee</Text>
<Text style={{width:"25%",color: COLORS.white, ...FONTS.body3, textAlign: "right"}}>$5.00</Text>
<Text style={{width:"25%",color: COLORS.white, ...FONTS.body3, textAlign: "right"}}>$4.00</Text>
</View>
{reusablePackageFee()}
<View style={{flexDirection: "row", marginVertical:SIZES.padding}}>
Expand Down Expand Up @@ -219,6 +215,39 @@ const Order = ({ route, navigation }) => {
)
}
function renderPlaceOrderButton(){
const placeOrderHandler = async () => {
try {
const response = await fetch('http://b51c079841e0.ngrok.io/user/placeOrder', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
reusablePackageFlag: reusablePackage,
restaurantPhoneNo: restaurant.phoneNo,
restaurantCourier: restaurant.courier.name,
orderItems: orderItems,
customer: global.user,
walletUsed: checked,
total: calculateTotal()
})
});
const responseData = await response.json();
if (!response.ok) {
alert('Error in placing order. Please try again later.')
throw new Error(responseData.message);
}
else{
global.user= responseData.user;
setModalVisible(true);
}
} catch (err) {
console.log(err);
alert('Error in placing order. Please try again later.')
}

};
return(
<TouchableOpacity
style={{
Expand All @@ -237,10 +266,7 @@ const Order = ({ route, navigation }) => {
setChecked(false);
}
else{
if(checked){
global.wallet -= calculateTotal();
}
setModalVisible(true);
placeOrderHandler();
}

}}
Expand Down
12 changes: 8 additions & 4 deletions Frontend/screens/CustomerHome.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const CustomerHome = ({ navigation }) => {
rating: 4.8,
categories: [5, 7],
priceRating: affordable,
phoneNo: 9818284671,
photo: images.burger_restaurant_1,
duration: "30 - 45 min",
location: {
Expand All @@ -93,7 +94,7 @@ const CustomerHome = ({ navigation }) => {
},
courier: {
avatar: images.avatar_1,
name: "Amy"
name: "Piyush Maheshwari"
},
menu: [
{
Expand Down Expand Up @@ -129,6 +130,7 @@ const CustomerHome = ({ navigation }) => {
rating: 4.8,
categories: [2, 4, 6],
priceRating: expensive,
phoneNo: 9818284671,
photo: images.pizza_restaurant,
duration: "15 - 20 min",
location: {
Expand Down Expand Up @@ -181,6 +183,7 @@ const CustomerHome = ({ navigation }) => {
rating: 4.8,
categories: [3],
priceRating: expensive,
phoneNo: 9818284671,
photo: images.hot_dog_restaurant,
duration: "20 - 25 min",
location: {
Expand Down Expand Up @@ -209,6 +212,7 @@ const CustomerHome = ({ navigation }) => {
rating: 4.8,
categories: [8],
priceRating: expensive,
phoneNo: 9818284671,
photo: images.japanese_restaurant,
duration: "10 - 15 min",
location: {
Expand Down Expand Up @@ -237,6 +241,7 @@ const CustomerHome = ({ navigation }) => {
rating: 4.8,
categories: [1, 2],
priceRating: affordable,
phoneNo: 9818284671,
photo: images.noodle_shop,
duration: "15 - 20 min",
location: {
Expand Down Expand Up @@ -291,6 +296,7 @@ const CustomerHome = ({ navigation }) => {
rating: 4.9,
categories: [9, 10],
priceRating: affordable,
phoneNo: 9818284671,
photo: images.kek_lapis_shop,
duration: "35 - 40 min",
location: {
Expand Down Expand Up @@ -334,7 +340,6 @@ const CustomerHome = ({ navigation }) => {
const [categories, setCategories] = React.useState(categoryData)
const [selectedCategory, setSelectedCategory] = React.useState(null)
const [restaurants, setRestaurants] = React.useState(restaurantData)
const [currentLocation, setCurrentLocation] = React.useState(global.address)

function onSelectCategory(category) {
//filter restaurant
Expand Down Expand Up @@ -368,7 +373,7 @@ const CustomerHome = ({ navigation }) => {
borderBottomRightRadius: SIZES.radius
}}
>
<Text style={{ ...FONTS.h3 ,color: COLORS.white}}>{currentLocation}</Text>
<Text style={{ ...FONTS.h3 ,color: COLORS.white}}>{global.user.address}</Text>
</View>
</View>

Expand Down Expand Up @@ -494,7 +499,6 @@ const CustomerHome = ({ navigation }) => {
style={{ marginBottom: SIZES.padding * 2 }}
onPress={() => navigation.navigate("Restaurant", {
item,
currentLocation
})}
>
{/* Image */}
Expand Down
42 changes: 19 additions & 23 deletions Frontend/screens/Restaurant.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,46 @@ const Restaurant = ({ route, navigation }) => {

const scrollX = new Animated.Value(0);
const [restaurant, setRestaurant] = useState(null);
const [currentLocation, setCurrentLocation] = useState(null);
const [orderItems, setOrderItems] = useState([]);
const [modalVisible, setModalVisible] = useState(false);
const [checked, setChecked]= useState(route.params.item.reusablePackage);

useEffect(() => {
let {item, currentLocation } = route.params;
let {item} = route.params;
global.orderItems=[]
setRestaurant(item)
setCurrentLocation(currentLocation)
})

function editOrder(action, menuId, price) {
function editOrder(action, menuId, price, name) {
let orderList = orderItems.slice()
let item = orderList.filter(a => a.menuId == menuId)

if (action == "+") {
if (item.length > 0) {
let newQty = item[0].qty + 1
item[0].qty = newQty
item[0].total = item[0].qty * price
let newQty = item[0].quantity + 1
item[0].quantity = newQty
item[0].price = item[0].quantity * price
} else {
const newItem = {
menuId: menuId,
qty: 1,
price: price,
total: price
name:name,
quantity: 1,
price: price
}
orderList.push(newItem)
}

setOrderItems(orderList)
} else {
if (item.length > 0) {
if (item[0]?.qty > 0) {
let newQty = item[0].qty - 1
if (item[0]?.quantity > 0) {
let newQty = item[0].quantity - 1
if(newQty===0){
orderList= orderList.filter(a=> a.menuId!= item[0].menuId)
}
else{
item[0].qty = newQty
item[0].total = newQty * price
item[0].quantity = newQty
item[0].price = newQty * price
}
}
}
Expand All @@ -74,22 +72,22 @@ const Restaurant = ({ route, navigation }) => {
let orderItem = orderItems.filter(a => a.menuId == menuId)

if (orderItem.length > 0) {
return orderItem[0].qty
return orderItem[0].quantity
}

return 0
}

function getBasketItemCount() {
let itemCount = orderItems.reduce((a, b) => a + (b.qty || 0), 0)
let itemCount = orderItems.reduce((a, b) => a + (b.quantity || 0), 0)

return itemCount
}

function sumOrder() {
let total = orderItems.reduce((a, b) => a + (b.total || 0), 0)
let price = orderItems.reduce((a, b) => a + (b.price || 0), 0)

return total.toFixed(2)
return price.toFixed(2)
}

function renderHeader() {
Expand Down Expand Up @@ -186,7 +184,7 @@ const Restaurant = ({ route, navigation }) => {
borderTopLeftRadius: 25,
borderBottomLeftRadius: 25
}}
onPress={() => editOrder("-", item.menuId, item.price)}
onPress={() => editOrder("-", item.menuId, item.price, item.name)}
>
<Text style={{ ...FONTS.body1,color: COLORS.black }}>-</Text>
</TouchableOpacity>
Expand All @@ -211,7 +209,7 @@ const Restaurant = ({ route, navigation }) => {
borderTopRightRadius: 25,
borderBottomRightRadius: 25
}}
onPress={() => editOrder("+", item.menuId, item.price)}
onPress={() => editOrder("+", item.menuId, item.price, item.name)}
>
<Text style={{ ...FONTS.body1,color: COLORS.black }}>+</Text>
</TouchableOpacity>
Expand Down Expand Up @@ -356,7 +354,7 @@ const Restaurant = ({ route, navigation }) => {
tintColor: COLORS.darkgray
}}
/>
<Text style={{ marginLeft: SIZES.padding, ...FONTS.h4 }}>{currentLocation}</Text>
<Text style={{ marginLeft: SIZES.padding, ...FONTS.h4 }}>{global.user.address}</Text>
</View>
</View>

Expand Down Expand Up @@ -387,7 +385,6 @@ const Restaurant = ({ route, navigation }) => {
else{
navigation.navigate("Cart", {
restaurant: restaurant,
currentLocation: currentLocation,
orderItems: orderItems,
reusablePackage: checked
})
Expand Down Expand Up @@ -504,7 +501,6 @@ const Restaurant = ({ route, navigation }) => {
setModalVisible(false);
navigation.navigate("Cart", {
restaurant: restaurant,
currentLocation: currentLocation,
orderItems: orderItems,
reusablePackage: checked
})}}
Expand Down
Loading

0 comments on commit 773eabc

Please sign in to comment.