From 088154ffcf11b31be6281e73c177e203c82353b7 Mon Sep 17 00:00:00 2001 From: Anthony Bushara Date: Fri, 20 Sep 2024 16:29:44 -0400 Subject: [PATCH] refactor: use async/await instead of then --- app/lib/theme/widgets/FileWidget.tsx | 32 +++++++++++++++++----------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/app/lib/theme/widgets/FileWidget.tsx b/app/lib/theme/widgets/FileWidget.tsx index d605b4ccd..68abf1f75 100644 --- a/app/lib/theme/widgets/FileWidget.tsx +++ b/app/lib/theme/widgets/FileWidget.tsx @@ -86,19 +86,19 @@ const FileWidget: React.FC = ({ if (file) { fileFormData.append('file', file); if (setTemplateData) { - await fetch( - `/api/applicant/template?templateNumber=${templateNumber}`, - { - method: 'POST', - body: fileFormData, - } - ).then((response) => { + try { + const response = await fetch( + `/api/applicant/template?templateNumber=${templateNumber}`, + { + method: 'POST', + body: fileFormData, + } + ); if (response.ok) { - response.json().then((data) => { - setTemplateData({ - templateNumber, - data, - }); + const data = response.json(); + setTemplateData({ + templateNumber, + data, }); } else { isTemplateValid = false; @@ -107,7 +107,13 @@ const FileWidget: React.FC = ({ error: true, }); } - }); + } catch (error) { + isTemplateValid = false; + setTemplateData({ + templateNumber, + error: true, + }); + } } } }