Working on saving journal entry edits
This commit is contained in:
@@ -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_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 { journals_func } from '$lib/ae_journals/ae_journals_functions';
|
||||||
|
|
||||||
|
console.log(`ae_comp__journal_entry_obj_id_view.svelte`);
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
log_lvl?: number;
|
log_lvl?: number;
|
||||||
lq__journal_obj: any;
|
lq__journal_obj: any;
|
||||||
@@ -44,9 +46,12 @@ let ae_promises: key_val = $state({});
|
|||||||
// let ae_triggers: key_val = {};
|
// let ae_triggers: key_val = {};
|
||||||
|
|
||||||
// let orig_entry_obj: key_val = $state({});
|
// 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_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: key_val = { ...$lq__journal_entry_obj };
|
||||||
|
|
||||||
// let tmp_entry_obj = $derived(async () => {
|
// 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(() => {
|
$effect(() => {
|
||||||
if ($lq__journal_entry_obj && !orig_entry_obj) {
|
// if (not_obj(orig_entry_obj) && not_obj(tmp_entry_obj) && $lq__journal_entry_obj && $lq__journal_entry_obj?.journal_entry_id) {
|
||||||
// console.log('TEST: orig_entry_obj?', orig_entry_obj);
|
if (updated_obj && JSON.stringify(orig_entry_obj) === JSON.stringify($lq__journal_entry_obj)) {
|
||||||
// console.log('TEST: Journal entry object available');
|
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 };
|
orig_entry_obj = { ...$lq__journal_entry_obj };
|
||||||
tmp_entry_obj = { ...$lq__journal_entry_obj };
|
tmp_entry_obj = { ...$lq__journal_entry_obj };
|
||||||
tmp_entry_obj_changed = false;
|
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 {
|
} 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(() => {
|
$effect(() => {
|
||||||
if (tmp_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 available; marking tmp_entry_obj as changed');
|
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($lq__journal_entry_obj);
|
||||||
// tmp_entry_obj_changed = JSON.stringify(tmp_entry_obj) != JSON.stringify(orig_entry_obj);
|
// tmp_entry_obj_changed = JSON.stringify(tmp_entry_obj) != JSON.stringify(orig_entry_obj);
|
||||||
|
tmp_entry_obj_changed = true;
|
||||||
} else {
|
} else {
|
||||||
// console.log('TEST: No tmp_entry_obj available');
|
console.log('TEST: tmp_entry_obj == orig_entry_obj');
|
||||||
tmp_entry_obj_changed = false;
|
tmp_entry_obj_changed = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -835,11 +881,14 @@ $effect(() => {
|
|||||||
api_cfg: $ae_api,
|
api_cfg: $ae_api,
|
||||||
journal_entry_id: $lq__journal_entry_obj?.journal_entry_id,
|
journal_entry_id: $lq__journal_entry_obj?.journal_entry_id,
|
||||||
data_kv: data_kv,
|
data_kv: data_kv,
|
||||||
log_lvl: log_lvl
|
log_lvl: 1
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
// Optionally, you can provide feedback to the user
|
// Optionally, you can provide feedback to the user
|
||||||
// alert('Journal entry updated successfully!');
|
// alert('Journal entry updated successfully!');
|
||||||
orig_entry_obj = null;
|
// orig_entry_obj = {};
|
||||||
|
// tmp_entry_obj = {};
|
||||||
|
updated_obj = true;
|
||||||
|
updated_idb = false;
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
console.error('Error updating journal entry:', error);
|
console.error('Error updating journal entry:', error);
|
||||||
alert('Failed to update journal entry.');
|
alert('Failed to update journal entry.');
|
||||||
@@ -859,6 +908,17 @@ $effect(() => {
|
|||||||
Save Changes?
|
Save Changes?
|
||||||
</button>
|
</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>
|
||||||
|
|
||||||
<!-- <div>
|
<!-- <div>
|
||||||
|
|||||||
Reference in New Issue
Block a user