Working on saving journal entry edits

This commit is contained in:
Scott Idem
2025-04-09 15:45:38 -04:00
parent b505424c9d
commit 3587258ec7

View File

@@ -26,6 +26,8 @@ import { ae_snip, ae_loc, ae_sess, ae_api, ae_trig, slct, slct_trigger } from '$
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';
console.log(`ae_comp__journal_entry_obj_id_view.svelte`);
interface Props {
log_lvl?: number;
lq__journal_obj: any;
@@ -44,9 +46,12 @@ let ae_promises: key_val = $state({});
// let ae_triggers: key_val = {};
// let orig_entry_obj: key_val = $state({});
let orig_entry_obj: key_val|null = null;
let orig_entry_obj: key_val|null = $state({});
let tmp_entry_obj_changed: boolean = $state(false);
let tmp_entry_obj: key_val = $state({});
let tmp_entry_obj: key_val|null = $state({});
let updated_obj: boolean = $state(true); // Start with true to force population of orig and tmp values.
let updated_idb: boolean = $state(true); // Updated in a separate browser session
// let tmp_entry_obj: key_val = { ...$lq__journal_entry_obj };
// let tmp_entry_obj = $derived(async () => {
@@ -57,26 +62,67 @@ let tmp_entry_obj: key_val = $state({});
// }
// );
function not_obj(obj) {
if (obj == null) {
// null or undefined
return true;
}
if (typeof obj !== 'object') {
return true;
}
return Object.keys(obj).length === 0;
}
$effect(() => {
if ($lq__journal_entry_obj && !orig_entry_obj) {
// console.log('TEST: orig_entry_obj?', orig_entry_obj);
// console.log('TEST: Journal entry object available');
// if (not_obj(orig_entry_obj) && not_obj(tmp_entry_obj) && $lq__journal_entry_obj && $lq__journal_entry_obj?.journal_entry_id) {
if (updated_obj && JSON.stringify(orig_entry_obj) === JSON.stringify($lq__journal_entry_obj)) {
console.log(`TEST: updated_obj but orig_entry_obj and $lq__journal_entry_obj still the same`, $lq__journal_entry_obj);
// orig_entry_obj = { ...$lq__journal_entry_obj };
// tmp_entry_obj = { ...$lq__journal_entry_obj };
// tmp_entry_obj_changed = false;
// updated_obj = false;
// } else if (not_obj(orig_entry_obj) && $lq__journal_entry_obj) {
// console.log('TEST: orig_entry_obj?', orig_entry_obj);
// console.log('TEST: LQ Journal Entry object available', $lq__journal_entry_obj);
// orig_entry_obj = { ...$lq__journal_entry_obj };
// tmp_entry_obj = { ...$lq__journal_entry_obj };
// tmp_entry_obj_changed = false;
} else if (updated_obj && $lq__journal_entry_obj) {
console.log(`TEST: updated_obj and $lq__journal_entry_obj; setting orig_entry_obj and tmp_entry_obj`, $lq__journal_entry_obj);
orig_entry_obj = { ...$lq__journal_entry_obj };
tmp_entry_obj = { ...$lq__journal_entry_obj };
tmp_entry_obj_changed = false;
updated_obj = false;
updated_idb = false;
} else if (
!updated_obj
&& JSON.stringify(orig_entry_obj) !== JSON.stringify($lq__journal_entry_obj)
) {
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
// // &&
// !not_obj(orig_entry_obj)
// && $lq__journal_entry_obj
// && JSON.stringify(orig_entry_obj) !== JSON.stringify($lq__journal_entry_obj)
// ) {
// console.log('TEST: IDB has changed!!!');
// orig_entry_obj = { ...$lq__journal_entry_obj };
} else {
// console.log('TEST: No journal entry object available');
console.log('TEST: Catch all: LQ Journal Entry object available', orig_entry_obj, tmp_entry_obj, $lq__journal_entry_obj);
}
});
$effect(() => {
if (tmp_entry_obj) {
// console.log('TEST: tmp_entry_obj available; marking tmp_entry_obj as changed');
tmp_entry_obj_changed = JSON.stringify(tmp_entry_obj) != JSON.stringify($lq__journal_entry_obj);
if (!not_obj(tmp_entry_obj) && !not_obj(orig_entry_obj) && JSON.stringify(tmp_entry_obj) != JSON.stringify(orig_entry_obj)) {
console.log('TEST: tmp_entry_obj and orig_entry_obj available; marking tmp_entry_obj as changed');
// tmp_entry_obj_changed = JSON.stringify(tmp_entry_obj) != JSON.stringify($lq__journal_entry_obj);
// tmp_entry_obj_changed = JSON.stringify(tmp_entry_obj) != JSON.stringify(orig_entry_obj);
tmp_entry_obj_changed = true;
} else {
// console.log('TEST: No tmp_entry_obj available');
console.log('TEST: tmp_entry_obj == orig_entry_obj');
tmp_entry_obj_changed = false;
}
});
@@ -835,11 +881,14 @@ $effect(() => {
api_cfg: $ae_api,
journal_entry_id: $lq__journal_entry_obj?.journal_entry_id,
data_kv: data_kv,
log_lvl: log_lvl
log_lvl: 1
}).then(() => {
// Optionally, you can provide feedback to the user
// alert('Journal entry updated successfully!');
orig_entry_obj = null;
// orig_entry_obj = {};
// tmp_entry_obj = {};
updated_obj = true;
updated_idb = false;
}).catch((error) => {
console.error('Error updating journal entry:', error);
alert('Failed to update journal entry.');
@@ -859,6 +908,17 @@ $effect(() => {
Save Changes?
</button>
<!-- Do a quick check to see if the updated_on timestamp has changed. Specifically, if the entry was updated since the last time it was loaded. -->
{#if updated_idb}
<span class="text-sm text-red-500">
WARNING: IDB object has been updated since last load.
</span>
{/if}
<!-- Updated: {orig_entry_obj?.updated_on}
Updated obj? {updated_obj} -->
<!-- && $lq__journal_entry_obj?.updated_on !== orig_entry_obj?.updated_on -->
</div>
<!-- <div>