From 2515ba55779e6b6546260b3b9e72550d23f4d4fb Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Wed, 9 Apr 2025 16:18:56 -0400 Subject: [PATCH] Cleaned up Journal Entry update functions --- src/lib/ae_journals/ae_journals__journal.ts | 6 +- .../ae_journals/ae_journals__journal_entry.ts | 6 +- .../ae_comp__journal_entry_obj_id_view.svelte | 468 +++--------------- 3 files changed, 87 insertions(+), 393 deletions(-) diff --git a/src/lib/ae_journals/ae_journals__journal.ts b/src/lib/ae_journals/ae_journals__journal.ts index f7e69387..fcda9eeb 100644 --- a/src/lib/ae_journals/ae_journals__journal.ts +++ b/src/lib/ae_journals/ae_journals__journal.ts @@ -268,7 +268,8 @@ export async function create_ae_obj__journal( db_save_ae_obj_li__journal( { obj_type: 'journal', - obj_li: [journal_obj_create_result] + obj_li: [journal_obj_create_result], + log_lvl: log_lvl }); } return journal_obj_create_result; @@ -375,7 +376,8 @@ export async function update_ae_obj__journal( if (journal_obj_update_result) { if (try_cache) { db_save_ae_obj_li__journal({ - obj_type: 'journal', obj_li: [journal_obj_update_result] + obj_type: 'journal', obj_li: [journal_obj_update_result], + log_lvl: log_lvl }); } return journal_obj_update_result; diff --git a/src/lib/ae_journals/ae_journals__journal_entry.ts b/src/lib/ae_journals/ae_journals__journal_entry.ts index c0144c96..f7b5d0f5 100644 --- a/src/lib/ae_journals/ae_journals__journal_entry.ts +++ b/src/lib/ae_journals/ae_journals__journal_entry.ts @@ -189,7 +189,8 @@ export async function create_ae_obj__journal_entry( await db_save_ae_obj_li__journal_entry( { obj_type: 'journal_entry', - obj_li: [journal_entry_obj_create_result] + obj_li: [journal_entry_obj_create_result], + log_lvl: log_lvl }); } return journal_entry_obj_create_result; @@ -294,7 +295,8 @@ export async function update_ae_obj__journal_entry( if (journal_entry_obj_update_result) { if (try_cache) { await db_save_ae_obj_li__journal_entry({ - obj_type: 'journal_entry', obj_li: [journal_entry_obj_update_result] + obj_type: 'journal_entry', obj_li: [journal_entry_obj_update_result], + log_lvl: log_lvl }); } return journal_entry_obj_update_result; diff --git a/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte b/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte index 8c02d47c..8d702663 100644 --- a/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte +++ b/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte @@ -25,6 +25,7 @@ import { ae_util } from '$lib/ae_utils/ae_utils'; import { ae_snip, ae_loc, ae_sess, ae_api, ae_trig, slct, slct_trigger } from '$lib/ae_stores'; import { journals_loc, journals_sess, journals_slct, journals_trig, journals_prom } from '$lib/ae_journals/ae_journals_stores'; import { journals_func } from '$lib/ae_journals/ae_journals_functions'; + import { updated } from '$app/state'; console.log(`ae_comp__journal_entry_obj_id_view.svelte`); @@ -101,6 +102,8 @@ $effect(() => { console.log('TEST: IDB has changed and not flagged as updated_obj!!!'); updated_idb = true; // orig_entry_obj = { ...$lq__journal_entry_obj }; + } + // } else if ( // // !updated_obj // // && @@ -110,9 +113,10 @@ $effect(() => { // ) { // console.log('TEST: IDB has changed!!!'); // orig_entry_obj = { ...$lq__journal_entry_obj }; - } else { - console.log('TEST: Catch all: LQ Journal Entry object available', orig_entry_obj, tmp_entry_obj, $lq__journal_entry_obj); - } + + // } else { + // console.log('TEST: Catch all: LQ Journal Entry object available', orig_entry_obj, tmp_entry_obj, $lq__journal_entry_obj); + // } }); $effect(() => { @@ -129,7 +133,49 @@ $effect(() => { // const test_html = marked.parse('# Marked in Node.js\n\nRendered by **marked**.'); -// Reminder: Styling is being done with Tailwind CSS, not Bootstrap. +async function update_journal_entry() { + if (!$ae_loc.trusted_access) { + alert('You do not have permission to update this journal entry.'); + return; + } + + let data_kv = { + alert: tmp_entry_obj?.alert, + personal: tmp_entry_obj?.personal, + private: tmp_entry_obj?.private, + professional: tmp_entry_obj?.professional, + public: tmp_entry_obj?.public, + template: tmp_entry_obj?.template, + + hide: tmp_entry_obj?.hide, + priority: tmp_entry_obj?.priority, + enable: tmp_entry_obj?.enable, + + // alert_msg: $lq__journal_entry_obj?.alert_msg ? false : true + alert_msg: tmp_entry_obj?.alert_msg, + category_code: tmp_entry_obj?.category_code, + content: tmp_entry_obj?.content, + group: tmp_entry_obj?.group, + name: tmp_entry_obj?.name, + tags: tmp_entry_obj?.tags, + }; + + // Call API to save the content + try { + await journals_func.update_ae_obj__journal_entry({ + api_cfg: $ae_api, + journal_entry_id: $lq__journal_entry_obj?.journal_entry_id, + data_kv: data_kv, + log_lvl: 0, + }); + updated_obj = true; + updated_idb = false; + console.log('Journal entry updated successfully!'); + } catch (error) { + console.error('Error updating journal entry:', error); + alert('Failed to update journal entry.'); + } +} @@ -146,32 +192,10 @@ $effect(() => { // Ask if they would like to save changes before toggling edit mode off. if ($journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id]) { if (tmp_entry_obj_changed && confirm('Would you like to save changes to this journal entry before exiting edit mode?')) { - // Call API to save the content - let data_kv = { - alert_msg: tmp_entry_obj?.alert_msg, - category_code: tmp_entry_obj?.category_code, - content: tmp_entry_obj?.content, - group: tmp_entry_obj?.group, - name: tmp_entry_obj?.name, - tags: tmp_entry_obj?.tags, - }; - journals_func.update_ae_obj__journal_entry({ - api_cfg: $ae_api, - journal_entry_id: $lq__journal_entry_obj?.journal_entry_id, - data_kv: data_kv, - log_lvl: log_lvl - }).then(() => { - // Optionally, you can provide feedback to the user - // alert('Journal entry updated successfully!'); - orig_entry_obj = null; - }).catch((error) => { - console.error('Error updating journal entry:', error); - alert('Failed to update journal entry.'); - }); + update_journal_entry(); } } - // Toggle edit mode if ($journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id]) { $journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id] = false; @@ -404,28 +428,7 @@ $effect(() => { tmp_entry_obj.category_code = event.target.value; console.log('Selected category:', tmp_entry_obj.category_code); - // Call API to save the content - let data_kv = { - alert_msg: tmp_entry_obj?.alert_msg, - category_code: tmp_entry_obj?.category_code, - content: tmp_entry_obj?.content, - group: tmp_entry_obj?.group, - name: tmp_entry_obj?.name, - tags: tmp_entry_obj?.tags, - }; - journals_func.update_ae_obj__journal_entry({ - api_cfg: $ae_api, - journal_entry_id: $lq__journal_entry_obj?.journal_entry_id, - data_kv: data_kv, - log_lvl: log_lvl - }).then(() => { - // Optionally, you can provide feedback to the user - // alert('Journal entry updated successfully!'); - orig_entry_obj = null; - }).catch((error) => { - console.error('Error updating journal entry:', error); - alert('Failed to update journal entry.'); - }); + update_journal_entry(); }} title="Select a category for the new journal entry" > @@ -475,31 +478,8 @@ $effect(() => { @@ -712,31 +578,8 @@ $effect(() => {