import { useState } from 'react'; import { View, Text, TextInput, TouchableOpacity, ScrollView, Alert, ActivityIndicator } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { router } from 'expo-router'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import { api } from '../../../services/api'; import { useAuthStore } from '../../../stores/authStore'; const TASK_TYPES = [ { value: 'SUSPEND_CLIENT', label: 'Suspend Client', color: '#DC2626', bg: '#FEE2E2' }, { value: 'REACTIVATE_CLIENT', label: 'Reactivate Client', color: '#059669', bg: '#D1FAE5' }, { value: 'ACTIVATE_CLIENT', label: 'Activate Client', color: '#0891B2', bg: '#ECFEFF' }, ]; export default function NewManualTaskScreen() { const qc = useQueryClient(); const { user } = useAuthStore(); const [taskType, setTaskType] = useState('SUSPEND_CLIENT'); const [client, setClient] = useState(null); const [clientSearch, setClientSearch] = useState(''); const [debouncedClientSearch, setDebouncedClientSearch] = useState(''); const [assignTo, setAssignTo] = useState(null); const [showUserPicker, setShowUserPicker] = useState(false); const [notes, setNotes] = useState(''); const [loading, setLoading] = useState(false); // Client search const { data: clientResults, isFetching: searchingClients } = useQuery({ queryKey: ['manual-task-clients', debouncedClientSearch], queryFn: () => api.get(`/api/v1/clients?search=${debouncedClientSearch}&limit=8`).then(r => r.data?.data ?? r.data ?? []), enabled: debouncedClientSearch.trim().length >= 2, }); // Users list const { data: usersData } = useQuery({ queryKey: ['manual-task-users'], queryFn: () => api.get('/api/v1/users').then(r => r.data?.data ?? r.data ?? []), }); const handleClientSearchChange = (v: string) => { setClientSearch(v); setTimeout(() => setDebouncedClientSearch(v), 400); }; const submit = async () => { if (!client) return Alert.alert('Required', 'Please select a client.'); setLoading(true); try { await api.post('/api/v1/manual-tasks', { type: taskType, clientId: client.id, assignedToId: assignTo?.id ?? undefined, notes: notes.trim() || undefined, }); qc.invalidateQueries({ queryKey: ['manual-tasks'] }); Alert.alert('Task created', '', [{ text: 'OK', onPress: () => router.back() }]); } catch (e: any) { Alert.alert('Error', e?.response?.data?.message ?? 'Could not create task.'); } finally { setLoading(false); } }; const users: any[] = usersData ?? []; return ( {/* Header */} router.back()} style={{ marginBottom: 12 }} activeOpacity={0.7}> ← Back New Manual Task {/* Task Type */} Task Type * {TASK_TYPES.map(t => ( setTaskType(t.value)} style={{ flexDirection: 'row', alignItems: 'center', borderRadius: 14, padding: 16, marginBottom: 8, backgroundColor: taskType === t.value ? t.bg : '#FFF', borderWidth: 1.5, borderColor: taskType === t.value ? t.color : '#E2E8F0', }} activeOpacity={0.7} > {taskType === t.value && } {t.label} ))} {/* Client */} Client * {client ? ( {client.firstName} {client.lastName} {client.accountNumber} { setClient(null); setClientSearch(''); setDebouncedClientSearch(''); }} hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}> Change ) : ( {clientSearch.length > 0 && ( { setClientSearch(''); setDebouncedClientSearch(''); }} hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}> × )} {searchingClients && } {debouncedClientSearch.trim().length >= 2 && ( {(clientResults ?? []).length === 0 && !searchingClients ? ( No clients found ) : ( (clientResults ?? []).map((c: any, i: number, arr: any[]) => ( { setClient(c); setClientSearch(''); setDebouncedClientSearch(''); }} activeOpacity={0.7} > {c.firstName} {c.lastName} {c.accountNumber} )) )} )} )} {/* Assign To */} Assign To (optional) {assignTo ? ( {assignTo.firstName} {assignTo.lastName} {assignTo.roles?.[0] ?? assignTo.role ?? 'Staff'} setAssignTo(null)} hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }}> Clear ) : ( {users.length === 0 ? ( ) : ( users.map((u: any, i: number) => ( setAssignTo(u)} activeOpacity={0.7} > {u.firstName} {u.lastName} {u.roles?.[0] ?? u.role ?? 'Staff'} )) )} )} {/* Notes */} Notes (optional) {/* Submit */} {loading ? : Create Task} ); }