import { defaultStyles } from '@/constants/defaultStyles'; import { supabase } from '@/lib/supabase'; import { router, Stack, useLocalSearchParams } from 'expo-router'; import { useState } from 'react'; import { ActivityIndicator, Alert, Keyboard, KeyboardAvoidingView, Platform, Pressable, ScrollView, 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() === '') { 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("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(''); SetIsCompleted(false); SetIsSaving(false); router.back(); } return ( <> Create New Assignment Title Description Deadline SetIsCompleted((current) => !current)} > {isCompleted && ( )} {isCompleted ? 'Completed' : 'Not completed'} {isSaving ? 'Saving...' : 'Save Changes'} {isSaving && ( )} router.back()} disabled={isSaving} > Cancel ); }