-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
649 lines (556 loc) · 18.8 KB
/
Copy pathscript.js
File metadata and controls
649 lines (556 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// ========================================
// Application State
// ========================================
const state = {
token: null,
contractData: null,
fields: [],
steps: [],
currentStep: 0,
formData: {}
};
// ========================================
// DOM Elements
// ========================================
const elements = {
loadingState: document.getElementById('loading-state'),
errorState: document.getElementById('error-state'),
successState: document.getElementById('success-state'),
formContainer: document.getElementById('form-container'),
formSteps: document.getElementById('form-steps'),
clientInfo: document.getElementById('client-info'),
clientDetails: document.getElementById('client-details'),
progressFill: document.getElementById('progress-fill'),
currentStepDisplay: document.getElementById('current-step'),
totalStepsDisplay: document.getElementById('total-steps'),
prevBtn: document.getElementById('prev-btn'),
nextBtn: document.getElementById('next-btn'),
submitBtn: document.getElementById('submit-btn'),
form: document.getElementById('contract-form'),
errorMessage: document.getElementById('error-message')
};
// ========================================
// Initialization
// ========================================
document.addEventListener('DOMContentLoaded', async () => {
// Extract token from URL
state.token = getTokenFromURL();
if (!state.token) {
showError('Token manquant dans l\'URL. Veuillez utiliser le lien fourni dans votre email.');
return;
}
// Fetch contract data
await fetchContractData();
});
// ========================================
// URL & Token Management
// ========================================
function getTokenFromURL() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('token');
}
// ========================================
// API Calls
// ========================================
async function fetchContractData() {
try {
let data;
// Mode test : charge data-example.json
if (CONFIG.testMode) {
console.log('MODE TEST: Chargement de data-example.json');
const response = await fetch('data-example.json');
if (!response.ok) {
throw new Error(`Erreur lors du chargement du fichier exemple: ${response.status}`);
}
data = await response.json();
} else {
// Mode production : appelle le webhook n8n
const url = `${CONFIG.webhookGetUrl}?token=${state.token}`;
console.log('MODE PRODUCTION: Appel à', url);
const headers = CONFIG.apiKey ? {
'Authorization': `Bearer ${CONFIG.apiKey}`,
'Content-Type': 'application/json'
} : {
'Content-Type': 'application/json'
};
console.log('Headers:', headers);
const response = await fetch(url, {
method: 'GET',
headers: headers
});
console.log('Réponse HTTP:', response.status, response.statusText);
if (!response.ok) {
throw new Error(`Erreur HTTP: ${response.status} - ${response.statusText}`);
}
const rawText = await response.text();
console.log('Réponse brute (100 premiers caractères):', rawText.substring(0, 100));
try {
data = JSON.parse(rawText);
console.log('JSON parsé avec succès:', data);
} catch (parseError) {
console.error('Erreur de parsing JSON:', parseError);
console.error('Réponse complète:', rawText);
throw new Error('La réponse n\'est pas un JSON valide');
}
}
console.log('Données du contrat:', data);
state.contractData = data;
// Support both old format (fields array) and new format (sections array)
if (data.sections) {
console.log('Format sections détecté, nombre de sections:', data.sections.length);
state.sections = data.sections;
} else if (data.fields) {
console.log('Format fields détecté, conversion en section unique');
// Old format: convert to single section
state.sections = [{
id: 'default',
title: 'Informations',
description: '',
fields: data.fields || []
}];
} else {
console.error('Format de données non reconnu:', data);
throw new Error('Format de données invalide : ni sections ni fields trouvés');
}
console.log('Sections chargées:', state.sections);
// Initialize form
initializeForm();
} catch (error) {
console.error('=== ERREUR DÉTAILLÉE ===');
console.error('Message:', error.message);
console.error('Stack:', error.stack);
console.error('========================');
showError(`Impossible de charger le contrat. Erreur: ${error.message}`);
}
}
async function submitFormData() {
try {
// Show loading on submit button
elements.submitBtn.disabled = true;
elements.submitBtn.textContent = 'Envoi en cours...';
const payload = {
token: state.token,
contractId: state.contractData.contractId,
formData: state.formData
};
// Mode test : simule l'envoi et affiche les données dans la console
if (CONFIG.testMode) {
console.log('=== MODE TEST - Données du formulaire ===');
console.log(JSON.stringify(payload, null, 2));
console.log('=========================================');
// Simule un délai réseau
await new Promise(resolve => setTimeout(resolve, 1000));
// Success!
showSuccess();
} else {
// Mode production : envoie vraiment vers n8n
const headers = CONFIG.apiKey ? {
'Authorization': `Bearer ${CONFIG.apiKey}`,
'Content-Type': 'application/json'
} : {
'Content-Type': 'application/json'
};
const response = await fetch(CONFIG.webhookPostUrl, {
method: 'POST',
headers: headers,
body: JSON.stringify(payload)
});
if (!response.ok) {
throw new Error(`Erreur HTTP: ${response.status}`);
}
// Success!
showSuccess();
}
} catch (error) {
console.error('Erreur lors de la soumission:', error);
elements.submitBtn.disabled = false;
elements.submitBtn.textContent = CONFIG.texts.submitButton;
alert('Erreur lors de la soumission. Veuillez réessayer.');
}
}
// ========================================
// Form Initialization
// ========================================
function initializeForm() {
// Display client info if available
if (state.contractData.clientName || state.contractData.clientEmail) {
displayClientInfo();
}
// Create form steps
createFormSteps();
// Hide loading, show form
elements.loadingState.classList.add('hidden');
elements.formContainer.classList.remove('hidden');
// Show first step
showStep(0);
// Attach event listeners
attachEventListeners();
}
function displayClientInfo() {
const { clientName, clientEmail, contractId } = state.contractData;
let html = '';
if (contractId) {
html += `<div class="client-detail"><strong>Contrat N°:</strong> ${contractId}</div>`;
}
if (clientName) {
html += `<div class="client-detail"><strong>Nom:</strong> ${clientName}</div>`;
}
if (clientEmail) {
html += `<div class="client-detail"><strong>Email:</strong> ${clientEmail}</div>`;
}
elements.clientDetails.innerHTML = html;
elements.clientInfo.classList.remove('hidden');
}
function createFormSteps() {
// Use sections as steps
state.steps = state.sections;
// Update total steps display
elements.totalStepsDisplay.textContent = state.steps.length;
// Generate HTML for each section/step
state.steps.forEach((section, stepIndex) => {
const stepDiv = document.createElement('div');
stepDiv.className = 'form-step';
stepDiv.dataset.step = stepIndex;
stepDiv.dataset.sectionId = section.id;
let stepHTML = `
<div class="section-header">
<h2 class="step-title">${section.title}</h2>
${section.description ? `<p class="section-description">${section.description}</p>` : ''}
<div class="step-indicator">Étape ${stepIndex + 1} sur ${state.steps.length}</div>
</div>
`;
section.fields.forEach(field => {
stepHTML += generateFieldHTML(field);
});
stepDiv.innerHTML = stepHTML;
elements.formSteps.appendChild(stepDiv);
});
// Setup conditional fields listeners
setupConditionalFields();
}
function generateFieldHTML(field) {
const required = field.required ? '<span class="required">*</span>' : '';
const value = field.value || '';
const conditionalClass = field.showIf ? 'conditional-field' : '';
const conditionalData = field.showIf ?
`data-show-if-field="${field.showIf.field}" data-show-if-value="${field.showIf.value}"` : '';
let inputHTML = '';
switch (field.type) {
case 'select':
inputHTML = `<select
id="${field.name}"
name="${field.name}"
${field.required ? 'required' : ''}
${field.pattern ? `pattern="${field.pattern}"` : ''}
>
<option value="">-- Sélectionnez --</option>
${field.options.map(opt =>
`<option value="${opt.value}" ${value === opt.value ? 'selected' : ''}>${opt.label}</option>`
).join('')}
</select>`;
break;
case 'email':
inputHTML = `<input
type="email"
id="${field.name}"
name="${field.name}"
value="${value}"
${field.required ? 'required' : ''}
${field.pattern ? `pattern="${field.pattern}"` : ''}
placeholder="${field.placeholder || ''}"
/>`;
break;
case 'number':
inputHTML = `<input
type="number"
id="${field.name}"
name="${field.name}"
value="${value}"
${field.required ? 'required' : ''}
${field.min !== undefined ? `min="${field.min}"` : ''}
${field.max !== undefined ? `max="${field.max}"` : ''}
${field.pattern ? `pattern="${field.pattern}"` : ''}
placeholder="${field.placeholder || ''}"
/>`;
break;
case 'date':
inputHTML = `<input
type="date"
id="${field.name}"
name="${field.name}"
value="${value}"
${field.required ? 'required' : ''}
/>`;
break;
case 'tel':
inputHTML = `<input
type="tel"
id="${field.name}"
name="${field.name}"
value="${value}"
${field.required ? 'required' : ''}
${field.pattern ? `pattern="${field.pattern}"` : ''}
placeholder="${field.placeholder || ''}"
/>`;
break;
case 'textarea':
inputHTML = `<textarea
id="${field.name}"
name="${field.name}"
${field.required ? 'required' : ''}
placeholder="${field.placeholder || ''}"
>${value}</textarea>`;
break;
default: // text
inputHTML = `<input
type="text"
id="${field.name}"
name="${field.name}"
value="${value}"
${field.required ? 'required' : ''}
${field.pattern ? `pattern="${field.pattern}"` : ''}
placeholder="${field.placeholder || ''}"
/>`;
}
return `
<div class="form-field ${conditionalClass}" id="field-${field.name}" ${conditionalData} style="${field.showIf ? 'display: none;' : ''}">
<label for="${field.name}">
${field.label}${required}
</label>
${inputHTML}
<div class="field-error" id="error-${field.name}"></div>
</div>
`;
}
// ========================================
// Conditional Fields
// ========================================
function setupConditionalFields() {
// Find all conditional fields
const conditionalFields = document.querySelectorAll('.conditional-field');
conditionalFields.forEach(fieldDiv => {
const triggerFieldName = fieldDiv.dataset.showIfField;
const triggerValue = fieldDiv.dataset.showIfValue;
const triggerField = document.getElementById(triggerFieldName);
if (triggerField) {
// Initial check
toggleConditionalField(fieldDiv, triggerField, triggerValue);
// Listen for changes
triggerField.addEventListener('change', () => {
toggleConditionalField(fieldDiv, triggerField, triggerValue);
});
}
});
}
function toggleConditionalField(fieldDiv, triggerField, requiredValue) {
const fieldInput = fieldDiv.querySelector('input, select, textarea');
if (triggerField.value === requiredValue) {
fieldDiv.style.display = 'block';
// Make field required if it was originally required
if (fieldInput && fieldInput.hasAttribute('data-originally-required')) {
fieldInput.required = true;
}
} else {
fieldDiv.style.display = 'none';
// Remove validation for hidden fields
if (fieldInput) {
fieldInput.classList.remove('error');
const errorDiv = fieldDiv.querySelector('.field-error');
if (errorDiv) {
errorDiv.classList.remove('visible');
errorDiv.textContent = '';
}
// Store original required state
if (fieldInput.required) {
fieldInput.setAttribute('data-originally-required', 'true');
fieldInput.required = false;
}
}
}
}
// ========================================
// Step Navigation
// ========================================
function showStep(stepIndex) {
state.currentStep = stepIndex;
// Hide all steps
document.querySelectorAll('.form-step').forEach(step => {
step.classList.remove('active');
});
// Show current step
const currentStepElement = document.querySelector(`.form-step[data-step="${stepIndex}"]`);
if (currentStepElement) {
currentStepElement.classList.add('active');
}
// Update progress bar
const progress = ((stepIndex + 1) / state.steps.length) * 100;
elements.progressFill.style.width = `${progress}%`;
elements.currentStepDisplay.textContent = stepIndex + 1;
// Update navigation buttons
updateNavigationButtons();
}
function updateNavigationButtons() {
const isFirstStep = state.currentStep === 0;
const isLastStep = state.currentStep === state.steps.length - 1;
// Previous button
if (isFirstStep) {
elements.prevBtn.classList.add('hidden');
} else {
elements.prevBtn.classList.remove('hidden');
}
// Next vs Submit button
if (isLastStep) {
elements.nextBtn.classList.add('hidden');
elements.submitBtn.classList.remove('hidden');
} else {
elements.nextBtn.classList.remove('hidden');
elements.submitBtn.classList.add('hidden');
}
}
function goToNextStep() {
// Validate current step
if (!validateCurrentStep()) {
return;
}
// Save current step data
saveCurrentStepData();
// Move to next step
if (state.currentStep < state.steps.length - 1) {
showStep(state.currentStep + 1);
}
}
function goToPreviousStep() {
// Save current step data (no validation needed)
saveCurrentStepData();
// Move to previous step
if (state.currentStep > 0) {
showStep(state.currentStep - 1);
}
}
function saveCurrentStepData() {
const currentSection = state.steps[state.currentStep];
currentSection.fields.forEach(field => {
const input = document.getElementById(field.name);
if (input) {
state.formData[field.name] = input.value;
}
});
}
// ========================================
// Validation
// ========================================
function validateCurrentStep() {
const currentSection = state.steps[state.currentStep];
let isValid = true;
currentSection.fields.forEach(field => {
const fieldDiv = document.getElementById(`field-${field.name}`);
const input = document.getElementById(field.name);
const errorDiv = document.getElementById(`error-${field.name}`);
// Skip validation for hidden fields
if (fieldDiv && fieldDiv.style.display === 'none') {
return;
}
if (!input) return;
// Clear previous errors
input.classList.remove('error');
errorDiv.classList.remove('visible');
errorDiv.textContent = '';
// Required field validation
if (field.required && input.required && !input.value.trim()) {
showFieldError(input, errorDiv, 'Ce champ est requis');
isValid = false;
return;
}
// Pattern validation
if (input.value.trim() && field.pattern) {
const pattern = new RegExp(field.pattern);
if (!pattern.test(input.value)) {
showFieldError(input, errorDiv, 'Format invalide');
isValid = false;
return;
}
}
// Type-specific validation
if (input.value.trim()) {
switch (field.type) {
case 'email':
if (!isValidEmail(input.value)) {
showFieldError(input, errorDiv, 'Email invalide');
isValid = false;
}
break;
case 'number':
const num = parseFloat(input.value);
if (isNaN(num)) {
showFieldError(input, errorDiv, 'Nombre invalide');
isValid = false;
} else if (field.min !== undefined && num < field.min) {
showFieldError(input, errorDiv, `Minimum: ${field.min}`);
isValid = false;
} else if (field.max !== undefined && num > field.max) {
showFieldError(input, errorDiv, `Maximum: ${field.max}`);
isValid = false;
}
break;
case 'tel':
if (!isValidPhone(input.value)) {
showFieldError(input, errorDiv, 'Numéro de téléphone invalide');
isValid = false;
}
break;
}
}
});
return isValid;
}
function showFieldError(input, errorDiv, message) {
input.classList.add('error');
errorDiv.textContent = message;
errorDiv.classList.add('visible');
}
function isValidEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
function isValidPhone(phone) {
const re = /^[\d\s\-\+\(\)]+$/;
return re.test(phone) && phone.replace(/\D/g, '').length >= 10;
}
// ========================================
// Form Submission
// ========================================
function handleFormSubmit(e) {
e.preventDefault();
// Validate last step
if (!validateCurrentStep()) {
return;
}
// Save last step data
saveCurrentStepData();
// Submit
submitFormData();
}
// ========================================
// Event Listeners
// ========================================
function attachEventListeners() {
elements.prevBtn.addEventListener('click', goToPreviousStep);
elements.nextBtn.addEventListener('click', goToNextStep);
elements.form.addEventListener('submit', handleFormSubmit);
}
// ========================================
// UI State Management
// ========================================
function showError(message) {
elements.loadingState.classList.add('hidden');
elements.formContainer.classList.add('hidden');
elements.successState.classList.add('hidden');
elements.errorState.classList.remove('hidden');
elements.errorMessage.textContent = message;
}
function showSuccess() {
elements.loadingState.classList.add('hidden');
elements.formContainer.classList.add('hidden');
elements.errorState.classList.add('hidden');
elements.successState.classList.remove('hidden');
}