import React, { useState, useEffect } from 'react';
import { View, Text, FlatList, Image, StyleSheet, TouchableOpacity, TextInput, Alert } from 'react-native';
import axios from 'axios';
const API_URL = 'http://localhost:5000/api'; // Замените на IP-адрес сервера при тестировании на устройстве
const HomeScreen = ({ navigation }) => {
const [microgreens, setMicrogreens] = useState([]);
useEffect(() => {
axios.get(`${API_URL}/microgreens`).then(response => {
setMicrogreens(response.data);
});
}, []);
return (
<View style={styles.container}>
<Text style={styles.title}>Микрозелень в Калининграде</Text>
<FlatList
data={microgreens}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => (
<TouchableOpacity style={styles.card} onPress={() => navigation.navigate('Details', { item })}>
<Image source={{ uri: item.image }} style={styles.image} />
<Text style={styles.name}>{item.name}</Text>
<Text style={styles.price}>{item.price} ₽ / 50г</Text>
</TouchableOpacity>
)}
/>
</View>
);
};
const DetailsScreen = ({ route, navigation }) => {
const { item } = route.params;
return (
<View style={styles.container}>
<Text style={styles.title}>{item.name}</Text>
<Text style={styles.description}>{item.description}</Text>
<TouchableOpacity style={styles.button} onPress={() => navigation.navigate('Order', { item })}>
<Text style={styles.buttonText}>Заказать</Text>
</TouchableOpacity>
</View>
);
};
const OrderScreen = ({ route }) => {
const { item } = route.params;
const [name, setName] = useState('');
const [phone, setPhone] = useState('');
const handleOrder = () => {
axios.post(`${API_URL}/orders`, {
name,
phone,
items: [item],
}).then(() => {
Alert.alert('Успех!', 'Ваш заказ принят!');
}).catch(() => {
Alert.alert('Ошибка!', 'Не удалось оформить заказ.');
});
};
return (
<View style={styles.container}>
<Text style={styles.title}>Оформление заказа</Text>
<TextInput
style={styles.input}
placeholder="Ваше имя"
value={name}
onChangeText={setName}
/>
<TextInput
style={styles.input}
placeholder="Ваш телефон"
value={phone}
onChangeText={setPhone}
keyboardType="phone-pad"
/>
<TouchableOpacity style={styles.button} onPress={handleOrder}>
<Text style={styles.buttonText}>Подтвердить заказ</Text>
</TouchableOpacity>
</View>
);
};
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} options={{ title: 'Каталог' }} />
<Stack.Screen name="Details" component={DetailsScreen} options={{ title: 'Подробнее' }} />
<Stack.Screen name="Order" component={OrderScreen} options={{ title: 'Оформление заказа' }} />
</Stack.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#fff',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
},
card: {
backgroundColor: '#f9f9f9',
padding: 15,
borderRadius: 10,
marginBottom: 10,
alignItems: 'center',
},
image: {
width: 100,
height: 100,
borderRadius: 10,
},
name: {
fontSize: 18,
marginTop: 10,
},
price: {
fontSize: 16,
color: 'green',
},
description: {
fontSize: 16,
marginBottom: 20,
},
button: {
backgroundColor: '#4CAF50',
padding: 15,
borderRadius: 10,
alignItems: 'center',
},
buttonText: {
color: '#fff',
fontSize: 16,
},
input: {
borderWidth: 1,
borderColor: '#ccc',
padding: 10,
marginBottom: 10,
borderRadius: 5,
},
});