Cart Logic v2

This commit is contained in:
Med Kamel 2025-04-23 04:45:54 +01:00
parent eb23fd4696
commit bb4befc189
8 changed files with 105 additions and 17 deletions

View File

@ -4,11 +4,14 @@ import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { SafeAreaProvider } from 'react-native-safe-area-context'; import { SafeAreaProvider } from 'react-native-safe-area-context';
import React from 'react'; import React from 'react';
import { CartProvider } from '../context/cartContext' import { CartProvider } from '../context/cartContext'
import { StatusBar } from 'expo-status-bar';
export default function Layout() { export default function Layout() {
return ( return (
<GestureHandlerRootView style={{ flex: 1 }}> <GestureHandlerRootView style={{ flex: 1 }}>
<SafeAreaProvider> <SafeAreaProvider>
<StatusBar style="inverted" animated />
<CartProvider> <CartProvider>
<Stack screenOptions={{ headerShown: false}} /> <Stack screenOptions={{ headerShown: false}} />
</CartProvider> </CartProvider>

View File

@ -9,7 +9,7 @@ const OpeningScreen = () => {
return ( return (
<View style={styles.container}> <View style={styles.container}>
<StatusBar style="inverted" />
<Image source={require('@/assets/images/logo.png')} style={styles.logo} /> <Image source={require('@/assets/images/logo.png')} style={styles.logo} />
<Text style={styles.welcomeText}>Bienvenue chez Brix Café</Text> <Text style={styles.welcomeText}>Bienvenue chez Brix Café</Text>
<Image source={require('@/assets/images/coffee_cup.jpg')} style={styles.coffeeImage} /> <Image source={require('@/assets/images/coffee_cup.jpg')} style={styles.coffeeImage} />

View File

@ -132,8 +132,21 @@ export default function CartScreen() {
</Animated.Text> </Animated.Text>
</View> </View>
<TouchableOpacity style={styles.ConfirmButton} onPress={handleConfirmOrder} disabled={totalAmount <= 0}> <TouchableOpacity style={styles.ConfirmButton}
<Text style={styles.ConfirmButtonText}>Ajouter au panier</Text> onPress={() => {
Alert.alert(
'Confirmer la commande',
'Êtes-vous sûr de vouloir confirmer la commande?',
[
{text : 'Annuler', style: 'cancel'},
{text: 'Confirmer', style: 'destructive', onPress: handleConfirmOrder }
]
);
}
}
disabled={totalAmount <= 0}>
<Text style={styles.ConfirmButtonText}>Ajouter au panier</Text>
</TouchableOpacity> </TouchableOpacity>
<TouchableOpacity style={styles.clearCartButton} <TouchableOpacity style={styles.clearCartButton}

View File

@ -1,4 +1,4 @@
import { View, Text, StyleSheet, ScrollView, TouchableOpacity } from 'react-native'; import { View, Text, StyleSheet, ScrollView, TouchableOpacity, ActivityIndicator } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context'; import { SafeAreaView } from 'react-native-safe-area-context';
import { ArrowLeft } from 'lucide-react-native'; import { ArrowLeft } from 'lucide-react-native';
import { router } from "expo-router"; import { router } from "expo-router";
@ -10,11 +10,13 @@ import { db } from '@/firebase/config';
import { Product } from '@/constants/types'; import { Product } from '@/constants/types';
export default function GrainsScreen() { export default function GrainsScreen() {
const [products, setProducts] = useState<Product[]>([]); const [products, setProducts] = useState<Product[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => { useEffect(() => {
const fetchProducts = async () => { const fetchProducts = async () => {
try { try {
setLoading(true);
const querySnapshot = await getDocs(collection(db, 'products')); const querySnapshot = await getDocs(collection(db, 'products'));
const items: Product[] = querySnapshot.docs.map(doc => ({ const items: Product[] = querySnapshot.docs.map(doc => ({
id: doc.id, id: doc.id,
@ -23,6 +25,8 @@ export default function GrainsScreen() {
setProducts(items); setProducts(items);
} catch (error) { } catch (error) {
console.error("Erreur lors de la récupération des produits :", error); console.error("Erreur lors de la récupération des produits :", error);
} finally {
setLoading(false);
} }
}; };
fetchProducts(); fetchProducts();
@ -37,11 +41,18 @@ export default function GrainsScreen() {
<Text style={styles.headerTitle}>Grains de café</Text> <Text style={styles.headerTitle}>Grains de café</Text>
</View> </View>
<ScrollView style={styles.content}> {loading ? (
{products.map((product) => ( <View style={styles.loadingContainer}>
<ProductCard key={product.id} {...product} /> <ActivityIndicator size="large" color={COLORS.primary} />
))} <Text style={styles.loadingText}>Chargement...</Text>
</ScrollView> </View>
) : (
<ScrollView contentContainerStyle={styles.content}>
{products.map((product) => (
<ProductCard key={product.id} {...product} />
))}
</ScrollView>
)}
</SafeAreaView> </SafeAreaView>
); );
} }
@ -73,8 +84,18 @@ const styles = StyleSheet.create({
color: '#333', color: '#333',
}, },
content: { content: {
flex: 1, padding: 15,
padding: 30,
backgroundColor: COLORS.background_user, backgroundColor: COLORS.background_user,
paddingBottom: 10,
},
loadingContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
loadingText: {
marginTop: 10,
fontSize: 16,
color: '#666',
}, },
}); });

View File

@ -1,6 +1,7 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { View, Text, StyleSheet, Image, TouchableOpacity, ActivityIndicator,Alert } from 'react-native'; import { View, Text, StyleSheet, Image, TouchableOpacity, ActivityIndicator,Alert } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context'; import { SafeAreaView } from 'react-native-safe-area-context';
import { useFocusEffect } from '@react-navigation/native';
import { useLocalSearchParams, useRouter } from 'expo-router'; import { useLocalSearchParams, useRouter } from 'expo-router';
import { ChevronLeft, Minus, Plus } from 'lucide-react-native'; import { ChevronLeft, Minus, Plus } from 'lucide-react-native';
import { doc, getDoc } from 'firebase/firestore'; import { doc, getDoc } from 'firebase/firestore';
@ -51,6 +52,15 @@ export default function ProductScreen() {
fetchProductDetails(); fetchProductDetails();
}, [productId]); }, [productId]);
useFocusEffect(
React.useCallback(() => {
setQuantity(1);
return () => {};
}, [])
);
// Add item to cart // Add item to cart
const { addItem } = useCart(); const { addItem } = useCart();

View File

@ -1,15 +1,50 @@
import React, { useCallback } from 'react'; import React, { useCallback,useEffect, useState } from 'react';
import { View, Text, StyleSheet, Image,BackHandler } from 'react-native'; import { View, Text, StyleSheet, Image,BackHandler,ActivityIndicator } from 'react-native';
import { TouchableOpacity } from 'react-native-gesture-handler'; import { TouchableOpacity } from 'react-native-gesture-handler';
import { useRouter } from 'expo-router'; import { useRouter } from 'expo-router';
import { BellRing } from 'lucide-react-native'; import { BellRing } from 'lucide-react-native';
import COLORS from '@/constants/colors'; import COLORS from '@/constants/colors';
import { useFocusEffect } from '@react-navigation/native'; import { useFocusEffect } from '@react-navigation/native';
import { db } from '@/firebase/config'; // Firebase configuration
import { doc, getDoc } from 'firebase/firestore';
import { getAuth } from 'firebase/auth'; // Import Firebase Authentication
const UserHomeScreen = () => { const UserHomeScreen = () => {
const router = useRouter(); const router = useRouter();
const [coffeeShopName, setCoffeeShopName] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
// Fetch the current user's UID
const currentUser = getAuth().currentUser; // Get the current user from Firebase Authentication
// Fetch coffee shop name based on the current user's ownerId
useEffect(() => {
const fetchCoffeeShopName = async () => {
if (currentUser) {
try {
const docRef = doc(db, 'coffee_shops', currentUser.uid); // Use the user's UID to fetch data
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
const data = docSnap.data();
setCoffeeShopName(data?.cafeName || "Nom de Café");
} else {
console.log("No such coffee shop!");
setCoffeeShopName("Nom de Café");
}
} catch (error) {
console.error("Error fetching coffee shop data:", error);
setCoffeeShopName("Nom de Café");
} finally {
setLoading(false);
}
}
};
fetchCoffeeShopName();
}, [currentUser]);
// Empêcher retour arrière // Empêcher retour arrière
useFocusEffect( useFocusEffect(
useCallback(() => { useCallback(() => {
@ -24,7 +59,11 @@ useFocusEffect(
<View style={styles.header}> <View style={styles.header}>
<View> <View>
<Text style={styles.welcome}>Bienvenue,</Text> <Text style={styles.welcome}>Bienvenue,</Text>
<Text style={styles.shopName}>Nom de Café</Text> {loading ? (
<ActivityIndicator size="small" color={COLORS.primary} />
) : (
<Text style={styles.shopName}>{coffeeShopName}</Text>
)}
</View> </View>
<View style={styles.notification}> <View style={styles.notification}>
<TouchableOpacity> <TouchableOpacity>

View File

@ -1,5 +1,5 @@
import { Tabs } from 'expo-router'; import { Tabs } from 'expo-router';
import { Home, Coffee, User } from 'lucide-react-native'; import { Home, Coffee, User,ShoppingCartIcon } from 'lucide-react-native';
import { StyleSheet } from 'react-native'; import { StyleSheet } from 'react-native';
export default function UserLayout() { export default function UserLayout() {
@ -30,7 +30,7 @@ export default function UserLayout() {
name="CartScreen" name="CartScreen"
options={{ options={{
title: 'Cart', title: 'Cart',
tabBarIcon: ({ color, size }) => <Coffee size={size} color={color} />, tabBarIcon: ({ color, size }) => <ShoppingCartIcon size={size} color={color} />,
}} }}
/> />
<Tabs.Screen <Tabs.Screen

View File

@ -22,6 +22,7 @@ export default function ProductCard({
inStock = true, inStock = true,
}: ProductCardProps) { }: ProductCardProps) {
const router = useRouter(); const router = useRouter();
console.log(image); // Log the image URL to ensure it's correct
return ( return (
@ -40,6 +41,7 @@ export default function ProductCard({
resizeMode="cover" resizeMode="cover"
onError={(error) => console.log('Image loading error:', error.nativeEvent.error)} onError={(error) => console.log('Image loading error:', error.nativeEvent.error)}
/> />
<View style={styles.content}> <View style={styles.content}>
<View> <View>