- 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>
51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
import axios from 'axios';
|
|
|
|
const CIVICRM_SITE_URL = process.env.CIVICRM_SITE_URL;
|
|
const CIVICRM_API_KEY = process.env.CIVICRM_API_KEY;
|
|
const CIVICRM_SITE_KEY = process.env.CIVICRM_SITE_KEY;
|
|
|
|
const civicrmClient = axios.create({
|
|
baseURL: `${CIVICRM_SITE_URL}/civicrm/ajax/api4`,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Civi-Auth': `Bearer ${CIVICRM_API_KEY}`, // Note: Auth strategy might vary (AuthX, etc.)
|
|
'X-Civi-Key': CIVICRM_SITE_KEY,
|
|
},
|
|
});
|
|
|
|
// Using AuthX style if preferred by the user's environment,
|
|
// but APIv4 REST usually uses these or similar headers.
|
|
// We'll stick to a standard implementation that can be adjusted.
|
|
|
|
export async function civicrmApi(entity, action, params = {}) {
|
|
try {
|
|
const response = await civicrmClient.post(`/${entity}/${action}`, {
|
|
params,
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error(`CiviCRM API Error (${entity}.${action}):`, error.response?.data || error.message);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export async function getOrganizationStage(orgId) {
|
|
const result = await civicrmApi('Contact', 'get', {
|
|
select: ['custom_stage_field'], // Replace with actual field name
|
|
where: [['id', '=', orgId]],
|
|
});
|
|
return result.values[0]?.custom_stage_field || 0;
|
|
}
|
|
|
|
export async function updateContact(contactId, values) {
|
|
return await civicrmApi('Contact', 'save', {
|
|
records: [{ id: contactId, ...values }],
|
|
});
|
|
}
|
|
|
|
export async function createActivity(params) {
|
|
return await civicrmApi('Activity', 'create', {
|
|
values: params,
|
|
});
|
|
}
|