diff --git a/components/RecordForm/RecordForm.jsx b/components/RecordForm/RecordForm.jsx index a948310..300dd4e 100644 --- a/components/RecordForm/RecordForm.jsx +++ b/components/RecordForm/RecordForm.jsx @@ -95,14 +95,14 @@ export function RecordForm() { toast({ title: "Record saved", status: "success", - duration: 4000, + duration: 3000, }); } catch (error) { toast({ title: "Error saving record", description: error.message, status: "error", - duration: 4000, + isClosable: true, }); } })} diff --git a/serverActions/appendRecord.js b/serverActions/appendRecord.js index f41a423..e9ac250 100644 --- a/serverActions/appendRecord.js +++ b/serverActions/appendRecord.js @@ -88,7 +88,7 @@ export async function appendRecord(formData) { ) ); - const record = new Record({ + const record = await new Record({ date: Date.now(), quotes: await getQuotes({ baseCurrencies, recordCurrencies }), institutions: recordInstitutions, @@ -97,8 +97,8 @@ export async function appendRecord(formData) { try { await connect(); await record.save(); - console.log("Document saved to db"); + console.info("record saved to db"); } catch (error) { - throw new Error("Error saving document:", error.message); + throw new Error(`Error saving data to database: ${error.message}`); } } diff --git a/serverActions/getQuotes.js b/serverActions/getQuotes.js index f74c96d..3139acc 100644 --- a/serverActions/getQuotes.js +++ b/serverActions/getQuotes.js @@ -13,16 +13,20 @@ export async function getQuotes({ baseCurrencies, recordCurrencies }) { ); async function fetchQuotes(fetchUrls) { - try { - const promises = fetchUrls.map((url) => fetch(url)); - const responses = await Promise.all(promises); - const data = await Promise.all( - responses.map((response) => response.json()) - ); - return data; - } catch (error) { - console.error(error); - } + const promises = await fetchUrls.map(async (url) => fetch(url)); + + const responses = await Promise.all(promises); + const data = await Promise.all( + responses.map(async (response) => { + if (!response.ok) { + throw new Error( + `Quotes API request failed with status ${response.status}. Url: ${response.url}` + ); + } + return await response.json(); + }) + ); + return data; } const quotes = (await fetchQuotes(fetchUrls))