|
| 1 | +import { useState } from 'react'; |
| 2 | +import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '../ui/dialog'; |
| 3 | +import { Button } from '../ui/button'; |
| 4 | +import { Input } from '../ui/input'; |
| 5 | +import { Label } from '../ui/label'; |
| 6 | +import { Alert, AlertDescription } from '../ui/alert'; |
| 7 | +import { AlertCircle, Building2 } from 'lucide-react'; |
| 8 | +import { Company } from '../../../shared/lib/types'; |
| 9 | +import { useCompany } from '../../contexts/CompanyContext'; |
| 10 | +import { useToast } from '../ui/use-toast'; |
| 11 | +import { createCompany } from '../../utils/api'; |
| 12 | + |
| 13 | +interface CompanyRegistrationModalProps { |
| 14 | + isOpen: boolean; |
| 15 | + onClose: () => void; |
| 16 | + onSuccess?: (company: Company) => void; |
| 17 | +} |
| 18 | + |
| 19 | +export function CompanyRegistrationModal({ isOpen, onClose, onSuccess }: CompanyRegistrationModalProps) { |
| 20 | + const [companyName, setCompanyName] = useState(''); |
| 21 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 22 | + const [error, setError] = useState(''); |
| 23 | + const { setUserCompanies, userCompanies, setSelectedCompany, refreshUserCompanies } = useCompany(); |
| 24 | + const { toast } = useToast(); |
| 25 | + |
| 26 | + const handleSubmit = async (e: React.FormEvent) => { |
| 27 | + e.preventDefault(); |
| 28 | + |
| 29 | + if (!companyName.trim()) { |
| 30 | + setError('Company name is required'); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + setIsSubmitting(true); |
| 35 | + setError(''); |
| 36 | + |
| 37 | + try { |
| 38 | + console.log('🏢 Creating company via API...'); |
| 39 | + |
| 40 | + const newCompany = await createCompany({ |
| 41 | + company_name: companyName.trim(), |
| 42 | + company_type: 'originator', // Default to originator, user can change later |
| 43 | + description: `${companyName.trim()} company created via portfolio registration`, |
| 44 | + }); |
| 45 | + |
| 46 | + console.log('✅ Company created successfully:', newCompany); |
| 47 | + |
| 48 | + // Add to user companies and select it |
| 49 | + const updatedCompanies = [...userCompanies, newCompany]; |
| 50 | + setUserCompanies(updatedCompanies); |
| 51 | + setSelectedCompany(newCompany); |
| 52 | + |
| 53 | + // Refresh companies from API to ensure consistency |
| 54 | + console.log('🔄 Refreshing companies after creation...'); |
| 55 | + try { |
| 56 | + await refreshUserCompanies(); |
| 57 | + } catch (refreshError) { |
| 58 | + console.warn('⚠️ Could not refresh companies after creation:', refreshError); |
| 59 | + } |
| 60 | + |
| 61 | + toast({ |
| 62 | + title: 'Company Created', |
| 63 | + description: `${companyName} has been successfully registered.`, |
| 64 | + }); |
| 65 | + |
| 66 | + if (onSuccess) { |
| 67 | + onSuccess(newCompany); |
| 68 | + } |
| 69 | + |
| 70 | + // Reset form and close modal |
| 71 | + setCompanyName(''); |
| 72 | + onClose(); |
| 73 | + } catch (error: any) { |
| 74 | + console.error('❌ Error creating company:', error); |
| 75 | + const errorMessage = error?.message || 'Failed to create company. Please try again.'; |
| 76 | + setError(errorMessage); |
| 77 | + } finally { |
| 78 | + setIsSubmitting(false); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + const handleClose = () => { |
| 83 | + if (!isSubmitting) { |
| 84 | + setCompanyName(''); |
| 85 | + setError(''); |
| 86 | + onClose(); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + return ( |
| 91 | + <Dialog open={isOpen} onOpenChange={handleClose}> |
| 92 | + <DialogContent className="sm:max-w-md"> |
| 93 | + <DialogHeader> |
| 94 | + <div className="flex items-center gap-3"> |
| 95 | + <div className="flex h-10 w-10 items-center justify-center rounded-full bg-primary/10"> |
| 96 | + <Building2 className="h-5 w-5 text-primary" /> |
| 97 | + </div> |
| 98 | + <div> |
| 99 | + <DialogTitle>Create Your Company</DialogTitle> |
| 100 | + <DialogDescription> |
| 101 | + Register your company to access the portfolio features and manage deals. |
| 102 | + </DialogDescription> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + </DialogHeader> |
| 106 | + |
| 107 | + <form onSubmit={handleSubmit} className="space-y-4"> |
| 108 | + <div className="space-y-2"> |
| 109 | + <Label htmlFor="companyName">Company Name</Label> |
| 110 | + <Input |
| 111 | + id="companyName" |
| 112 | + type="text" |
| 113 | + placeholder="Enter your company name" |
| 114 | + value={companyName} |
| 115 | + onChange={(e) => setCompanyName(e.target.value)} |
| 116 | + disabled={isSubmitting} |
| 117 | + className="w-full" |
| 118 | + autoFocus |
| 119 | + /> |
| 120 | + </div> |
| 121 | + |
| 122 | + {error && ( |
| 123 | + <Alert variant="destructive"> |
| 124 | + <AlertCircle className="h-4 w-4" /> |
| 125 | + <AlertDescription>{error}</AlertDescription> |
| 126 | + </Alert> |
| 127 | + )} |
| 128 | + |
| 129 | + <div className="flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2 space-y-2 space-y-reverse sm:space-y-0"> |
| 130 | + <Button |
| 131 | + type="button" |
| 132 | + variant="outline" |
| 133 | + onClick={handleClose} |
| 134 | + disabled={isSubmitting} |
| 135 | + > |
| 136 | + Cancel |
| 137 | + </Button> |
| 138 | + <Button type="submit" disabled={isSubmitting || !companyName.trim()}> |
| 139 | + {isSubmitting ? 'Creating...' : 'Create Company'} |
| 140 | + </Button> |
| 141 | + </div> |
| 142 | + </form> |
| 143 | + |
| 144 | + <div className="text-sm text-muted-foreground"> |
| 145 | + <p>You can update company details and settings after creation.</p> |
| 146 | + </div> |
| 147 | + </DialogContent> |
| 148 | + </Dialog> |
| 149 | + ); |
| 150 | +} |
0 commit comments