import { defaultStyles } from '@/constants/defaultStyles'; import { supabase } from '@/lib/supabase'; import { router, Stack } 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 CreateSubject() { 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: title.trim(), description: description.trim(), isActive, lastChanged: new Date().toISOString(), uId: data.user.id, }); if (dbError) { SetIsSaving(false); Alert.alert('Subject could not be created, please try again'); return; } Alert.alert('Subject successfully created!'); SetTitle(''); SetDescription(''); SetIsActive(true); SetIsSaving(false); router.back(); }; const inputClassName = 'rounded-2xl border border-app-border bg-app-subtle px-4 py-3 text-base text-text-main'; const labelClassName = 'mb-2 text-sm font-semibold text-text-secondary'; return ( <> Create Subject Add a subject to organize your assignments and study tasks. Title Description SetIsActive((state) => !state)} disabled={isSaving} className={`mb-6 flex-row items-center rounded-2xl border p-4 ${ isActive ? 'border-accent bg-accent-soft' : 'border-app-border bg-app-subtle' }`} > {isActive && ( )} Active subject Active subjects appear in your main study workflow. {isSaving ? ( Creating... ) : ( Create Subject )} router.back()} disabled={isSaving} > Cancel ); }