import React, { useState } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from './ui/card'; import { Input } from './ui/input'; import { Button } from './ui/button'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from './ui/select'; import { Textarea } from './ui/textarea'; import { AlertCircle } from 'lucide-react'; import { useAuth } from '../services/AuthContext'; const OpportunityForm = () => { // Generate a simple ID using timestamp and random number const generateId = () => `opp-${crypto.randomUUID()}`; const initialFormState = { opportunityId: generateId(), customerName: '', opportunityName: '', opportunityState: '', opportunityDescription: '', opportunityValue: '', closeDate: '', customerContact: '', customerContactRole: '', activity: '', nextSteps: '' }; const [formData, setFormData] = useState(initialFormState); const [isSubmitting, setIsSubmitting] = useState(false); const [errors, setErrors] = useState({}); const { token } = useAuth(); const handleChange = (e) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); // Clear error when field is modified if (errors[name]) { setErrors(prev => ({ ...prev, [name]: '' })); } }; const handleSelectChange = (value) => { setFormData(prev => ({ ...prev, opportunityState: value })); if (errors.opportunityState) { setErrors(prev => ({ ...prev, opportunityState: '' })); } }; const validateForm = () => { const newErrors = {}; if (!formData.customerName.trim()) newErrors.customerName = 'Customer name is required'; if (!formData.opportunityName.trim()) newErrors.opportunityName = 'Opportunity name is required'; if (!formData.opportunityState) newErrors.opportunityState = 'Opportunity state is required'; if (!formData.opportunityDescription.trim()) newErrors.opportunityDescription = 'Description is required'; if (!formData.opportunityValue) newErrors.opportunityValue = 'Value is required'; if (!formData.closeDate) newErrors.closeDate = 'Close date is required'; if (!formData.customerContact.trim()) newErrors.customerContact = 'Customer contact is required'; if (!formData.customerContactRole.trim()) newErrors.customerContactRole = 'Contact role is required'; if (!formData.activity.trim()) newErrors.activity = 'Activity is required'; if (!formData.nextSteps.trim()) newErrors.nextSteps = 'Next steps are required'; setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = async (e) => { e.preventDefault(); if (!validateForm()) { return; } setIsSubmitting(true); try { const response = await fetch('/api/save_opportunity', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, body: JSON.stringify(formData), }); if (!response.ok) { throw new Error('Submission failed'); } handleClear(); alert('Form submitted successfully!'); } catch (error) { alert('Error submitting form: ' + error.message); } finally { setIsSubmitting(false); } }; const handleClear = () => { setFormData({ ...initialFormState, opportunityId: generateId() }); setErrors({}); }; const FormLabel = ({ children, required }) => (
{children} {required && *}
); const ErrorMessage = ({ message }) => message ? (
{message}
) : null; return ( New Opportunity
Customer Name
Opportunity Name
Opportunity State
Opportunity Description