Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions app/companies/register/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,69 @@ function CompanyRegisterContent() {
console.error("Registration error:", error);
};

// Check if user already has a company
useEffect(() => {
const checkExistingCompany = async () => {
console.log('🔍 Checking for existing company...', { user: !!user, resubmitId });

if (!user || resubmitId) {
console.log('⏭️ Skipping check - user:', !!user, 'resubmitId:', resubmitId);
return; // Skip if already in resubmit mode
}

try {
console.log('📡 Fetching /api/companies/me...');
const response = await fetch('/api/companies/me');
console.log('📡 Response status:', response.status);

if (response.ok) {
const result = await response.json();
console.log('📦 API Response:', result);

if (result.companies && result.companies.length > 0) {
console.log('🏢 Found companies:', result.companies.length);

// Get the company where user is owner
const companyMember = result.companies.find((c: { role: string; company: CompanyData }) => c.role === 'owner');
console.log('👤 Owner company member:', companyMember);

// Check if company member exists AND the company object is not null
if (companyMember && companyMember.company && companyMember.company.id) {
const company = companyMember.company; // Extract the actual company data
console.log('✅ Company found - status:', company.verification_status, 'id:', company.id);

// If company is rejected, redirect to resubmit flow
if (company.verification_status === 'rejected') {
console.log('🔄 User has rejected company, redirecting to resubmit flow');
router.push(`/companies/register?resubmit=${company.id}`);
return;
}

// If company is pending or verified, show message
if (company.verification_status === 'pending' || company.verification_status === 'verified') {
console.log('ℹ️ User already has a company');
toast.info('You already have a registered company');
router.push('/protected');
return;
}
} else {
console.log('❌ No valid owner company found (company may have been deleted)');
}
} else {
console.log('📭 No companies found for user');
}
} else {
console.log('❌ API request failed:', response.status);
}
} catch (error) {
console.error('❌ Error checking existing company:', error);
// Continue to show registration form if check fails
}
};

checkExistingCompany();
}, [user, resubmitId, router]);

// Fetch company data for resubmission
useEffect(() => {
const fetchResubmitData = async () => {
Expand Down
21 changes: 18 additions & 3 deletions components/companies/CompanyRegistrationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();

// Only allow submission on the final step
if (currentStep !== totalSteps) {
console.log('Not on final step, preventing submission');
return;
}

if (!validateStep(currentStep)) {
return;
}
Expand Down Expand Up @@ -248,7 +254,7 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa

// Add verification documents
formData.verification_documents.forEach((file, index) => {
submitData.append(`verification_document_${index}`, file);
submitData.append(`verification_document_${index} `, file);
});

const response = await fetch("/api/companies/register", {
Expand Down Expand Up @@ -295,7 +301,15 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa
<Progress value={progress} className="h-2" />
</div>

<form onSubmit={handleSubmit} className="space-y-6">
<form
className="space-y-6"
onKeyDown={(e) => {
// Prevent Enter key from submitting the form unless on final step
if (e.key === 'Enter') {
e.preventDefault();
}
}}
>
{/* Step 1: Company Information */}
{currentStep === 1 && (
<motion.div
Expand Down Expand Up @@ -704,7 +718,8 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa
</Button>
) : (
<Button
type="submit"
type="button"
onClick={handleSubmit}
disabled={isSubmitting}
className="ml-auto bg-gradient-to-r from-primary to-primary/80 hover:from-primary/90 hover:to-primary/70"
>
Expand Down
Loading