From a6d61f6e5492bd435814bac1843779e821c5ab98 Mon Sep 17 00:00:00 2001 From: catto Date: Mon, 15 Apr 2024 14:22:46 +0200 Subject: [PATCH] keep new note in editor --- libsnotes/src/lib.rs | 48 +++++++++++++++++++++++++++++++++++++++++-- src-tauri/src/main.rs | 7 +++++++ src/main.ts | 34 ++++++++++++++++++++++-------- 3 files changed, 79 insertions(+), 10 deletions(-) diff --git a/libsnotes/src/lib.rs b/libsnotes/src/lib.rs index 7343cdc..954a197 100644 --- a/libsnotes/src/lib.rs +++ b/libsnotes/src/lib.rs @@ -146,7 +146,9 @@ pub fn search_notes(query: &str) -> Result { query, query ); - let mut prepare = connection.prepare(&query).map_err(|e| format!("Query Error: {}", e))?; + let mut prepare = connection + .prepare(&query) + .map_err(|e| format!("Query Error: {}", e))?; let notes = prepare .query_map([], |row| { @@ -172,11 +174,53 @@ pub fn search_notes(query: &str) -> Result { json_array.push(note_json); } - let json_string = serde_json::to_string(&json_array).map_err(|e| format!("JSON Error: {}", e))?; + let json_string = + serde_json::to_string(&json_array).map_err(|e| format!("JSON Error: {}", e))?; Ok(json_string) } +/// get latest note +/// Returns a json array string of size 1 +pub fn get_latest_note() -> Result { + let home = home_dir().unwrap().join(".snotes.db"); + let connection = Connection::open(home).map_err(|e| format!("Database Error: {}", e))?; + + let query = "SELECT * FROM notes WHERE ROWID IN (SELECT max(ROWID) FROM notes); + ".to_string(); + let mut prepare = connection + .prepare(&query) + .map_err(|e| format!("Query Error: {}", e))?; + + let notes = prepare + .query_map([], |row| { + Ok(Note { + id: row.get(0)?, + content: row.get(1)?, + date: row.get(2)?, + tag: row.get(3)?, + }) + }) + .map_err(|e| format!("Mapping Error: {}", e))?; + + let mut json_array = Vec::new(); + + for note in notes { + let unwrapped = note.map_err(|e| format!("Note Error: {}", e))?; + let note_json = json!({ + "id": unwrapped.id, + "date": unwrapped.date, + "content": unwrapped.content, + "tag": unwrapped.tag + }); + json_array.push(note_json); + } + + let json_string = + serde_json::to_string(&json_array).map_err(|e| format!("JSON Error: {}", e))?; + println!("{}", json_string); + Ok(json_string) +} #[cfg(test)] mod tests { diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a78c1fb..0e1cbac 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -16,6 +16,12 @@ fn get_notes_list() -> String { notes.to_string() } +#[tauri::command] +fn get_latest_note() -> String { + let note = libsnotes::get_latest_note().unwrap(); + note.to_string() +} + #[tauri::command] fn search_notes(query: &str) -> String { let results = libsnotes::search_notes(query).unwrap(); @@ -43,6 +49,7 @@ fn main() { .invoke_handler(tauri::generate_handler![ greet, get_notes_list, + get_latest_note, search_notes, create_note, delete_specific_note, diff --git a/src/main.ts b/src/main.ts index f970fb6..fa20a03 100644 --- a/src/main.ts +++ b/src/main.ts @@ -44,7 +44,8 @@ async function saveNote() { content: createNoteContentEl.value, tag: createNoteTagEl.value }); - clearEditor(); + //clearEditor(); + refreshSidebarAndOpenLatestNote(); break; case EditorState.EDITING: console.log("updating existing note..") @@ -82,8 +83,6 @@ async function showNotes() { tag: jsonObj.tag })); - console.log(noteArray[0]) - fillNoteSidebar(noteArray); } else { searchNote(searchbarContents); @@ -94,7 +93,7 @@ async function showNotes() { async function retrieveNotes(): Promise> { const notesString: string = await invoke("get_notes_list"); const notesJson = JSON.parse(notesString); - console.log(notesJson); + return notesJson; } @@ -255,7 +254,7 @@ function fillNoteSidebar(noteArray: Note[]) { function handleSidebarNoteClick(id: Number): any { - console.log("huh " + id); + console.log("clicked note " + id); if (createNoteContentEl && createNoteTagEl) { // search for note let n: Note = { @@ -380,8 +379,6 @@ async function searchNote(input: string) { tag: jsonObj.tag })); - console.log(noteArray[0]) - fillNoteSidebar(noteArray); } } @@ -391,7 +388,28 @@ async function getSearchResults(input: string): Promise> { query: input }); const resultsJson = JSON.parse(resultsString); - console.log(resultsJson); return resultsJson; } +/** + * When we save a new note, we want to keep that note + * in the editor, so we switch out the New note for + * Editing an existing note (the latest one, the one + * we just created) + */ +async function refreshSidebarAndOpenLatestNote() { + showNotes(); + + const noteString = await invoke("get_latest_note"); + const noteJson = JSON.parse(noteString as string); + + const latestNote: Note = { + id: noteJson[0].id, + content: noteJson[0].content, + date: noteJson[0].date, + tag: noteJson[0].tag + } + + openNote(latestNote); +} +