import React, { useState } from 'react'; import { View, Alert, KeyboardAvoidingView, Platform, TextInput, ActivityIndicator } from 'react-native'; import { MaterialIcons, Ionicons } from '@expo/vector-icons'; import { tw, MyText, MyTouchableOpacity, BottomDialog } from 'common-ui'; import { trpc } from '@/src/trpc-client'; import ComplaintForm from '@/components/ComplaintForm'; interface OrderMenuProps { orderId: string; postActionHandler?: () => void; } const OrderMenu: React.FC = ({ orderId, postActionHandler }) => { const [open, setOpen] = useState(false); const [editNotesDialogOpen, setEditNotesDialogOpen] = useState(false); const [editNotes, setEditNotes] = useState(''); const [complaintDialogOpen, setComplaintDialogOpen] = useState(false); const [cancelDialogOpen, setCancelDialogOpen] = useState(false); const [cancelReason, setCancelReason] = useState(''); const cancelOrderMutation = trpc.user.order.cancelOrder.useMutation(); const updateNotesMutation = trpc.user.order.updateUserNotes.useMutation({ onSuccess: () => { Alert.alert('Success', 'Notes updated successfully'); setEditNotesDialogOpen(false); setEditNotes(''); postActionHandler?.(); }, onError: (error: any) => { Alert.alert('Error', error.message || 'Failed to update notes'); }, }); const handleEditNotes = async () => { try { await updateNotesMutation.mutateAsync({ id: orderId, userNotes: editNotes.trim() }); } catch (error) { // Error handled in mutation } }; const handleCancelOrder = async () => { if (!cancelReason.trim()) { Alert.alert('Error', 'Please enter a reason for cancellation'); return; } try { await cancelOrderMutation.mutateAsync({ id: orderId, reason: cancelReason }); Alert.alert('Success', 'Order cancelled successfully'); setCancelDialogOpen(false); setCancelReason(''); postActionHandler?.(); } catch (error: any) { Alert.alert('Error', error.message || 'Failed to cancel order'); } }; const handleComplaintClose = () => { setComplaintDialogOpen(false); postActionHandler?.(); }; return ( <> {/* Menu Trigger */} setOpen(true)} > {/* Menu Dialog */} setOpen(false)}> {/* Handle */} Order Options Select an action for Order #{orderId} { setEditNotes(''); setEditNotesDialogOpen(true); }} > Edit Notes Add special instructions setComplaintDialogOpen(true)} > Raise Complaint Report an issue with this order setCancelDialogOpen(true)} > Cancel Order Request order cancellation {/* Edit Notes Dialog */} setEditNotesDialogOpen(false)}> Edit Instructions setEditNotesDialogOpen(false)}> {updateNotesMutation.isPending ? ( ) : ( Save Instructions )} {/* Cancel Order Dialog */} setCancelDialogOpen(false)}> Cancel Order setCancelDialogOpen(false)}> Are you sure you want to cancel this order? This action cannot be undone. Reason for cancellation {cancelOrderMutation.isPending ? ( ) : ( Confirm Cancellation )} {/* Raise Complaint Dialog */} ); }; export default OrderMenu;