Skip to content

Commit a900edb

Browse files
María Paz Arrieta LandazuriMaría Paz Arrieta Landazuri
authored andcommitted
feat: agregar nombre y email a la tabla de Airtable desde la pantalla de resultados
1 parent 884e714 commit a900edb

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

frontend/src/app/evaluacion-previsional/resultados/page.tsx

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ function ResultadosContent() {
1515
const [isSubscribing, setIsSubscribing] = useState(false);
1616
const [subscriptionMessage, setSubscriptionMessage] = useState('');
1717
const [showModal, setShowModal] = useState(false);
18+
const [showSuccessMessage, setShowSuccessMessage] = useState(false);
19+
const [error, setError] = useState('');
1820

1921
// Get form data from URL parameters
2022
const saldo = Number(searchParams.get('saldo')) || 0;
@@ -143,33 +145,24 @@ function ResultadosContent() {
143145

144146
const handleSubscriptionSubmit = async (e: React.FormEvent) => {
145147
e.preventDefault();
146-
setIsSubscribing(true);
147-
setSubscriptionMessage('');
148+
if (!subscriptionData.email || !subscriptionData.nombre) return;
148149

149150
try {
150-
// Validate email format
151-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
152-
if (!emailRegex.test(subscriptionData.email)) {
153-
throw new Error('Por favor, ingresa un correo electrónico válido');
154-
}
155-
156-
if (!subscriptionData.nombre) {
157-
throw new Error('Por favor, ingresa tu nombre');
158-
}
151+
// Guardar en Airtable con los datos adicionales
152+
await createEvaluation({
153+
afp,
154+
fondo,
155+
saldo: Number(saldo),
156+
fechaNacimiento,
157+
nombre: subscriptionData.nombre,
158+
email: subscriptionData.email
159+
});
159160

160-
// No need to save to Airtable again, just show success message
161-
setSubscriptionMessage('¡Gracias por suscribirte! Te enviaremos información sobre cómo mejorar tu situación previsional.');
161+
setShowSuccessMessage(true);
162162
setSubscriptionData({ nombre: '', email: '' });
163-
// Close modal after successful subscription
164-
setTimeout(() => {
165-
setShowModal(false);
166-
setSubscriptionMessage('');
167-
}, 3000);
168163
} catch (error) {
169-
console.error('Error submitting subscription:', error);
170-
setSubscriptionMessage('Hubo un error al procesar tu suscripción. Por favor, intenta nuevamente.');
171-
} finally {
172-
setIsSubscribing(false);
164+
console.error('Error al guardar suscripción:', error);
165+
setError('Hubo un error al procesar tu suscripción. Por favor, intenta nuevamente.');
173166
}
174167
};
175168

@@ -326,9 +319,9 @@ function ResultadosContent() {
326319
>
327320
{isSubscribing ? 'Enviando...' : 'Recibir Recomendaciones'}
328321
</button>
329-
{subscriptionMessage && (
330-
<p className={`text-center text-sm ${subscriptionMessage.includes('error') ? 'text-red-600' : 'text-green-600'}`}>
331-
{subscriptionMessage}
322+
{error && (
323+
<p className="text-center text-sm text-red-600">
324+
{error}
332325
</p>
333326
)}
334327
</form>

frontend/src/lib/airtable.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export interface EvaluationData {
1616
fondo: string;
1717
saldo: number;
1818
fechaNacimiento: string;
19+
nombre?: string;
20+
email?: string;
1921
}
2022

2123
// Function to create a new evaluation record
@@ -26,15 +28,19 @@ export async function createEvaluation(data: EvaluationData) {
2628
data: data
2729
});
2830

31+
const fields: Record<string, any> = {
32+
'FechaNacimiento': data.fechaNacimiento,
33+
'AFP': data.afp,
34+
'Fondo': data.fondo,
35+
'Saldo': data.saldo
36+
};
37+
38+
// Agregar nombre y email solo si están presentes
39+
if (data.nombre) fields['Nombre'] = data.nombre;
40+
if (data.email) fields['Email'] = data.email;
41+
2942
const record = await base(TABLE_NAME).create([
30-
{
31-
fields: {
32-
'AFP': data.afp,
33-
'Fondo': data.fondo,
34-
'Saldo': data.saldo,
35-
'Fecha de Nacimiento': data.fechaNacimiento
36-
},
37-
},
43+
{ fields }
3844
]);
3945

4046
console.log('Registro creado exitosamente:', record);

0 commit comments

Comments
 (0)