diff --git a/pages/settings/account.tsx b/pages/settings/account.tsx index 851a7c7..d4f99fe 100644 --- a/pages/settings/account.tsx +++ b/pages/settings/account.tsx @@ -127,39 +127,64 @@ export default function Account() { }; const importBookmarks = async ( - e: ChangeEvent, + e: React.ChangeEvent, format: MigrationFormat ) => { - setSubmitLoader(true); - const file = e.target.files?.[0]; + const file: File | null = e.target.files && e.target.files[0]; if (file) { - var reader = new FileReader(); + const reader = new FileReader(); reader.readAsText(file, "UTF-8"); reader.onload = async function (e) { - const load = toast.loading(t("importing_bookmarks")); + const load = toast.loading("Importing..."); + const request: string = e.target?.result as string; - const body: MigrationRequest = { format, data: request }; - const response = await fetch("/api/v1/migration", { - method: "POST", - body: JSON.stringify(body), - }); - const data = await response.json(); - toast.dismiss(load); - if (response.ok) { - toast.success(t("import_success")); + + const body: MigrationRequest = { + format, + data: request, + }; + + try { + const response = await fetch("/api/v1/migration", { + method: "POST", + body: JSON.stringify(body), + }); + + if (!response.ok) { + const errorData = await response.json(); + toast.dismiss(load); + + toast.error( + errorData.response || + "Failed to import bookmarks. Please try again." + ); + return; + } + + await response.json(); + toast.dismiss(load); + toast.success("Imported the Bookmarks! Reloading the page..."); + setTimeout(() => { location.reload(); }, 2000); - } else { - toast.error(data.response as string); + } catch (error) { + console.error("Request failed", error); + toast.dismiss(load); + toast.error( + "An error occurred while importing bookmarks. Please check the logs for more info." + ); } }; + reader.onerror = function (e) { - console.log("Error:", e); + console.log("Error reading file:", e); + toast.error( + "Failed to read the file. Please make sure the file is correct and try again." + ); }; } - setSubmitLoader(false); }; const [whitelistedUsersTextbox, setWhiteListedUsersTextbox] = useState("");