web: do not interrupt importer if notebook is undefined

This commit is contained in:
Abdullah Atta
2023-03-24 11:43:37 +05:00
parent 87ebe1918c
commit 61b7e43d08

View File

@@ -101,8 +101,13 @@ async function importNote(note: Note) {
note.notebooks = []; note.notebooks = [];
const noteId = await db.notes?.add(note); const noteId = await db.notes?.add(note);
for (const notebook of notebooks) { for (const nb of notebooks) {
await db.notes?.addToNotebook(await importNotebook(notebook), noteId); const notebook = await importNotebook(nb);
if (!notebook.id) continue;
await db.notes?.addToNotebook(
{ id: notebook.id, topic: notebook.topic },
noteId
);
} }
} }
@@ -111,7 +116,9 @@ async function fileToJson<T>(file: Entry) {
return JSON.parse(text) as T; return JSON.parse(text) as T;
} }
async function importNotebook(notebook: Notebook) { async function importNotebook(
notebook: Notebook
): Promise<{ id?: string; topic?: string }> {
let nb = db.notebooks?.all.find((nb) => nb.title === notebook.notebook); let nb = db.notebooks?.all.find((nb) => nb.title === notebook.notebook);
if (!nb) { if (!nb) {
const nbId = await db.notebooks?.add({ const nbId = await db.notebooks?.add({
@@ -122,5 +129,5 @@ async function importNotebook(notebook: Notebook) {
} }
const topic = nb?.topics.find((t: any) => t.title === notebook.topic); const topic = nb?.topics.find((t: any) => t.title === notebook.topic);
return { id: nb.id, topic: topic.id }; return { id: nb?.id, topic: topic?.id };
} }