import { useState, useEffect } from 'react'; import { useNavigate, useSearchParams } from 'react-router-dom'; import Layout from '../../components/Layout'; import FileAttachment from '../../components/FileAttachment'; import { serviceTypes } from '../../data/mockData'; import { supabase } from '../../lib/supabase'; import { sendEmail } from '../../lib/email'; import { useAuth } from '../../context/AuthContext'; export default function NewRequest() { const { currentUser } = useAuth(); const navigate = useNavigate(); const [searchParams] = useSearchParams(); const preselectedProject = searchParams.get('project') || ''; const [existingProjects, setExistingProjects] = useState([]); const [submitted, setSubmitted] = useState(false); const [saving, setSaving] = useState(false); const [form, setForm] = useState({ project: preselectedProject, serviceType: '', title: '', deadline: '', description: '' }); const [files, setFiles] = useState([]); const [customProjects, setCustomProjects] = useState([]); const [isTypingProject, setIsTypingProject] = useState(false); const [newProjectName, setNewProjectName] = useState(''); useEffect(() => { async function load() { if (!currentUser.company_id) return; const { data: p } = await supabase .from('projects') .select('id, name') .eq('company_id', currentUser.company_id) .order('created_at', { ascending: false }); setExistingProjects((p || []).map(pr => ({ id: pr.id, name: pr.name }))); } load(); }, [currentUser.company_id]); const allProjectNames = [ ...existingProjects.map(p => p.name), ...customProjects.filter(name => !existingProjects.some(p => p.name === name)), ]; const set = (field) => (e) => setForm(f => ({ ...f, [field]: e.target.value })); const handleProjectSelect = (e) => { if (e.target.value === '__new__') { setIsTypingProject(true); setForm(f => ({ ...f, project: '' })); } else { setForm(f => ({ ...f, project: e.target.value })); } }; const handleAddProject = () => { const name = newProjectName.trim(); if (!name) return; if (!customProjects.includes(name) && !existingProjects.some(p => p.name === name)) { setCustomProjects(prev => [...prev, name]); } setForm(f => ({ ...f, project: name })); setIsTypingProject(false); setNewProjectName(''); }; const handleSubmit = async (e) => { e.preventDefault(); if (!currentUser.company_id) { alert('Your account is not yet assigned to a company. Please contact support.'); return; } setSaving(true); // Find existing project by name within this company, or create new one let projectId; const existing = existingProjects.find(p => p.name === form.project); if (existing) { projectId = existing.id; } else { const { data: newProject } = await supabase.from('projects').insert({ company_id: currentUser.company_id, name: form.project, status: 'active', }).select().single(); projectId = newProject?.id; } if (!projectId) { setSaving(false); return; } // Create task const { data: task } = await supabase.from('tasks').insert({ project_id: projectId, title: form.title.trim() || form.serviceType, status: 'not_started', current_version: 0, }).select().single(); if (!task) { setSaving(false); return; } // Create submission const { data: submission } = await supabase.from('submissions').insert({ task_id: task.id, version_number: 1, type: 'initial', service_type: form.serviceType, deadline: form.deadline || null, description: form.description, submitted_by: currentUser.id, submitted_by_name: currentUser.name, }).select().single(); // Upload files if (submission && files.length > 0) { for (const file of files) { const path = `${task.id}/${Date.now()}_${file.name}`; const { data: uploaded } = await supabase.storage.from('submissions').upload(path, file); if (uploaded) { await supabase.from('submission_files').insert({ submission_id: submission.id, name: file.name, storage_path: path, size: file.size, }); } } } sendEmail('new_request', 'hello@fourgebranding.com', { clientName: currentUser.name, clientEmail: currentUser.email, company: currentUser.company?.name || '', serviceType: form.serviceType, projectName: form.project, deadline: form.deadline, description: form.description, taskId: task.id, }); setSaving(false); setSubmitted(true); }; if (!currentUser.company_id) { return (
⚠️

Account Not Yet Active

Your account hasn't been linked to a company yet. Please contact the Fourge team to get set up.

); } if (submitted) { return (

Request Submitted!

Thanks, {currentUser?.name?.split(' ')[0]}! We've received your request for {form.serviceType} {form.project && <> under {form.project}}. Our team will review it and update you shortly.

); } return (
New Request
Tell us what you need and we'll get to work.
{isTypingProject ? (
setNewProjectName(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') { e.preventDefault(); handleAddProject(); } }} autoFocus style={{ flex: 1 }} />
) : ( )}