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

@@ -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,110 +0,0 @@
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 [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!");
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").insert({
title,
description,
isCompleted,
lastChanged: new Date().toISOString(),
deadline,
uId: data.user.id,
});
if (dbError) {
Alert.alert("Task could not be created, please try again");
SetIsSaving(false);
return;
}
Alert.alert("Task successfully added!");
SetTitle('');
SetDescription('');
SetIsCompleted(false);
SetDeadline('');
SetIsSaving(false);
router.back();
}
return (
<>
<Stack.Screen
options={{
title: "Create Task",
headerTitleStyle: defaultStyles.title
}}
/>
<View style={defaultStyles.container}>
<Text style={defaultStyles.title}>Create New 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="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={AddNote} disabled={isSaving} />
{isSaving && (
<ActivityIndicator size="large" />
)}
<Button title="Cancel" onPress={() => router.back()} />
</View>
</TouchableWithoutFeedback>
</KeyboardAvoidingView>
</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>
)}