Lots of general clean up and fixes.

This commit is contained in:
Scott Idem
2024-03-20 13:31:42 -04:00
parent fbbaa1392b
commit 84e3098b72
10 changed files with 375 additions and 173 deletions

View File

@@ -8,11 +8,13 @@ import { ae_loc, ae_sess, ae_api, slct, slct_trigger, ae_trig } from '$lib/ae_st
import { ae_util } from '$lib/ae_utils';
import type { key_val } from '$lib/ae_stores';
export let expire_minutes: number = 10;
export let ds_code: string;
export let ds_name: null|string = null;
export let ds_type: string = 'text';
export let for_type: null|string;
export let for_id: null|string;
export let for_type: null|string = null;
export let for_id: null|string = null;
console.log(`ae_e_data_store ${ds_code} for_type=${for_type} for_id=${for_id}`);
export let store: string = 'local';
@@ -71,7 +73,37 @@ $ae_sess.ds.submit_status = null;
$ae_sess.ds.create_status = null;
$ae_sess.ds.update_status = null;
let trigger: null|string = 'load__ds__code';
let trigger: null|string = null;
// This is a quick check to make sure the data store is not stale. If it is, then we need to trigger a reload.
if ($ae_ds_loc.loaded_on) {
let loaded_on = new Date($ae_ds_loc.loaded_on);
let now = new Date();
let diff = now.getTime() - loaded_on.getTime();
let diff_minutes = diff / (1000 * 60);
if (diff_minutes > expire_minutes) {
console.log(`Data Store ${ds_code} stale. Last loaded on: ${loaded_on.toISOString()}`);
// Wait for random number of milliseconds to avoid all data stores being reloaded at the same time.
let random_ms = Math.floor(Math.random() * 500);
console.log(`Random number of milliseconds: ${random_ms}`);
setTimeout(() => {
trigger = 'load__ds__code';
}, random_ms);
// trigger = 'load__ds__code';
}
// let diff_hours = diff / (1000 * 60 * 60);
// if (diff_hours > 1) {
// trigger = 'load__ds__code';
// }
} else {
console.log('No loaded_on date found. Need to trigger reload.');
trigger = 'load__ds__code';
}
// This is a secondary check... The account_id should either be null or match the current account_id.
if (!$ae_ds_loc.account_id === null || $ae_loc.account_id == $ae_loc.account_id) {
trigger = 'load__ds__code';
}
onMount(() => {
console.log('Element: Data Store element_data_store.svelte');
@@ -123,6 +155,11 @@ async function load_data_store({
return false;
}
ds_loaded = true;
// Set the loaded_on datetime to the current time for reference later. This will be used to determine if the data store is stale.
$ae_ds_loc.loaded_on = new Date().toISOString();
$ae_ds_loc.id = ds_results.data_store_id_random;
$ae_ds_loc.account_id = ds_results.account_id_random;
$ae_ds_loc.code = ds_results.code; // This will overwrite whatever was passed in.
@@ -149,8 +186,6 @@ async function load_data_store({
val_text = ds_results.text;
return ds_results.text;
}
ds_loaded = true;
} else {
ds_loaded = false;
ds_loading_status = '-- not found --';
@@ -654,11 +689,12 @@ async function handle_update__data_store({
class="ae_btn_edit__ds btn hover:variant-glass-warning text-xs absolute top-0 right-0 opacity-30 hover:opacity-100 transition delay-700 hover:delay-200 m-1 p-1"
class:opacity-5={!$ae_loc.administrator_access}
class:hidden={!show_edit_btn || !$ae_loc.trusted_access}
on:click={() => {
on:dblclick={() => {
trigger = 'load__ds__code';
show_edit = true;
show_view = false;
}}
title="Double click to edit data store: {ds_code} with {$ae_ds_loc.account_id ? `account ID=${$ae_ds_loc.account_id}` : 'no account ID'}"
>
<span class="fas fa-edit mx-1"></span>
Edit
@@ -681,7 +717,7 @@ async function handle_update__data_store({
</pre> -->
{#await ds_get_results}
<div class="modal-loading text-xs">
<div class="modal-loading text-xs absolute bottom-0 left-0 opacity-30 hover:opacity-100 transition delay-700 hover:delay-200">
<span class="fas fa-spinner fa-spin"></span>
<span class="loading-text">
Loading...