Distributor.js (6482B)
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | 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 DistributorScreen({ 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 [reason, setReason] = useState(""); const [newStatus, setNewStatus] = 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", "Ownership transferred successfully"); onComplete(); setActive(null); } catch (err) { Alert.alert("Error", err.message); } }; const markAsUnavailable = async () => { try { await api.markAsUnavailable( { produceId, actorId: userId, reason, newStatus }, token ); Alert.alert("Marked Unavailable", "Produce marked as unavailable"); resetCommon(); setReason(""); setNewStatus(""); setActive(null); } catch (err) { Alert.alert("Error", err.message); } }; const updateStorageConditions = async () => { try { await api.updateDetails( { produceId, actorId: userId, details: { storageConditions: storageConditions ? storageConditions.split(",").map((c) => c.trim()) : [], }, }, token ); Alert.alert("Updated", "Storage conditions updated successfully"); resetCommon(); setStorageConditions(""); setActive(null); } catch (err) { Alert.alert("Error", err.message); } }; return ( <SafeAreaView style={styles.container}> <ScreenHeader title="Distributor Dashboard" navigation={navigation} role="Distributor" /> <ScrollView> <View style={styles.actionGrid}> <ActionButton icon="map-marker" text="Update Location" onPress={() => setActive("location")} /> <ActionButton icon="cash" text="Transfer Ownership" onPress={() => setActive("transfer")} /> <ActionButton icon="cancel" text="Mark Unavailable" onPress={() => setActive("remove")} /> <ActionButton icon="thermometer" text="Update Storage" onPress={() => setActive("storage")} /> </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 === "remove" && ( <View> <Scanner value={produceId} onChange={setProduceId} /> <TextInput style={styles.input} placeholder="Reason" value={reason} onChangeText={setReason} /> <TextInput style={styles.input} placeholder="New Status (e.g. Removed, Missing)" value={newStatus} onChangeText={setNewStatus} /> <TouchableOpacity style={styles.primaryButton} onPress={markAsUnavailable} > <Text style={styles.buttonText}>Mark as Unavailable</Text> </TouchableOpacity> </View> )} {active === "storage" && ( <View> <Scanner value={produceId} onChange={setProduceId} /> <TextInput style={styles.input} placeholder="Storage Conditions (comma separated)" value={storageConditions} onChangeText={setStorageConditions} /> <TouchableOpacity style={styles.primaryButton} onPress={updateStorageConditions} > <Text style={styles.buttonText}>Update Storage Conditions</Text> </TouchableOpacity> </View> )} </ScrollView> <View style={{ padding: 12 }}> <TouchableOpacity style={styles.secondaryButton} onPress={() => navigation.navigate("Inventory", { userId, role: "Distributor", }) } > <Text style={styles.secondaryButtonText}>View Inventory</Text> </TouchableOpacity> </View> </SafeAreaView> ); } |