From 5bde3735149f4ece8c16e6282d35808fd1d4f633 Mon Sep 17 00:00:00 2001 From: Fhj0607 Date: Tue, 21 Apr 2026 12:56:24 +0200 Subject: [PATCH] style edit task screen and fix task update flow --- app/(tabs)/_layout.tsx | 8 +- app/(tabs)/editTask.tsx | 415 ++++++++++++++++++++++++++++++---------- app/createUser.tsx | 2 +- lib/supabase.ts | 8 + 4 files changed, 330 insertions(+), 103 deletions(-) diff --git a/app/(tabs)/_layout.tsx b/app/(tabs)/_layout.tsx index a028b63..a3d72a9 100644 --- a/app/(tabs)/_layout.tsx +++ b/app/(tabs)/_layout.tsx @@ -1,6 +1,6 @@ import { supabase } from "@/lib/supabase"; import { Session } from "@supabase/supabase-js"; -import { Redirect, Tabs } from "expo-router"; +import { Tabs } from "expo-router"; import { useEffect, useState } from "react"; export default function TabLayout() { @@ -27,9 +27,9 @@ export default function TabLayout() { return null; } - if (!session) { - return ; - } + // if (!session) { + // return ; + // } return ( diff --git a/app/(tabs)/editTask.tsx b/app/(tabs)/editTask.tsx index 9cce4b0..6957ad5 100644 --- a/app/(tabs)/editTask.tsx +++ b/app/(tabs)/editTask.tsx @@ -1,80 +1,103 @@ import { defaultStyles } from '@/constants/defaultStyles'; import { supabase } from '@/lib/supabase'; -import { router, Stack, useFocusEffect, useLocalSearchParams } from 'expo-router'; +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'; +import { + ActivityIndicator, + Alert, + Keyboard, + KeyboardAvoidingView, + Platform, + Pressable, + ScrollView, + StyleSheet, + Text, + TextInput, + TouchableWithoutFeedback, + View +} from 'react-native'; export default function EditTask() { - const [title, SetTitle] = useState(''); - const [description, SetDescription] = useState(''); - const [isCompleted, SetIsCompleted] = useState(false); - const [deadline, SetDeadline] = useState(''); - const [isSaving, SetIsSaving] = useState(false); - const { tId } = useLocalSearchParams(); + const { taskId } = useLocalSearchParams<{ taskId?: string }>(); + + const [title, setTitle] = useState(''); + const [description, setDescription] = useState(''); + const [deadline, setDeadline] = useState(''); + const [isCompleted, setIsCompleted] = useState(false); + + const [isLoading, setIsLoading] = useState(false); + const [isSaving, setIsSaving] = useState(false); useFocusEffect( useCallback(() => { - const GetTask = async () => { - if (!tId) return; + const getTask = async () => { + if (!taskId) return; - const { data, error } = await supabase.from("tasks").select("*").eq("tId", tId).single(); + setIsLoading(true); - if (error) { - Alert.alert("Task not found"); - return; + try { + const { data, error } = await supabase + .from('tasks') + .select('*') + .eq('tId', taskId) + .single(); + + if (error || !data) { + Alert.alert('Task not found'); + router.back(); + return; + } + + setTitle(data.title ?? ''); + setDescription(data.description ?? ''); + setDeadline(data.deadline ?? ''); + setIsCompleted(Boolean(data.isCompleted)); + } finally { + setIsLoading(false); } + }; - if (data) { - SetTitle(data.title); - SetDescription(data.description); - SetIsCompleted(data.isCompleted); - SetDeadline(data.deadline); - } - } - GetTask(); - }, [tId]) + getTask(); + }, [taskId]) ); - const EditTask = async () => { - if(title.trim() === '' || description.trim() === '' || deadline.trim() === '') { + const handleSaveTask = async () => { + if (!title.trim() || !description.trim() || !deadline.trim()) { Alert.alert("All fields are required!"); return; } - - const { data, error: userError } = await supabase.auth.getUser(); + setIsSaving(true); - if(userError || !data.user) { - router.replace("../createUser"); - return; + try { + const { data: userData, error: userError } = await supabase.auth.getUser(); + if (userError || !userData.user) { + router.replace("../createUser"); + return; + } + const { error: dbError } = await supabase.from("tasks").update({ + title, + description, + isCompleted, + lastChanged: new Date().toISOString(), + deadline, + uId: userData.user.id, + }).eq("tId", taskId); + + if (dbError) { + Alert.alert("Task could not be edited, please try again"); + return; + } + Alert.alert("Task successfully edited!"); + router.back(); + } finally { + setIsSaving(false); } - - SetIsSaving(true); - - const { error: dbError } = await supabase.from("tasks").update({ - title, - description, - isCompleted, - lastChanged: new Date().toISOString(), - deadline, - uId: data.user.id, - }).eq("tId", tId); - - if (dbError) { - Alert.alert("Task could not be edited, please try again"); - return; - } - - Alert.alert("Task successfully edited!"); - - SetTitle(''); - SetDescription(''); - SetIsCompleted(false); - SetDeadline(''); - - SetIsSaving(false); - - router.back(); - } + }; return ( <> @@ -85,49 +108,245 @@ export default function EditTask() { }} /> - - Edit Task - - - - - - - SetIsCompleted(state => !state)} - style={defaultStyles.checkboxContainer} - > - - {isCompleted && } - - {isCompleted ? 'Completed' : 'Not completed'} - + -