Merge branch 'backup-my-local-work'

This commit is contained in:
Teodor
2026-04-22 00:27:25 +02:00
16 changed files with 1521 additions and 280 deletions

View File

@@ -38,7 +38,8 @@
"backgroundColor": "#000000"
}
}
]
],
"expo-secure-store"
],
"experiments": {
"typedRoutes": true,

View File

@@ -34,8 +34,10 @@ export default function TabLayout() {
return (
<Tabs>
<Tabs.Screen name="index" options={{title: "Today"}} />
<Tabs.Screen name="index" options={{title: "Index"}} />
<Tabs.Screen name="tasks" options={{title: "Tasks"}} />
<Tabs.Screen name="assignments" options={{title: "Assignments"}} />
<Tabs.Screen name="subjects" options={{title: "Subjects"}} />
</Tabs>
);
}

147
app/(tabs)/assignments.tsx Normal file
View File

@@ -0,0 +1,147 @@
import { defaultStyles } from "@/constants/defaultStyles";
import { supabase } from "@/lib/supabase";
import { Ionicons } from '@expo/vector-icons';
import { Session } from "@supabase/supabase-js";
import { router, Stack, useFocusEffect } from "expo-router";
import { useCallback, useEffect, useState } from "react";
import { Alert, Button, Pressable, SectionList, Text, View } from "react-native";
type Assignment = {
aId: string;
title: string;
description: string;
deadline: string;
isCompleted: boolean;
lastChanged: string;
uId: string;
sId: string;
}
export default function Assignments() {
const [assignments, SetAssignments] = useState<Assignment[]>([])
const [session, SetSession] = useState<Session | null>(null)
const assignmentSections = [
{ title: "Upcoming Assignments", data: assignments.filter((assignment) => !assignment.isCompleted), emptyMessage: "No upcoming assignments" },
{ title: "Completed Assignments", data: assignments.filter((assignment) => assignment.isCompleted), emptyMessage: "No completed assignments" },
];
useEffect(() => {
supabase.auth.getSession().then(({ data }) => SetSession(data.session ?? null))
const { data: sub } = supabase.auth.onAuthStateChange((_event, newSession) => {
SetSession(newSession)
})
return () => sub.subscription.unsubscribe()
},
[])
const GetAssignments = async () => {
const { data, error } = await supabase.from("assignments").select("*").order("deadline", { ascending: false });
if (error) {
Alert.alert("Assignments could not be fetched, please try again");
return;
}
SetAssignments(data ?? []);
}
useFocusEffect(
useCallback(() => {
if (session) {
GetAssignments();
}
}, [session])
);
const DeleteAssignment = async (aId: string) => {
Alert.alert(
"Delete Assignment",
"Are you sure you want to delete this assignment?",
[
{
text: "Cancel",
style: "cancel"
},
{
text: "Delete",
style: "destructive",
onPress: async () => {
const { error } = await supabase.from("assignments").delete().eq("aId", aId);
if (error) {
Alert.alert("Assignment could not be deleted, please try again");
return;
}
Alert.alert("Assignment deleted successfully!");
GetAssignments();
}
}
]
)
}
return (
<View style={defaultStyles.container}>
<Stack.Screen
options={{
title: "Assignments",
headerTitleStyle: defaultStyles.title,
headerRight: () => {
return (
<View style={defaultStyles.buttonContainer}>
<Pressable style={defaultStyles.circularButton} onPress={GetAssignments}>
<Ionicons name="refresh" size={22} color="#333" />
</Pressable>
<Button title="Logout" onPress={async () => await supabase.auth.signOut()} />
</View>
)
},
}}
/>
<View style={defaultStyles.buttonContainer}>
<Button title="Create Assignment" onPress={() => router.push("/assignment/createAssignment")} />
</View>
<SectionList
sections={assignmentSections}
keyExtractor={(item) => item.aId}
renderSectionHeader={({ section: { title } }) => <Text style={defaultStyles.subtitle}>{title}</Text>}
renderItem={({ item }) => {
const isOwner = session?.user.id === item.uId;
return (
<View style={defaultStyles.container}>
<Pressable style={defaultStyles.container} onPress={() => router.push({pathname: "/assignment/viewDetailsAssignment", params: { aId: item.aId }})}>
<Text style={defaultStyles.boldBody}>{item.title}</Text>
<Text style={defaultStyles.body}>{item.deadline}</Text>
<View style={defaultStyles.checkbox}>
{item.isCompleted && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
</Pressable>
{isOwner && (
<View style={defaultStyles.buttonContainer}>
<Button title="Edit" onPress={() => router.push({pathname: "/assignment/editAssignment", params: { aId: item.aId }})} />
<Button title="Delete" onPress={() => DeleteAssignment(item.aId)} />
</View>
)}
</View>
);
}}
renderSectionFooter={({ section }) =>
section.data.length === 0 ? (
<View style={defaultStyles.container}>
<Text style={defaultStyles.body}>{section.emptyMessage}</Text>
<View style={defaultStyles.separator} />
</View>
) : (
<View style={defaultStyles.separator} />
)
}
/>
</View>
)
}

View File

@@ -1,245 +0,0 @@
import { supabase } from '@/lib/supabase';
import {
router,
Stack,
useFocusEffect,
useLocalSearchParams,
} from 'expo-router';
import { useCallback, useState } from 'react';
import {
ActivityIndicator,
Alert,
Keyboard,
KeyboardAvoidingView,
Platform,
Pressable,
ScrollView,
Text,
TextInput,
TouchableWithoutFeedback,
View,
} from 'react-native';
export default function EditTask() {
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 (!taskId) return;
setIsLoading(true);
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);
}
};
getTask();
}, [taskId])
);
const handleSaveTask = async () => {
if (!title.trim() || !description.trim() || !deadline.trim()) {
Alert.alert('All fields are required!');
return;
}
setIsSaving(true);
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: title.trim(),
description: description.trim(),
isCompleted,
lastChanged: new Date().toISOString(),
deadline: deadline.trim(),
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);
}
};
return (
<>
<Stack.Screen
options={{
title: 'Edit Task',
headerTitleStyle: {
fontSize: 20,
fontWeight: '700',
},
}}
/>
<KeyboardAvoidingView
className="flex-1 bg-gray-100"
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<ScrollView
keyboardShouldPersistTaps="handled"
contentContainerClassName="flex-grow justify-center px-5 py-8"
>
<View className="rounded-3xl bg-white p-6 shadow-lg">
<Text className="mb-1 text-3xl font-bold text-gray-900">
Edit Task
</Text>
<Text className="mb-6 text-base text-gray-500">
Update the details for this task.
</Text>
{isLoading ? (
<View className="items-center justify-center py-12">
<ActivityIndicator size="large" />
<Text className="mt-3 text-gray-500">Loading task...</Text>
</View>
) : (
<>
<View className="mb-4">
<Text className="mb-2 text-sm font-semibold text-gray-700">
Title
</Text>
<TextInput
className="rounded-xl border border-gray-300 bg-gray-50 px-4 py-3 text-base text-gray-900"
placeholder="Enter title"
placeholderTextColor="#9ca3af"
value={title}
onChangeText={setTitle}
/>
</View>
<View className="mb-4">
<Text className="mb-2 text-sm font-semibold text-gray-700">
Description
</Text>
<TextInput
className="min-h-28 rounded-xl border border-gray-300 bg-gray-50 px-4 py-3 text-base text-gray-900"
placeholder="Enter description"
placeholderTextColor="#9ca3af"
value={description}
onChangeText={setDescription}
multiline
textAlignVertical="top"
/>
</View>
<View className="mb-4">
<Text className="mb-2 text-sm font-semibold text-gray-700">
Deadline
</Text>
<TextInput
className="rounded-xl border border-gray-300 bg-gray-50 px-4 py-3 text-base text-gray-900"
placeholder="YYYY-MM-DD"
placeholderTextColor="#9ca3af"
value={deadline}
onChangeText={setDeadline}
/>
</View>
<Pressable
className={`mb-6 flex-row items-center rounded-xl border p-4 ${
isCompleted
? 'border-blue-600 bg-blue-50'
: 'border-gray-300 bg-gray-50'
}`}
onPress={() => setIsCompleted((current) => !current)}
>
<View
className={`mr-3 h-6 w-6 items-center justify-center rounded-md border-2 ${
isCompleted
? 'border-blue-600 bg-blue-600'
: 'border-gray-400 bg-white'
}`}
>
{isCompleted && (
<Text className="text-base font-bold text-white"></Text>
)}
</View>
<Text className="text-base font-semibold text-gray-900">
{isCompleted ? 'Completed' : 'Not completed'}
</Text>
</Pressable>
<Pressable
className={`h-14 items-center justify-center rounded-2xl ${
isSaving ? 'bg-blue-400' : 'bg-blue-600'
}`}
onPress={handleSaveTask}
disabled={isSaving}
>
<Text className="text-base font-bold text-white">
{isSaving ? 'Saving...' : 'Save Changes'}
</Text>
</Pressable>
{isSaving && (
<View className="mt-4">
<ActivityIndicator size="small" />
</View>
)}
<Pressable
className="mt-3 h-14 items-center justify-center rounded-2xl bg-gray-200"
onPress={() => router.back()}
disabled={isSaving}
>
<Text className="text-base font-bold text-gray-900">
Cancel
</Text>
</Pressable>
</>
)}
</View>
</ScrollView>
</TouchableWithoutFeedback>
</KeyboardAvoidingView>
</>
);
}

