import { defaultStyles } from '@/constants/defaultStyles'; import { supabase } from '@/lib/supabase'; import { router, Stack, useFocusEffect, useLocalSearchParams } from 'expo-router'; import { useCallback, useState } from 'react'; import { ActivityIndicator, Alert, Button, Keyboard, KeyboardAvoidingView, Platform, Pressable, Text, TextInput, TouchableWithoutFeedback, View } from 'react-native'; type Task = { tId: string; title: string; description: string; isCompleted: boolean; lastChanged: string; uId: string; aId: string; } export default function EditTask() { const { tId } = useLocalSearchParams<{ tId: string }>(); const [task, SetTask] = useState(null) const [isSaving, SetIsSaving] = useState(false); const GetTask = async (tId: string) => { const { data, error } = await supabase.from("tasks").select("*").eq("tId", tId).single(); if (error) { Alert.alert("Task could not be fetched, please try again"); return; } SetTask(data ?? null); } useFocusEffect( useCallback(() => { if (tId) { GetTask(tId); } }, [tId]) ); const EditTask = async () => { if (!task) return; if(task.title.trim() === '') { Alert.alert("Title is required!"); return; } const { data, error: userError } = await supabase.auth.getUser(); if(userError || !data.user) { router.replace("../createUser"); return; } SetIsSaving(true); const { error: dbError } = await supabase.from("tasks").update({ title: task.title, description: task.description, isCompleted: task.isCompleted, lastChanged: new Date().toISOString(), uId: data.user.id, aId: task.aId, }).eq("tId", tId); SetIsSaving(false); if (dbError) { Alert.alert("Task could not be edited, please try again"); return; } Alert.alert("Task successfully edited!"); router.back(); } return ( {!task && ( Task not found )} {task && ( Edit Task SetTask(prev => prev ? { ...prev, title: text } : prev)} /> SetTask(prev => prev ? { ...prev, description: text } : prev)} /> SetTask(prev => prev ? { ...prev, isCompleted: !prev.isCompleted } : prev)} style={defaultStyles.checkboxContainer} > {task.isCompleted && } {task.isCompleted ? 'Completed' : 'Not Completed'}