keep new note in editor

This commit is contained in:
catto 2024-04-15 14:22:46 +02:00
parent c9e2a0eb7f
commit a6d61f6e54
3 changed files with 79 additions and 10 deletions

View File

@ -146,7 +146,9 @@ pub fn search_notes(query: &str) -> Result<String, String> {
query, query 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 let notes = prepare
.query_map([], |row| { .query_map([], |row| {
@ -172,11 +174,53 @@ pub fn search_notes(query: &str) -> Result<String, String> {
json_array.push(note_json); 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) Ok(json_string)
} }
/// get latest note
/// Returns a json array string of size 1
pub fn get_latest_note() -> Result<String, String> {
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)] #[cfg(test)]
mod tests { mod tests {

View File

@ -16,6 +16,12 @@ fn get_notes_list() -> String {
notes.to_string() notes.to_string()
} }
#[tauri::command]
fn get_latest_note() -> String {
let note = libsnotes::get_latest_note().unwrap();
note.to_string()
}
#[tauri::command] #[tauri::command]
fn search_notes(query: &str) -> String { fn search_notes(query: &str) -> String {
let results = libsnotes::search_notes(query).unwrap(); let results = libsnotes::search_notes(query).unwrap();
@ -43,6 +49,7 @@ fn main() {
.invoke_handler(tauri::generate_handler![ .invoke_handler(tauri::generate_handler![
greet, greet,
get_notes_list, get_notes_list,
get_latest_note,
search_notes, search_notes,
create_note, create_note,
delete_specific_note, delete_specific_note,

View File

@ -44,7 +44,8 @@ async function saveNote() {
content: createNoteContentEl.value, content: createNoteContentEl.value,
tag: createNoteTagEl.value tag: createNoteTagEl.value
}); });
clearEditor(); //clearEditor();
refreshSidebarAndOpenLatestNote();
break; break;
case EditorState.EDITING: case EditorState.EDITING:
console.log("updating existing note..") console.log("updating existing note..")
@ -82,8 +83,6 @@ async function showNotes() {
tag: jsonObj.tag tag: jsonObj.tag
})); }));
console.log(noteArray[0])
fillNoteSidebar(noteArray); fillNoteSidebar(noteArray);
} else { } else {
searchNote(searchbarContents); searchNote(searchbarContents);
@ -94,7 +93,7 @@ async function showNotes() {
async function retrieveNotes(): Promise<Array<JSON>> { async function retrieveNotes(): Promise<Array<JSON>> {
const notesString: string = await invoke("get_notes_list"); const notesString: string = await invoke("get_notes_list");
const notesJson = JSON.parse(notesString); const notesJson = JSON.parse(notesString);
console.log(notesJson);
return notesJson; return notesJson;
} }
@ -255,7 +254,7 @@ function fillNoteSidebar(noteArray: Note[]) {
function handleSidebarNoteClick(id: Number): any { function handleSidebarNoteClick(id: Number): any {
console.log("huh " + id); console.log("clicked note " + id);
if (createNoteContentEl && createNoteTagEl) { if (createNoteContentEl && createNoteTagEl) {
// search for note // search for note
let n: Note = { let n: Note = {
@ -380,8 +379,6 @@ async function searchNote(input: string) {
tag: jsonObj.tag tag: jsonObj.tag
})); }));
console.log(noteArray[0])
fillNoteSidebar(noteArray); fillNoteSidebar(noteArray);
} }
} }
@ -391,7 +388,28 @@ async function getSearchResults(input: string): Promise<Array<JSON>> {
query: input query: input
}); });
const resultsJson = JSON.parse(resultsString); const resultsJson = JSON.parse(resultsString);
console.log(resultsJson);
return 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);
}