Retailer.js (5370B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import { useContext, useState } from "react"; import { Alert, ScrollView, Text, TextInput, TouchableOpacity, View, } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; import ScreenHeader from "../components/ScreenHeader"; import ActionButton from "../components/ActionButton"; import Scanner from "../components/Scanner"; import LocationPicker from "../components/LocationPicker"; import TransferOwnershipForm from "../components/TransferOwnershipForm"; import { api } from "../services/api"; import styles from "../styles"; import { AuthContext } from "../AuthContext"; export default function RetailerScreen({ navigation, route }) { const { user } = useContext(AuthContext); const userId = route.params?.userId || user?.id; const token = user?.token; const [active, setActive] = useState(null); const [produceId, setProduceId] = useState(""); const [location, setLocation] = useState(""); const [pricePerUnit, setPricePerUnit] = useState(""); const [storageConditions, setStorageConditions] = useState(""); const resetCommon = () => { setProduceId(""); }; const updateLocation = async () => { try { await api.updateLocation( { produceId, actorId: userId, newLocation: location, }, token ); Alert.alert("Location Updated", "Location updated successfully"); resetCommon(); setLocation(""); setActive(null); } catch (err) { Alert.alert("Error", err.message); } }; const transferOwnership = async ({ produceId, newOwnerId, qty, salePrice, onComplete, }) => { if (!produceId || !newOwnerId || !qty) return Alert.alert( "Error", "Please provide Produce ID, New Owner ID, and Quantity." ); try { await api.transferOwnership( { produceId, newOwnerId, qty: parseFloat(qty), salePrice: parseFloat(salePrice), }, token ); Alert.alert("Transferred", "Transfer successful"); onComplete(); setActive(null); } catch (err) { Alert.alert("Error", err.message); } }; const updateDetails = async () => { try { await api.updateDetails( { produceId, actorId: userId, details: { pricePerUnit: parseFloat(pricePerUnit), storageConditions: storageConditions ? storageConditions.split(",").map((c) => c.trim()) : [], }, }, token ); Alert.alert("Updated Details", "Produce details updated successfully"); resetCommon(); setPricePerUnit(""); setStorageConditions(""); setActive(null); } catch (err) { Alert.alert("Error", err.message); } }; return ( <SafeAreaView style={styles.container}> <ScreenHeader title="Retailer Dashboard" navigation={navigation} role="Retailer" /> <ScrollView> <View style={styles.actionGrid}> <ActionButton icon="map-marker" text="Update Location" onPress={() => setActive("location")} /> <ActionButton icon="cash" text="Sell / Transfer" onPress={() => setActive("transfer")} /> <ActionButton icon="update" text="Update Details" onPress={() => setActive("update")} /> </View> {active === "location" && ( <View> <Scanner value={produceId} onChange={setProduceId} /> <LocationPicker value={location} onChange={setLocation} /> <TouchableOpacity style={styles.primaryButton} onPress={updateLocation} > <Text style={styles.buttonText}>Update Location</Text> </TouchableOpacity> </View> )} {active === "transfer" && ( <TransferOwnershipForm onSubmit={transferOwnership} /> )} {active === "update" && ( <View> <Scanner value={produceId} onChange={setProduceId} /> <TextInput style={styles.input} placeholder="New Price Per Unit" keyboardType="numeric" value={pricePerUnit} onChangeText={setPricePerUnit} /> <TextInput style={styles.input} placeholder="Storage Conditions (comma separated)" value={storageConditions} onChangeText={setStorageConditions} /> <TouchableOpacity style={styles.primaryButton} onPress={updateDetails} > <Text style={styles.buttonText}>Update</Text> </TouchableOpacity> </View> )} </ScrollView> <View style={{ padding: 12 }}> <TouchableOpacity style={styles.secondaryButton} onPress={() => navigation.navigate("Inventory", { userId, role: "Retailer", }) } > <Text style={styles.secondaryButtonText}>View Inventory</Text> </TouchableOpacity> </View> </SafeAreaView> ); } |