diff --git a/app/assignment/viewDetailsAssignment.tsx b/app/assignment/viewDetailsAssignment.tsx index 1f1ff43..7cd9f0d 100644 --- a/app/assignment/viewDetailsAssignment.tsx +++ b/app/assignment/viewDetailsAssignment.tsx @@ -1,4 +1,5 @@ import { CheckAssignmentCompletion, CheckSubjectCompletion } from '@/lib/progress'; +import { getSubjectColorSet, type SubjectColor } from '@/lib/subjectColors'; import { supabase } from '@/lib/supabase'; import type { Assignment, Task } from '@/lib/types'; import { Session } from '@supabase/supabase-js'; @@ -6,11 +7,14 @@ import { router, Stack, useFocusEffect, useLocalSearchParams } from 'expo-router import { useCallback, useEffect, useState } from 'react'; import { Alert, Pressable, SectionList, Text, View } from "react-native"; + export default function ViewDetailsAssignment() { const { aId } = useLocalSearchParams<{ aId: string }>(); - const [assignment, SetAssignment] = useState(null) - const [tasks, SetTasks] = useState([]) - const [session, SetSession] = useState(null) + const [assignment, SetAssignment] = useState(null); + const [tasks, SetTasks] = useState([]); + const [session, SetSession] = useState(null); + const [subjectColor, setSubjectColor] = useState('slate'); + const [subjectTitle, setSubjectTitle] = useState('No Subject'); const taskSections = [ { title: "Upcoming Tasks", data: tasks.filter((task) => !task.isCompleted), emptyMessage: "No upcoming tasks" }, @@ -26,16 +30,39 @@ export default function ViewDetailsAssignment() { }, []) - const GetAssignment = async (aId: string) => { - const { data, error } = await supabase.from("assignments").select("*").eq("aId", aId).single(); + const GetAssignment = async (assignmentId: string) => { + const { data, error } = await supabase + .from('assignments') + .select('*') + .eq('aId', assignmentId) + .single(); - if (error) { - Alert.alert("Assignment could not be fetched, please try again"); + if (error || !data) { + console.log('GetAssignment error:', error); + Alert.alert('Assignment could not be fetched, please try again'); return; } - SetAssignment(data ?? null); - } + SetAssignment(data); + + if (data.sId) { + const { data: subjectData, error: subjectError } = await supabase + .from('subjects') + .select('title, color') + .eq('sId', data.sId) + .single(); + + if (subjectError || !subjectData) { + console.log('GetSubjectMeta error:', subjectError); + setSubjectTitle('Unknown Subject'); + setSubjectColor('slate'); + return; + } + + setSubjectTitle(subjectData.title ?? 'Unknown Subject'); + setSubjectColor((subjectData.color as SubjectColor | undefined) ?? 'slate'); + } + }; const GetTasks = async (aId: string) => { const { data, error } = await supabase.from("tasks").select("*").eq("aId", aId); @@ -132,7 +159,9 @@ export default function ViewDetailsAssignment() { ] ) } - + + const colorSet = getSubjectColorSet(subjectColor); + const completedTasks = tasks.filter((task) => task.isCompleted).length; const totalTasks = tasks.length; const remainingTasks = totalTasks - completedTasks; @@ -151,7 +180,13 @@ export default function ViewDetailsAssignment() { }} /> - + Assignment not found @@ -170,6 +205,7 @@ export default function ViewDetailsAssignment() { ); } + return ( - + {assignment.isCompleted && ( @@ -223,14 +265,34 @@ export default function ViewDetailsAssignment() { ) : null} + + + + {subjectTitle} + + + + Deadline: {assignment.deadline || 'No deadline'} - - + + {assignment.isCompleted ? 'Completed' : 'In progress'} @@ -252,7 +314,7 @@ export default function ViewDetailsAssignment() { className="h-full rounded-full" style={{ width: `${progress}%`, - backgroundColor: progress === 100 ? '#34D399' : '#3B82F6', + backgroundColor: colorSet.strong, }} /> @@ -324,7 +386,13 @@ export default function ViewDetailsAssignment() { const isOwner = session?.user.id === item.uId; return ( - + router.push({ @@ -335,11 +403,11 @@ export default function ViewDetailsAssignment() { > {item.isCompleted && ( diff --git a/lib/subjectColors.ts b/lib/subjectColors.ts index 141297f..1ffe774 100644 --- a/lib/subjectColors.ts +++ b/lib/subjectColors.ts @@ -50,4 +50,9 @@ export const SUBJECT_COLORS: Record< export const SUBJECT_COLOR_KEYS = Object.keys( SUBJECT_COLORS -) as SubjectColor[]; \ No newline at end of file +) as SubjectColor[]; + +export const getSubjectColorSet = (color?: SubjectColor) => { + const colorKey: SubjectColor = color ?? 'slate'; + return SUBJECT_COLORS[colorKey]; +}; \ No newline at end of file