View File

@@ -8,7 +8,7 @@ export default function HomeScreen() {
<View style={defaultStyles.container}>
<Stack.Screen
options={{
title: "Tasks",
title: "Home",
headerTitleStyle: defaultStyles.title,
headerRight: () => {
return (

144
app/(tabs)/subjects.tsx Normal file
View File

@@ -0,0 +1,144 @@
import { defaultStyles } from "@/constants/defaultStyles";
import { supabase } from "@/lib/supabase";
import { Ionicons } from '@expo/vector-icons';
import { Session } from "@supabase/supabase-js";
import { router, Stack, useFocusEffect } from "expo-router";
import { useCallback, useEffect, useState } from "react";
import { Alert, Button, Pressable, SectionList, Text, View } from "react-native";
type Subject = {
sId: string;
title: string;
description: string;
isActive: boolean;
lastChanged: string;
uId: string;
}
export default function Subjects() {
const [subjects, SetSubject] = useState<Subject[]>([])
const [session, SetSession] = useState<Session | null>(null)
const subjectSections = [
{ title: "Active Subjects", data: subjects.filter((subject) => !subject.isActive), emptyMessage: "No active subjects" },
{ title: "Inactive Subjects", data: subjects.filter((subject) => subject.isActive), emptyMessage: "No inactive subjects" },
];
useEffect(() => {
supabase.auth.getSession().then(({ data }) => SetSession(data.session ?? null))
const { data: sub } = supabase.auth.onAuthStateChange((_event, newSession) => {
SetSession(newSession)
})
return () => sub.subscription.unsubscribe()
},
[])
const GetSubjects = async () => {
const { data, error } = await supabase.from("subjects").select("*");
if (error) {
Alert.alert("Subjects could not be fetched, please try again");
return;
}
SetSubject(data ?? []);
}
useFocusEffect(
useCallback(() => {
if (session) {
GetSubjects();
}
}, [session])
);
const DeleteSubject = async (sId: string) => {
Alert.alert(
"Delete Subject",
"Are you sure you want to delete this subject?",
[
{
text: "Cancel",
style: "cancel"
},
{
text: "Delete",
style: "destructive",
onPress: async () => {
const { error } = await supabase.from("subjects").delete().eq("sId", sId);
if (error) {
Alert.alert("Subject could not be deleted, please try again");
return;
}
Alert.alert("Subject deleted successfully!");
GetSubjects();
}
}
]
)
}
return (
<View style={defaultStyles.container}>
<Stack.Screen
options={{
title: "Subjects",
headerTitleStyle: defaultStyles.title,
headerRight: () => {
return (
<View style={defaultStyles.buttonContainer}>
<Pressable style={defaultStyles.circularButton} onPress={GetSubjects}>
<Ionicons name="refresh" size={22} color="#333" />
</Pressable>
<Button title="Logout" onPress={async () => await supabase.auth.signOut()} />
</View>
)
},
}}
/>
<View style={defaultStyles.buttonContainer}>
<Button title="Create Subject" onPress={() => router.push("/subject/createSubject")} />
</View>
<SectionList
sections={subjectSections}
keyExtractor={(item) => item.sId}
renderSectionHeader={({ section: { title } }) => <Text style={defaultStyles.subtitle}>{title}</Text>}
renderItem={({ item }) => {
const isOwner = session?.user.id === item.uId;
return (
<View style={defaultStyles.container}>
<Pressable style={defaultStyles.buttonContainer} onPress={() => router.push({pathname: "/subject/viewDetailsSubject", params: { sId: item.sId }})}>
<Text style={defaultStyles.title}>{item.title}</Text>
<View style={defaultStyles.checkbox}>
{item.isActive && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
</Pressable>
{isOwner && (
<View style={defaultStyles.buttonContainer}>
<Button title="Edit" onPress={() => router.push({pathname: "/subject/editSubject", params: { sId: item.sId }})} />
<Button title="Delete" onPress={() => DeleteSubject(item.sId)} />
</View>
)}
</View>
);
}}
renderSectionFooter={({ section }) =>
section.data.length === 0 ? (
<View style={defaultStyles.container}>
<Text style={defaultStyles.body}>{section.emptyMessage}</Text>
<View style={defaultStyles.separator} />
</View>
) : (
<View style={defaultStyles.separator} />
)
}
/>
</View>
)
}

View File

@@ -12,8 +12,8 @@ type Task = {
description: string;
isCompleted: boolean;
lastChanged: string;
deadline: string;
uId: string;
aId: string;
}
export default function Tasks() {
@@ -34,6 +34,17 @@ export default function Tasks() {
},
[])
const GetTasks = async () => {
const { data, error } = await supabase.from("tasks").select("*");
if (error) {
Alert.alert("Tasks could not be fetched, please try again");
return;
}
SetTasks(data ?? []);
}
useFocusEffect(
useCallback(() => {
if (session) {
@@ -42,17 +53,6 @@ export default function Tasks() {
}, [session])
);
const GetTasks = async () => {
const { data, error } = await supabase.from("tasks").select("tId, title, description, isCompleted,lastChanged, deadline, uId").order("deadline", { ascending: false });
if (error) {
Alert.alert("Task could not be fetched, please try again");
return;
}
SetTasks(data ?? []);
}
const DeleteTask = async (tId: string) => {
Alert.alert(
"Delete Task",
@@ -101,7 +101,7 @@ export default function Tasks() {
/>
<View style={defaultStyles.buttonContainer}>
<Button title="Create Task" onPress={() => router.push("/createTask")} />
<Button title="Create Task" onPress={() => router.push("/task/createTask")} />
</View>
<SectionList
@@ -113,12 +113,16 @@ export default function Tasks() {
return (
<View style={defaultStyles.container}>
<Text style={defaultStyles.boldBody}>{item.title}</Text>
<Text style={defaultStyles.body}>{item.deadline}</Text>
<Pressable style={defaultStyles.container} onPress={() => router.push({pathname: "/task/viewDetailsTask", params: { tId: item.tId }})}>
<Text style={defaultStyles.boldBody}>{item.title}</Text>
<View style={defaultStyles.checkbox}>
{item.isCompleted && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
</Pressable>
{isOwner && (
<View style={defaultStyles.buttonContainer}>
<Button title="Edit" onPress={() => router.push({pathname: "/editTask", params: { tId: item.tId }})} />
<Button title="Edit" onPress={() => router.push({pathname: "/task/editTask", params: { tId: item.tId }})} />
<Button title="Delete" onPress={() => DeleteTask(item.tId)} />
</View>
)}

View File

@@ -0,0 +1,112 @@
import { supabase } from '@/lib/supabase';
import { router, Stack, useLocalSearchParams } from 'expo-router';
import { useState } from 'react';
import { ActivityIndicator, Alert, Button, Keyboard, KeyboardAvoidingView, Platform, Pressable, Text, TextInput, TouchableWithoutFeedback, View } from 'react-native';
export default function CreateAssignment() {
const sId = (useLocalSearchParams().sId as string) ?? null;
const [title, SetTitle] = useState('');
const [description, SetDescription] = useState('');
const [deadline, SetDeadline] = useState('');
const [isCompleted, SetIsCompleted] = useState(false);
const [isSaving, SetIsSaving] = useState(false);
const CreateAssignment = async () => {
if(title.trim() === '' || deadline.trim() === '') {
Alert.alert("Title and deadline are required!");
return;
}
setIsSaving(true);
try {
const { data: userData, error: userError } = await supabase.auth.getUser();
if (userError || !userData.user) {
router.replace('../createUser');
return;
}
SetIsSaving(true);
const { error: dbError } = await supabase.from("assignments").insert({
title,
description,
deadline,
isCompleted,
lastChanged: new Date().toISOString(),
uId: data.user.id,
sId: sId,
});
if (dbError) {
Alert.alert("Assignment could not be created, please try again");
return;
}
Alert.alert("Assignment successfully created!");
SetTitle('');
SetDescription('');
SetDeadline('');
SetIsCompleted(false);
SetIsSaving(false);
router.back();
}
return (
<>
<Stack.Screen
options={{
title: "Create Assignment",
headerTitleStyle: defaultStyles.title
}}
/>
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Create New Assignment</Text>
<KeyboardAvoidingView style={{ flex: 1 }} behavior={Platform.OS === "ios" ? "padding" : "height"}>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={defaultStyles.container}>
<TextInput
style={defaultStyles.inputText}
placeholder="Enter title"
value={title}
onChangeText={SetTitle}
/>
<TextInput
style={defaultStyles.inputText}
placeholder="Enter description"
value={description}
onChangeText={SetDescription}
/>
<TextInput
style={defaultStyles.inputText}
placeholder="Deadline (YYYY-MM-DD)"
value={deadline}
onChangeText={SetDeadline}
/>
<Pressable
onPress={() => SetIsCompleted(state => !state)}
style={defaultStyles.checkboxContainer}
>
<View style={defaultStyles.checkbox}>
{isCompleted && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
<Text style={defaultStyles.checkboxLabel}>{isCompleted ? 'Completed' : 'Not completed'}</Text>
</Pressable>
<Button title={isSaving ? "Saving..." : "Save"} onPress={CreateAssignment} disabled={isSaving} />
{isSaving && (
<ActivityIndicator size="large" />
)}
<Button title="Cancel" onPress={() => router.back()} />
</View>
</ScrollView>
</TouchableWithoutFeedback>
</KeyboardAvoidingView>
</>
);
}

View File

@@ -0,0 +1,143 @@
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 Assignment = {
aId: string;
title: string;
description: string;
deadline: string;
isCompleted: boolean;
lastChanged: string;
uId: string;
sId: string;
}
export default function EditAssignment() {
const { aId } = useLocalSearchParams<{ aId: string }>();
const [assignment, SetAssignment] = useState<Assignment | null>(null)
const [isSaving, SetIsSaving] = useState(false);
const GetAssignment = async (aId: string) => {
const { data, error } = await supabase.from("assignments").select("*").eq("aId", aId).single();
if (error) {
Alert.alert("Assignment could not be fetched, please try again");
return;
}
SetAssignment(data ?? null);
}
useFocusEffect(
useCallback(() => {
if (aId) {
GetAssignment(aId);
}
}, [aId])
);
const EditAssignment = async () => {
if (!assignment) return;
if(assignment.title.trim() === '' || assignment.deadline.trim() === '') {
Alert.alert("Title and deadline are 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("assignments").update({
title: assignment.title,
description: assignment.description,
deadline: assignment.deadline,
isCompleted: assignment.isCompleted,
lastChanged: new Date().toISOString(),
uId: data.user.id,
sId: assignment.sId,
}).eq("aId", aId);
SetIsSaving(false);
if (dbError) {
Alert.alert("Assignment could not be edited, please try again");
return;
}
Alert.alert("Assignment successfully edited!");
router.back();
}
return (
<View style={defaultStyles.container}>
<Stack.Screen
options={{
title: "Edit Assignment",
headerTitleStyle: defaultStyles.title
}}
/>
{!assignment && (
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Assignment not found</Text>
</View>
)}
{assignment && (
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Edit Assignment</Text>
<KeyboardAvoidingView style={{ flex: 1 }} behavior={Platform.OS === "ios" ? "padding" : "height"}>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={defaultStyles.container}>
<TextInput
style={defaultStyles.inputText}
placeholder="Title"
value={assignment.title}
onChangeText={(text) => SetAssignment(prev => prev ? { ...prev, title: text } : prev)}
/>
<TextInput
style={defaultStyles.inputText}
placeholder="Text"
value={assignment.description}
onChangeText={(text) => SetAssignment(prev => prev ? { ...prev, description: text } : prev)}
/>
<TextInput
style={defaultStyles.inputText}
placeholder="Text"
value={assignment.deadline}
onChangeText={(text) => SetAssignment(prev => prev ? { ...prev, deadline: text } : prev)}
/>
<Pressable
onPress={() => SetAssignment(prev => prev ? { ...prev, isCompleted: !prev.isCompleted } : prev)}
style={defaultStyles.checkboxContainer}
>
<View style={defaultStyles.checkbox}>
{assignment.isCompleted && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
<Text style={defaultStyles.checkboxLabel}>{assignment.isCompleted ? 'Completed' : 'Not Completed'}</Text>
</Pressable>
<Button title={isSaving ? "Saving..." : "Save"} onPress={EditAssignment} disabled={isSaving} />
{isSaving && (
<ActivityIndicator size="large" />
)}
<Button title="Cancel" onPress={() => router.back()} />
</View>
</TouchableWithoutFeedback>
</KeyboardAvoidingView>
</View>
)}
</View>
)
}

View File

@@ -0,0 +1,224 @@
import { defaultStyles } from '@/constants/defaultStyles';
import { supabase } from '@/lib/supabase';
import { Session } from '@supabase/supabase-js';
import { router, Stack, useFocusEffect, useLocalSearchParams } from 'expo-router';
import { useCallback, useEffect, useState } from 'react';
import { Alert, Button, Pressable, SectionList, Text, View } from "react-native";
type Assignment = {
aId: string;
title: string;
description: string;
deadline: string;
isCompleted: boolean;
lastChanged: string;
uId: string;
sId: string;
}
type Task = {
tId: string;
title: string;
description: string;
isCompleted: boolean;
lastChanged: string;
uId: string;
aId: string;
}
export default function ViewDetailsAssignment() {
const { aId } = useLocalSearchParams<{ aId: string }>();
const [assignment, SetAssignment] = useState<Assignment | null>(null)
const [tasks, SetTasks] = useState<Task[]>([])
const [session, SetSession] = useState<Session | null>(null)
const taskSections = [
{ title: "Upcoming Tasks", data: tasks.filter((task) => !task.isCompleted), emptyMessage: "No upcoming tasks" },
{ title: "Completed Tasks", data: tasks.filter((task) => task.isCompleted), emptyMessage: "No completed tasks" },
];
useEffect(() => {
supabase.auth.getSession().then(({ data }) => SetSession(data.session ?? null))
const { data: sub } = supabase.auth.onAuthStateChange((_event, newSession) => {
SetSession(newSession)
})
return () => sub.subscription.unsubscribe()
},
[])
const GetAssignment = async (aId: string) => {
const { data, error } = await supabase.from("assignments").select("*").eq("aId", aId).single();
if (error) {
Alert.alert("Assignment could not be fetched, please try again");
return;
}
SetAssignment(data ?? null);
}
const GetTasks = async (aId: string) => {
const { data, error } = await supabase.from("tasks").select("*").eq("aId", aId);
if (error) {
Alert.alert("Tasks could not be fetched, please try again");
return;
}
SetTasks(data ?? []);
}
useFocusEffect(
useCallback(() => {
if (session && aId) {
GetAssignment(aId);
GetTasks(aId);
}
}, [session, aId])
);
const DeleteAssignment = async (aId: string) => {
Alert.alert(
"Delete Assignment",
"Are you sure you want to delete this assignment?",
[
{
text: "Cancel",
style: "cancel"
},
{
text: "Delete",
style: "destructive",
onPress: async () => {
const { error } = await supabase.from("assignments").delete().eq("aId", aId);
if (error) {
Alert.alert("Assignment could not be deleted, please try again");
return;
}
Alert.alert("Assignment deleted successfully!");
router.back();
}
}
]
)
}
const DeleteTask = async (tId: string, aId: string) => {
Alert.alert(
"Delete Task",
"Are you sure you want to delete this task?",
[
{
text: "Cancel",
style: "cancel"
},
{
text: "Delete",
style: "destructive",
onPress: async () => {
const { error } = await supabase.from("tasks").delete().eq("tId", tId);
if (error) {
Alert.alert("Task could not be deleted, please try again");
return;
}
Alert.alert("Task deleted successfully!");
GetTasks(aId);
}
}
]
)
}
return (
<View style={defaultStyles.container}>
<Stack.Screen
options={{
title: "Details",
headerTitleStyle: defaultStyles.title,
headerLeft: () => {
return (
<View style={defaultStyles.buttonContainer}>
<Button title="Back" onPress={router.back} />
</View>
)
},
headerRight: () => {
return (
<View style={defaultStyles.buttonContainer}>
<Button title="Logout" onPress={async () => await supabase.auth.signOut()} />
</View>
)
},
}}
/>
{!assignment && (
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Assignment not found</Text>
</View>
)}
{assignment && (
<View style={defaultStyles.container}>
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>{assignment.title}</Text>
<Text style={defaultStyles.body}>{assignment.description}</Text>
<Text style={defaultStyles.body}>{assignment.deadline}</Text>
<View style={defaultStyles.checkbox}>
{assignment.isCompleted && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
<Text style={defaultStyles.body}>{assignment.lastChanged}</Text>
<Button title="Edit" onPress={() => router.push({pathname: "/assignment/editAssignment", params: { aId: assignment.aId }})} />
<Button title="Delete" onPress={() => DeleteAssignment(assignment.aId)} />
</View>
<View style={defaultStyles.buttonContainer}>
<Button title="Create Task" onPress={() => router.push({pathname: "/task/createTask", params: { aId: assignment.aId }})} />
</View>
<SectionList
sections={taskSections}
keyExtractor={(item) => item.tId}
renderSectionHeader={({ section: { title } }) => <Text style={defaultStyles.subtitle}>{title}</Text>}
renderItem={({ item }) => {
const isOwner = session?.user.id === item.uId;
return (
<View style={defaultStyles.container}>
<Pressable style={defaultStyles.container} onPress={() => router.push({pathname: "/task/viewDetailsTask", params: { tId: item.tId }})}>
<Text style={defaultStyles.boldBody}>{item.title}</Text>
<View style={defaultStyles.checkbox}>
{item.isCompleted && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
</Pressable>
{isOwner && (
<View style={defaultStyles.buttonContainer}>
<Button title="Edit" onPress={() => router.push({pathname: "/task/editTask", params: { tId: item.tId }})} />
<Button title="Delete" onPress={() => DeleteTask(item.tId, item.tId)} />
</View>
)}
</View>
);
}}
renderSectionFooter={({ section }) =>
section.data.length === 0 ? (
<View style={defaultStyles.container}>
<Text style={defaultStyles.body}>{section.emptyMessage}</Text>
<View style={defaultStyles.separator} />
</View>
) : (
<View style={defaultStyles.separator} />
)
}
/>
</View>
)}
</View>
);
}

View File

@@ -0,0 +1,100 @@
import { defaultStyles } from '@/constants/defaultStyles';
import { supabase } from '@/lib/supabase';
import { router, Stack } from 'expo-router';
import { useState } from 'react';
import { ActivityIndicator, Alert, Button, Keyboard, KeyboardAvoidingView, Platform, Pressable, Text, TextInput, TouchableWithoutFeedback, View } from 'react-native';
export default function CreateTask() {
const [title, SetTitle] = useState('');
const [description, SetDescription] = useState('');
const [isActive, SetIsActive] = useState(true);
const [isSaving, SetIsSaving] = useState(false);
const CreateSubject = async () => {
if(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("subjects").insert({
title,
description,
isActive,
lastChanged: new Date().toISOString(),
uId: data.user.id,
});
if (dbError) {
Alert.alert("Subject could not be created, please try again");
return;
}
Alert.alert("Subject successfully created!");
SetTitle('');
SetDescription('');
SetIsActive(false);
SetIsSaving(false);
router.back();
}
return (
<>
<Stack.Screen
options={{
title: "Create Subject",
headerTitleStyle: defaultStyles.title
}}
/>
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Create New Subject</Text>
<KeyboardAvoidingView style={{ flex: 1 }} behavior={Platform.OS === "ios" ? "padding" : "height"}>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={defaultStyles.container}>
<TextInput
style={defaultStyles.inputText}
placeholder="Enter title"
value={title}
onChangeText={SetTitle}
/>
<TextInput
style={defaultStyles.inputText}
placeholder="Enter description"
value={description}
onChangeText={SetDescription}
/>
<Pressable
onPress={() => SetIsActive(state => !state)}
style={defaultStyles.checkboxContainer}
>
<View style={defaultStyles.checkbox}>
{isActive && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
<Text style={defaultStyles.checkboxLabel}>{isActive ? 'Active' : 'Inactive'}</Text>
</Pressable>
<Button title={isSaving ? "Saving..." : "Save"} onPress={CreateSubject} disabled={isSaving} />
{isSaving && (
<ActivityIndicator size="large" />
)}
<Button title="Cancel" onPress={() => router.back()} />
</View>
</TouchableWithoutFeedback>
</KeyboardAvoidingView>
</View>
</>
)
}

133
app/subject/editSubject.tsx Normal file
View File

@@ -0,0 +1,133 @@
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 Subject = {
sId: string;
title: string;
description: string;
isActive: boolean;
lastChanged: string;
uId: string;
}
export default function EditSubject() {
const { sId } = useLocalSearchParams<{ sId: string }>();
const [subject, SetSubject] = useState<Subject | null>(null)
const [isSaving, SetIsSaving] = useState(false);
const GetSubject = async (sId: string) => {
const { data, error } = await supabase.from("subjects").select("*").eq("sId", sId).single();
if (error) {
Alert.alert("Subject could not be fetched, please try again");
return;
}
SetSubject(data ?? null);
}
useFocusEffect(
useCallback(() => {
if (sId) {
GetSubject(sId);
}
}, [sId])
);
const EditSubject = async () => {
if (!subject) return;
if(subject.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("subjects").update({
title: subject.title,
description: subject.description,
isActive: subject.isActive,
lastChanged: new Date().toISOString(),
uId: data.user.id,
}).eq("sId", sId);
SetIsSaving(false);
if (dbError) {
Alert.alert("Subject could not be edited, please try again");
return;
}
Alert.alert("Subject successfully edited!");
router.back();
}
return (
<View style={defaultStyles.container}>
<Stack.Screen
options={{
title: "Edit Subject",
headerTitleStyle: defaultStyles.title
}}
/>
{!subject && (
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Subject not found</Text>
</View>
)}
{subject && (
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Edit Subject</Text>
<KeyboardAvoidingView style={{ flex: 1 }} behavior={Platform.OS === "ios" ? "padding" : "height"}>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={defaultStyles.container}>
<TextInput
style={defaultStyles.inputText}
placeholder="Title"
value={subject.title}
onChangeText={(text) => SetSubject(prev => prev ? { ...prev, title: text } : prev)}
/>
<TextInput
style={defaultStyles.inputText}
placeholder="Text"
value={subject.description}
onChangeText={(text) => SetSubject(prev => prev ? { ...prev, description: text } : prev)}
/>
<Pressable
onPress={() => SetSubject(prev => prev ? { ...prev, isActive: !prev.isActive } : prev)}
style={defaultStyles.checkboxContainer}
>
<View style={defaultStyles.checkbox}>
{subject.isActive && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
<Text style={defaultStyles.checkboxLabel}>{subject.isActive ? 'Active' : 'inactive'}</Text>
</Pressable>
<Button title={isSaving ? "Saving..." : "Save"} onPress={EditSubject} disabled={isSaving} />
{isSaving && (
<ActivityIndicator size="large" />
)}
<Button title="Cancel" onPress={() => router.back()} />
</View>
</TouchableWithoutFeedback>
</KeyboardAvoidingView>
</View>
)}
</View>
)
}

View File

@@ -0,0 +1,223 @@
import { defaultStyles } from '@/constants/defaultStyles';
import { supabase } from '@/lib/supabase';
import { Session } from '@supabase/supabase-js';
import { router, Stack, useFocusEffect, useLocalSearchParams } from 'expo-router';
import { useCallback, useEffect, useState } from 'react';
import { Alert, Button, Pressable, SectionList, Text, View } from "react-native";
type Subject = {
sId: string;
title: string;
description: string;
isActive: boolean;
lastChanged: string;
uId: string;
}
type Assignment = {
aId: string;
title: string;
description: string;
deadline: string;
isCompleted: boolean;
lastChanged: string;
uId: string;
sId: string;
}
export default function ViewDetailsSubject() {
const { sId } = useLocalSearchParams<{ sId: string }>();
const [subject, SetSubject] = useState<Subject | null>(null)
const [assignments, SetAssignments] = useState<Assignment[]>([])
const [session, SetSession] = useState<Session | null>(null)
const assignmentSections = [
{ title: "Upcoming Assignments", data: assignments.filter((assignment) => !assignment.isCompleted), emptyMessage: "No upcoming assignments" },
{ title: "Completed Assignments", data: assignments.filter((assignment) => assignment.isCompleted), emptyMessage: "No completed assignments" },
];
useEffect(() => {
supabase.auth.getSession().then(({ data }) => SetSession(data.session ?? null))
const { data: sub } = supabase.auth.onAuthStateChange((_event, newSession) => {
SetSession(newSession)
})
return () => sub.subscription.unsubscribe()
},
[])
const GetSubject = async (sId: string) => {
const { data, error } = await supabase.from("subjects").select("*").eq("sId", sId).single();
if (error) {
Alert.alert("Subject could not be fetched, please try again");
return;
}
SetSubject(data ?? null);
}
const GetAssignments = async (sId: string) => {
const { data, error } = await supabase.from("assignments").select("*").eq("sId", sId).order("deadline", { ascending: true });
if (error) {
Alert.alert("Assignments could not be fetched, please try again");
return;
}
SetAssignments(data ?? []);
}
useFocusEffect(
useCallback(() => {
if (session && sId) {
GetSubject(sId);
GetAssignments(sId);
}
}, [session, sId])
);
const DeleteSubject = async (sId: string) => {
Alert.alert(
"Delete Subject",
"Are you sure you want to delete this subject?",
[
{
text: "Cancel",
style: "cancel"
},
{
text: "Delete",
style: "destructive",
onPress: async () => {
const { error } = await supabase.from("subjects").delete().eq("sId", sId);
if (error) {
Alert.alert("Subject could not be deleted, please try again");
return;
}
Alert.alert("Subject deleted successfully!");
router.back();
}
}
]
)
}
const DeleteAssignment = async (aId: string, sId: string) => {
Alert.alert(
"Delete Assignment",
"Are you sure you want to delete this assignment?",
[
{
text: "Cancel",
style: "cancel"
},
{
text: "Delete",
style: "destructive",
onPress: async () => {
const { error } = await supabase.from("assignments").delete().eq("aId", aId);
if (error) {
Alert.alert("Assignment could not be deleted, please try again");
return;
}
Alert.alert("Assignment deleted successfully!");
GetAssignments(sId);
}
}
]
)
}
return (
<View style={defaultStyles.container}>
<Stack.Screen
options={{
title: "Details",
headerTitleStyle: defaultStyles.title,
headerLeft: () => {
return (
<View style={defaultStyles.buttonContainer}>
<Button title="Back" onPress={router.back} />
</View>
)
},
headerRight: () => {
return (
<View style={defaultStyles.buttonContainer}>
<Button title="Logout" onPress={async () => await supabase.auth.signOut()} />
</View>
)
},
}}
/>
{!subject && (
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Subject not found</Text>
</View>
)}
{subject && (
<View style={defaultStyles.container}>
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>{subject.title}</Text>
<Text style={defaultStyles.body}>{subject.description}</Text>
<View style={defaultStyles.checkbox}>
{subject.isActive && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
<Text style={defaultStyles.body}>{subject.lastChanged}</Text>
<Button title="Edit" onPress={() => router.push({pathname: "/subject/editSubject", params: { sId: subject.sId }})} />
<Button title="Delete" onPress={() => DeleteSubject(subject.sId)} />
<View style={defaultStyles.buttonContainer}>
<Button title="Create Assignment" onPress={() => router.push({pathname: "/assignment/createAssignment", params: { sId: subject.sId }})} />
</View>
</View>
<SectionList
sections={assignmentSections}
keyExtractor={(item) => item.aId}
renderSectionHeader={({ section: { title } }) => <Text style={defaultStyles.subtitle}>{title}</Text>}
renderItem={({ item }) => {
const isOwner = session?.user.id === item.uId;
return (
<View style={defaultStyles.container}>
<Pressable style={defaultStyles.container} onPress={() => router.push({pathname: "/assignment/viewDetailsAssignment", params: { aId: item.aId }})}>
<Text style={defaultStyles.boldBody}>{item.title}</Text>
<Text style={defaultStyles.body}>{item.deadline}</Text>
<View style={defaultStyles.checkbox}>
{item.isCompleted && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
</Pressable>
{isOwner && (
<View style={defaultStyles.buttonContainer}>
<Button title="Edit" onPress={() => router.push({pathname: "/assignment/editAssignment", params: { aId: item.aId }})} />
<Button title="Delete" onPress={() => DeleteAssignment(item.aId, item.sId)} />
</View>
)}
</View>
);
}}
renderSectionFooter={({ section }) =>
section.data.length === 0 ? (
<View style={defaultStyles.container}>
<Text style={defaultStyles.body}>{section.emptyMessage}</Text>
<View style={defaultStyles.separator} />
</View>
) : (
<View style={defaultStyles.separator} />
)
}
/>
</View>
)}
</View>
);
}

View File

@@ -1,19 +1,19 @@
import { defaultStyles } from '@/constants/defaultStyles';
import { supabase } from '@/lib/supabase';
import { router, Stack } from 'expo-router';
import { router, Stack, useLocalSearchParams } from 'expo-router';
import { useState } from 'react';
import { ActivityIndicator, Alert, Button, Keyboard, KeyboardAvoidingView, Platform, Pressable, Text, TextInput, TouchableWithoutFeedback, View } from 'react-native';
export default function CreateTask() {
const aId = (useLocalSearchParams().aId as string) ?? null;
const [title, SetTitle] = useState('');
const [description, SetDescription] = useState('');
const [isCompleted, SetIsCompleted] = useState(false);
const [deadline, SetDeadline] = useState('');
const [isSaving, SetIsSaving] = useState(false);
const AddNote = async () => {
if(title.trim() === '' || description.trim() === '' || deadline.trim() === '') {
Alert.alert("All fields are required!");
const CreateTask = async () => {
if(title.trim() === '') {
Alert.alert("Title is required!");
return;
}
@@ -31,22 +31,20 @@ export default function CreateTask() {
description,
isCompleted,
lastChanged: new Date().toISOString(),
deadline,
uId: data.user.id,
aId: aId,
});
if (dbError) {
Alert.alert("Task could not be created, please try again");
SetIsSaving(false);
return;
}
Alert.alert("Task successfully added!");
Alert.alert("Task successfully created!");
SetTitle('');
SetDescription('');
SetIsCompleted(false);
SetDeadline('');
SetIsSaving(false);
@@ -79,12 +77,7 @@ export default function CreateTask() {
value={description}
onChangeText={SetDescription}
/>
<TextInput
style={defaultStyles.inputText}
placeholder="Deadline (YYYY-MM-DD)"
value={deadline}
onChangeText={SetDeadline}
/>
<Pressable
onPress={() => SetIsCompleted(state => !state)}
style={defaultStyles.checkboxContainer}
@@ -95,7 +88,7 @@ export default function CreateTask() {
<Text style={defaultStyles.checkboxLabel}>{isCompleted ? 'Completed' : 'Not completed'}</Text>
</Pressable>
<Button title={isSaving ? "Saving..." : "Save"} onPress={AddNote} disabled={isSaving} />
<Button title={isSaving ? "Saving..." : "Save"} onPress={CreateTask} disabled={isSaving} />
{isSaving && (
<ActivityIndicator size="large" />
)}

135
app/task/editTask.tsx Normal file
View File

@@ -0,0 +1,135 @@
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<Task | null>(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 (
<View style={defaultStyles.container}>
<Stack.Screen
options={{
title: "Edit Task",
headerTitleStyle: defaultStyles.title
}}
/>
{!task && (
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Task not found</Text>
</View>
)}
{task && (
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Edit Task</Text>
<KeyboardAvoidingView style={{ flex: 1 }} behavior={Platform.OS === "ios" ? "padding" : "height"}>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={defaultStyles.container}>
<TextInput
style={defaultStyles.inputText}
placeholder="Title"
value={task.title}
onChangeText={(text) => SetTask(prev => prev ? { ...prev, title: text } : prev)}
/>
<TextInput
style={defaultStyles.inputText}
placeholder="Text"
value={task.description}
onChangeText={(text) => SetTask(prev => prev ? { ...prev, description: text } : prev)}
/>
<Pressable
onPress={() => SetTask(prev => prev ? { ...prev, isCompleted: !prev.isCompleted } : prev)}
style={defaultStyles.checkboxContainer}
>
<View style={defaultStyles.checkbox}>
{task.isCompleted && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
<Text style={defaultStyles.checkboxLabel}>{task.isCompleted ? 'Completed' : 'Not Completed'}</Text>
</Pressable>
<Button title={isSaving ? "Saving..." : "Save"} onPress={EditTask} disabled={isSaving} />
{isSaving && (
<ActivityIndicator size="large" />
)}
<Button title="Cancel" onPress={() => router.back()} />
</View>
</TouchableWithoutFeedback>
</KeyboardAvoidingView>
</View>
)}
</View>
)
}

View File

@@ -0,0 +1,125 @@
import { defaultStyles } from '@/constants/defaultStyles';
import { supabase } from '@/lib/supabase';
import { Session } from '@supabase/supabase-js';
import { router, Stack, useFocusEffect, useLocalSearchParams } from 'expo-router';
import { useCallback, useEffect, useState } from 'react';
import { Alert, Button, Text, View } from "react-native";
type Task = {
tId: string;
title: string;
description: string;
isCompleted: boolean;
lastChanged: string;
uId: string;
aId: string;
}
export default function ViewDetailsTask() {
const { tId } = useLocalSearchParams<{ tId: string }>();
const [task, SetTask] = useState<Task | null>(null)
const [session, SetSession] = useState<Session | null>(null)
useEffect(() => {
supabase.auth.getSession().then(({ data }) => SetSession(data.session ?? null))
const { data: sub } = supabase.auth.onAuthStateChange((_event, newSession) => {
SetSession(newSession)
})
return () => sub.subscription.unsubscribe()
},
[])
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 (session && tId) {
GetTask(tId);
}
}, [session, tId])
);
const DeleteTask = async (tId: string) => {
Alert.alert(
"Delete Task",
"Are you sure you want to delete this task?",
[
{
text: "Cancel",
style: "cancel"
},
{
text: "Delete",
style: "destructive",
onPress: async () => {
const { error } = await supabase.from("tasks").delete().eq("tId", tId);
if (error) {
Alert.alert("Task could not be deleted, please try again");
return;
}
Alert.alert("Task deleted successfully!");
router.back();
}
}
]
)
}
return (
<View style={defaultStyles.container}>
<Stack.Screen
options={{
title: "Details",
headerTitleStyle: defaultStyles.title,
headerLeft: () => {
return (
<View style={defaultStyles.buttonContainer}>
<Button title="Back" onPress={router.back} />
</View>
)
},
headerRight: () => {
return (
<View style={defaultStyles.buttonContainer}>
<Button title="Logout" onPress={async () => await supabase.auth.signOut()} />
</View>
)
},
}}
/>
{!task && (
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Task not found</Text>
</View>
)}
{task && (
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>{task.title}</Text>
<Text style={defaultStyles.body}>{task.description}</Text>
<View style={defaultStyles.checkbox}>
{task.isCompleted && <Text style={defaultStyles.checkboxMark}></Text>}
</View>
<Text style={defaultStyles.body}>{task.lastChanged}</Text>
<View style={defaultStyles.buttonContainer}>
<Button title="Edit" onPress={() => router.push({pathname: "/task/editTask", params: { tId: task.tId }})} />
<Button title="Delete" onPress={() => DeleteTask(task.tId)} />
</View>
</View>
)}
</View>
);
}