Implement CiviCRM middleware form with stage-based visibility
- Initialize Next.js project with Tailwind CSS - Create CiviCRM APIv4 integration layer - Implement stage-based form visibility (stages 0-5) - Add field mapping configuration for CRM-to-form linking - Create API routes for data retrieval and submission - Record form submissions as CiviCRM activities - Support dynamic contactId and orgId via URL parameters - Ensure robust form state management with react-hook-form Co-authored-by: joelbrock <52835+joelbrock@users.noreply.github.com>
This commit is contained in:
60
app/api/data/route.js
Normal file
60
app/api/data/route.js
Normal file
@@ -0,0 +1,60 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { civicrmApi } from '@/lib/civicrm';
|
||||
import mapping from '@/config/mapping.json';
|
||||
|
||||
export async function GET(request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const orgId = searchParams.get('orgId');
|
||||
const contactId = searchParams.get('contactId');
|
||||
|
||||
if (!orgId || !contactId) {
|
||||
return NextResponse.json({ error: 'Missing orgId or contactId' }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. Fetch current stage from Organization (Contact record with type Organization)
|
||||
const orgResult = await civicrmApi('Contact', 'get', {
|
||||
select: [mapping.stageField],
|
||||
where: [['id', '=', parseInt(orgId)]],
|
||||
});
|
||||
|
||||
const currentStage = orgResult.values[0]?.[mapping.stageField] || 0;
|
||||
|
||||
// 2. Prepare select fields for pre-filling contact data
|
||||
const selectFields = ['id'];
|
||||
mapping.stages.forEach(stage => {
|
||||
stage.fields.forEach(field => {
|
||||
if (field.entity === 'Contact') {
|
||||
selectFields.push(field.crmField);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 3. Fetch contact data for pre-filling
|
||||
const contactResult = await civicrmApi('Contact', 'get', {
|
||||
select: selectFields,
|
||||
where: [['id', '=', parseInt(contactId)]],
|
||||
});
|
||||
|
||||
const contactData = contactResult.values[0] || {};
|
||||
|
||||
return NextResponse.json({
|
||||
currentStage: parseInt(currentStage),
|
||||
prefillData: contactData,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('API Error:', error);
|
||||
// Return mock data if CiviCRM is not reachable (for development/demo)
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
return NextResponse.json({
|
||||
currentStage: 2,
|
||||
prefillData: {
|
||||
first_name: 'John',
|
||||
last_name: 'Doe',
|
||||
'email_primary.email': 'john@example.com'
|
||||
}
|
||||
});
|
||||
}
|
||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user