Skip to content

Commit 0b27c42

Browse files
author
Andrea Aramini, INI-MBM-BNC (EXT)
committed
⚡ perf: decrease number of calls and improve error handling
1 parent d557e1a commit 0b27c42

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

src/app/components/ContactSection.jsx

+18-16
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,25 @@ const EmailSection = () => {
1414
e.target.email.value,
1515
e.target.subject.value,
1616
e.target.message.value,
17-
).then((response) => {
18-
if (response.status === 201) {
17+
)
18+
.then((_) => {
1919
formRef.current.reset();
20-
}
21-
});
20+
})
21+
.then((_) => {
22+
controls.set({ opacity: 0, scale: 1 });
23+
controls.start({
24+
opacity: 1,
25+
scale: 1,
26+
transition: {
27+
repeat: 1,
28+
repeatType: 'reverse',
29+
duration: 2,
30+
},
31+
});
32+
})
33+
.catch((err) => {
34+
console.log(err);
35+
});
2236
};
2337
return (
2438
<section
@@ -108,18 +122,6 @@ const EmailSection = () => {
108122
<button
109123
type="submit"
110124
className="rounded hover:bg-primary-400 bg-primary-600 text-primaryText w-full py-2.5 flex justify-center"
111-
onClick={() => {
112-
controls.set({ opacity: 0, scale: 1 });
113-
controls.start({
114-
opacity: 1,
115-
scale: 1,
116-
transition: {
117-
repeat: 1,
118-
repeatType: 'reverse',
119-
duration: 2,
120-
},
121-
});
122-
}}
123125
>
124126
Send Message
125127
</button>

src/app/components/HeroSection.jsx

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use client';
2-
import React, { useEffect, useState } from 'react';
2+
import React, { useEffect, useRef, useState } from 'react';
33
import Image from 'next/image';
44
import { TypeAnimation } from 'react-type-animation';
55
import Link from 'next/link';
@@ -8,16 +8,24 @@ import { generateCurriculum } from '../lib/client';
88
const HeroSection = () => {
99
const [hasCvDownloaded, setHasCvDownloaded] = useState(false);
1010
const [downloadLink, setDownloadLink] = useState();
11+
const initialized = useRef(false);
1112

1213
useEffect(() => {
1314
const getDownloadLink = async () => {
14-
await generateCurriculum().then((response) => {
15-
if (response.status === 200) {
16-
setDownloadLink(response.data.download_link);
17-
}
18-
});
15+
await generateCurriculum()
16+
.then((response) => {
17+
if (response) {
18+
setDownloadLink(response.data.download_link);
19+
}
20+
})
21+
.catch((err) => {
22+
console.log(err);
23+
});
1924
};
20-
getDownloadLink();
25+
if (!initialized.current) {
26+
initialized.current = true;
27+
getDownloadLink();
28+
}
2129
});
2230

2331
return (

src/app/lib/client.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const instance = axios.create({
66
: '/api/v1',
77
timeout: 10000,
88
headers: { 'Content-Type': 'application/json' },
9+
validateStatus: (status) => {
10+
return status >= 200 && status < 300;
11+
},
912
});
1013

1114
export async function sendContactMeEmail(emailFrom, subject, text) {
@@ -14,17 +17,9 @@ export async function sendContactMeEmail(emailFrom, subject, text) {
1417
subject: subject,
1518
text: text,
1619
};
17-
try {
18-
return await instance.post('/contact-me/', data);
19-
} catch (error) {
20-
console.log(`unable to send email ${error}`);
21-
}
20+
return await instance.post('/contact-me/', data);
2221
}
2322

2423
export async function generateCurriculum() {
25-
try {
26-
return await instance.post('/curriculum/');
27-
} catch (error) {
28-
console.log(`unable to generate curriculum ${error}`);
29-
}
24+
return await instance.post('/curriculum/');
3025
}

0 commit comments

Comments
 (0)