|
| 1 | +import { Ionicons } from "@expo/vector-icons"; |
| 2 | +import { useContext, useState } from "react"; |
| 3 | +import { |
| 4 | + Alert, |
| 5 | + StyleSheet, |
| 6 | + Text, |
| 7 | + TouchableOpacity, |
| 8 | + View, |
| 9 | +} from "react-native"; |
| 10 | +import { Button, TextInput } from "react-native-paper"; |
| 11 | +import RNPickerSelect from "react-native-picker-select"; |
| 12 | +import { recordSettlement } from "../api/groups"; |
| 13 | +import { AuthContext } from "../context/AuthContext"; |
| 14 | +import { colors, spacing, typography } from "../styles/theme"; |
| 15 | + |
| 16 | +const SettleUpScreen = ({ route, navigation }) => { |
| 17 | + const { groupId, settlement, members } = route.params; |
| 18 | + const { token } = useContext(AuthContext); |
| 19 | + const [fromUser, setFromUser] = useState(settlement.fromUserId); |
| 20 | + const [toUser, setToUser] = useState(settlement.toUserId); |
| 21 | + const [amount, setAmount] = useState(settlement.amount.toString()); |
| 22 | + const [isLoading, setIsLoading] = useState(false); |
| 23 | + |
| 24 | + const memberOptions = members.map((member) => ({ |
| 25 | + label: member.user.name, |
| 26 | + value: member.userId, |
| 27 | + })); |
| 28 | + |
| 29 | + const handleRecordPayment = async () => { |
| 30 | + if (!fromUser || !toUser || !amount) { |
| 31 | + Alert.alert("Error", "Please fill in all fields."); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const settlementData = { |
| 36 | + payer_id: fromUser, |
| 37 | + payee_id: toUser, |
| 38 | + amount: parseFloat(amount), |
| 39 | + }; |
| 40 | + |
| 41 | + try { |
| 42 | + setIsLoading(true); |
| 43 | + await recordSettlement(groupId, settlementData); |
| 44 | + Alert.alert("Success", "Payment recorded successfully."); |
| 45 | + navigation.goBack(); |
| 46 | + } catch (error) { |
| 47 | + console.error("Failed to record settlement:", error); |
| 48 | + Alert.alert("Error", "Failed to record settlement."); |
| 49 | + } finally { |
| 50 | + setIsLoading(false); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + return ( |
| 55 | + <View style={styles.container}> |
| 56 | + <View style={styles.header}> |
| 57 | + <TouchableOpacity onPress={() => navigation.goBack()}> |
| 58 | + <Ionicons name="arrow-back" size={24} color={colors.text} /> |
| 59 | + </TouchableOpacity> |
| 60 | + <Text style={styles.title}>Settle Up</Text> |
| 61 | + </View> |
| 62 | + <Text style={styles.label}>From</Text> |
| 63 | + <RNPickerSelect |
| 64 | + onValueChange={(value) => setFromUser(value)} |
| 65 | + items={memberOptions} |
| 66 | + style={pickerSelectStyles} |
| 67 | + value={fromUser} |
| 68 | + placeholder={{ label: "Select a member", value: null }} |
| 69 | + /> |
| 70 | + <Text style={styles.label}>To</Text> |
| 71 | + <RNPickerSelect |
| 72 | + onValueChange={(value) => setToUser(value)} |
| 73 | + items={memberOptions} |
| 74 | + style={pickerSelectStyles} |
| 75 | + value={toUser} |
| 76 | + placeholder={{ label: "Select a member", value: null }} |
| 77 | + /> |
| 78 | + <TextInput |
| 79 | + label="Amount" |
| 80 | + value={amount} |
| 81 | + onChangeText={setAmount} |
| 82 | + keyboardType="numeric" |
| 83 | + style={styles.input} |
| 84 | + /> |
| 85 | + <Button |
| 86 | + mode="contained" |
| 87 | + onPress={handleRecordPayment} |
| 88 | + loading={isLoading} |
| 89 | + style={styles.button} |
| 90 | + > |
| 91 | + Record Payment |
| 92 | + </Button> |
| 93 | + </View> |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +const styles = StyleSheet.create({ |
| 98 | + container: { |
| 99 | + flex: 1, |
| 100 | + backgroundColor: colors.secondary, |
| 101 | + padding: spacing.md, |
| 102 | + }, |
| 103 | + header: { |
| 104 | + flexDirection: "row", |
| 105 | + alignItems: "center", |
| 106 | + marginBottom: spacing.lg, |
| 107 | + }, |
| 108 | + title: { |
| 109 | + ...typography.h2, |
| 110 | + color: colors.text, |
| 111 | + marginLeft: spacing.md, |
| 112 | + }, |
| 113 | + label: { |
| 114 | + ...typography.body, |
| 115 | + color: colors.textSecondary, |
| 116 | + marginBottom: spacing.xs, |
| 117 | + }, |
| 118 | + input: { |
| 119 | + marginBottom: spacing.md, |
| 120 | + }, |
| 121 | + button: { |
| 122 | + marginTop: spacing.md, |
| 123 | + }, |
| 124 | +}); |
| 125 | + |
| 126 | +const pickerSelectStyles = StyleSheet.create({ |
| 127 | + inputIOS: { |
| 128 | + fontSize: 16, |
| 129 | + paddingVertical: 12, |
| 130 | + paddingHorizontal: 10, |
| 131 | + borderWidth: 1, |
| 132 | + borderColor: "gray", |
| 133 | + borderRadius: 4, |
| 134 | + color: "black", |
| 135 | + paddingRight: 30, |
| 136 | + backgroundColor: colors.white, |
| 137 | + marginBottom: spacing.md, |
| 138 | + }, |
| 139 | + inputAndroid: { |
| 140 | + fontSize: 16, |
| 141 | + paddingHorizontal: 10, |
| 142 | + paddingVertical: 8, |
| 143 | + borderWidth: 0.5, |
| 144 | + borderColor: "purple", |
| 145 | + borderRadius: 8, |
| 146 | + color: "black", |
| 147 | + paddingRight: 30, |
| 148 | + backgroundColor: colors.white, |
| 149 | + marginBottom: spacing.md, |
| 150 | + }, |
| 151 | +}); |
| 152 | + |
| 153 | +export default SettleUpScreen; |
0 commit comments