This commit is contained in:
daniel31x13 2024-11-09 23:11:03 -05:00
parent 836360f99d
commit e39645e135

View File

@ -127,39 +127,64 @@ export default function Account() {
};
const importBookmarks = async (
e: ChangeEvent<HTMLInputElement>,
e: React.ChangeEvent<HTMLInputElement>,
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 body: MigrationRequest = {
format,
data: request,
};
try {
const response = await fetch("/api/v1/migration", {
method: "POST",
body: JSON.stringify(body),
});
const data = await response.json();
if (!response.ok) {
const errorData = await response.json();
toast.dismiss(load);
if (response.ok) {
toast.success(t("import_success"));
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("");