@pagamio/frontend-commons-lib
Version:
Pagamio library for Frontend reusable components like the form engine and table container
37 lines (36 loc) • 1.54 kB
JavaScript
/**
* Generate a pre-filled Google Form URL with user data
* @param config - Form configuration with base URL and field mappings
* @param userData - User information to pre-fill in the form
* @returns Pre-filled Google Form URL
*/
export function generateFeedbackFormUrl(config, userData = {}) {
const params = new URLSearchParams();
// Required by Google Forms for pre-filled links
params.append('usp', 'pp_url');
// Add user data to URL parameters
for (const [key, value] of Object.entries(userData)) {
const fieldId = config.fieldIds[key];
if (value != null && fieldId) {
// Encode the value but preserve @ symbol in email addresses
let encodedValue = encodeURIComponent(String(value));
if (key === 'email') {
// Replace encoded @ symbol back to @ for email fields
encodedValue = encodedValue.replaceAll('%40', '@');
}
params.append(fieldId, encodedValue);
}
}
return `${config.baseUrl}?${params.toString()}`;
}
/**
* Open feedback form in a new tab with pre-filled user data
* @param config - Form configuration with base URL and field mappings
* @param userData - User information to pre-fill in the form
* @param onFormOpened - Optional callback after form is opened
*/
export function openFeedbackForm(config, userData = {}, onFormOpened) {
const url = generateFeedbackFormUrl(config, userData);
window.open(url, '_blank', 'noopener,noreferrer');
onFormOpened?.();
}