Skip to content

Commit 8e2e80f

Browse files
authored
Merge pull request #358 from codeunia-dev/feat/companyregisterationflow
feat: Add pre-registration company check and improve multi-step form submission flow
2 parents bbb0e24 + 234cca3 commit 8e2e80f

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

app/companies/register/page.tsx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,69 @@ function CompanyRegisterContent() {
6060
console.error("Registration error:", error);
6161
};
6262

63+
// Check if user already has a company
64+
useEffect(() => {
65+
const checkExistingCompany = async () => {
66+
console.log('🔍 Checking for existing company...', { user: !!user, resubmitId });
67+
68+
if (!user || resubmitId) {
69+
console.log('⏭️ Skipping check - user:', !!user, 'resubmitId:', resubmitId);
70+
return; // Skip if already in resubmit mode
71+
}
72+
73+
try {
74+
console.log('📡 Fetching /api/companies/me...');
75+
const response = await fetch('/api/companies/me');
76+
console.log('📡 Response status:', response.status);
77+
78+
if (response.ok) {
79+
const result = await response.json();
80+
console.log('📦 API Response:', result);
81+
82+
if (result.companies && result.companies.length > 0) {
83+
console.log('🏢 Found companies:', result.companies.length);
84+
85+
// Get the company where user is owner
86+
const companyMember = result.companies.find((c: { role: string; company: CompanyData }) => c.role === 'owner');
87+
console.log('👤 Owner company member:', companyMember);
88+
89+
// Check if company member exists AND the company object is not null
90+
if (companyMember && companyMember.company && companyMember.company.id) {
91+
const company = companyMember.company; // Extract the actual company data
92+
console.log('✅ Company found - status:', company.verification_status, 'id:', company.id);
93+
94+
// If company is rejected, redirect to resubmit flow
95+
if (company.verification_status === 'rejected') {
96+
console.log('🔄 User has rejected company, redirecting to resubmit flow');
97+
router.push(`/companies/register?resubmit=${company.id}`);
98+
return;
99+
}
100+
101+
// If company is pending or verified, show message
102+
if (company.verification_status === 'pending' || company.verification_status === 'verified') {
103+
console.log('ℹ️ User already has a company');
104+
toast.info('You already have a registered company');
105+
router.push('/protected');
106+
return;
107+
}
108+
} else {
109+
console.log('❌ No valid owner company found (company may have been deleted)');
110+
}
111+
} else {
112+
console.log('📭 No companies found for user');
113+
}
114+
} else {
115+
console.log('❌ API request failed:', response.status);
116+
}
117+
} catch (error) {
118+
console.error('❌ Error checking existing company:', error);
119+
// Continue to show registration form if check fails
120+
}
121+
};
122+
123+
checkExistingCompany();
124+
}, [user, resubmitId, router]);
125+
63126
// Fetch company data for resubmission
64127
useEffect(() => {
65128
const fetchResubmitData = async () => {

components/companies/CompanyRegistrationForm.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa
210210
const handleSubmit = async (e: React.FormEvent) => {
211211
e.preventDefault();
212212

213+
// Only allow submission on the final step
214+
if (currentStep !== totalSteps) {
215+
console.log('Not on final step, preventing submission');
216+
return;
217+
}
218+
213219
if (!validateStep(currentStep)) {
214220
return;
215221
}
@@ -248,7 +254,7 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa
248254

249255
// Add verification documents
250256
formData.verification_documents.forEach((file, index) => {
251-
submitData.append(`verification_document_${index}`, file);
257+
submitData.append(`verification_document_${index} `, file);
252258
});
253259

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

298-
<form onSubmit={handleSubmit} className="space-y-6">
304+
<form
305+
className="space-y-6"
306+
onKeyDown={(e) => {
307+
// Prevent Enter key from submitting the form unless on final step
308+
if (e.key === 'Enter') {
309+
e.preventDefault();
310+
}
311+
}}
312+
>
299313
{/* Step 1: Company Information */}
300314
{currentStep === 1 && (
301315
<motion.div
@@ -704,7 +718,8 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa
704718
</Button>
705719
) : (
706720
<Button
707-
type="submit"
721+
type="button"
722+
onClick={handleSubmit}
708723
disabled={isSubmitting}
709724
className="ml-auto bg-gradient-to-r from-primary to-primary/80 hover:from-primary/90 hover:to-primary/70"
710725
>

0 commit comments

Comments
 (0)