Making the loading a bit more efficient and cleaner

This commit is contained in:
Scott Idem
2024-02-22 20:28:29 -05:00
parent d1328eb67c
commit 02f693c13e
4 changed files with 215 additions and 7 deletions

View File

@@ -1,3 +1,152 @@
/** @type {import('./$types').LayoutLoad} */
import { api } from '$lib/api';
import { ae_loc, ae_sess, ae_api, slct, slct_trigger } from '$lib/ae_stores';
import { get } from 'svelte/store';
async function handle_load_ae_obj_id__site_domain({fqdn, try_cache=false}) {
console.log(`*** handle_load_ae_obj_id__site_domain() *** fqdn=${fqdn}`);
let params = {};
// ae_loc.hub.site_domain_id_qry_status = 'loading';
let ae_site_domain_obj_get_promise = api.get_ae_obj_id_crud({
api_cfg: get(ae_api),
no_account_id: true,
obj_type: 'site_domain',
obj_id: fqdn, // NOTE: This is the FQDN, not normally the ID.
use_alt_table: true, // NOTE: This will use the table_name_alt value instead of the table_name value in the API config.
use_alt_base: true, // NOTE: This will use the base_name_alt value instead of the base_name value in the API config.
params: params,
log_lvl: 2
})
.then(function (site_domain_obj_get_result) {
if (site_domain_obj_get_result) {
// slct.site_domain_obj = site_domain_obj_get_result;
// console.log(`site_domain object:`, get(slct).site_domain_obj);
// ae_loc.account_id = $slct.site_domain_obj.account_id_random;
// ae_loc.site_id = $slct.site_domain_obj.site_id_random;
// ae_loc.site_domain_id = $slct.site_domain_obj.site_domain_id_random;
return site_domain_obj_get_result;
} else {
console.log('No results returned.');
return null;
}
})
.catch(function (error) {
console.log('No results returned or failed.', error);
});
return ae_site_domain_obj_get_promise;
}
let data_store_obj_get_promises: key_val = {};
async function handle_get_data_store_obj_w_code({ code=null, data_type='text' }) {
console.log(`*** handle_get_data_store_obj_w_code() *** code=${code}`);
if (!code) {
console.log('No code provided.');
return;
}
let data_store_obj_get_promise = api.get_data_store_obj_w_code({
api_cfg: get(ae_api),
data_store_code: code,
data_type: data_type,
log_lvl: 0
})
.then(function (get_data_store_result) {
let return_this = null;
if (get_data_store_result) {
if (data_type == 'text') {
// console.log(get_data_store_result.text);
return_this = get_data_store_result.text;
} else if (data_type == 'json') {
// console.log(get_data_store_result.json);
return_this = get_data_store_result.json;
}
} else {
console.log('No results returned.');
return_this = null;
}
return return_this;
})
.catch(function (error) {
console.log('No results returned or failed.', error);
});
return data_store_obj_get_promise;
}
export function load({ params, url }) { // route
console.log(`Svelte root layout.ts data = params:`, params);
// console.log(`Svelte root layout.ts data = route:`, route);
console.log(`Svelte root layout.ts data = url:`, url);
let site_domain_results = null;
let access_code_li_json = null;
let ae_core = null;
if (url.host) {
// ae_loc.url_host = data.url.host; // Use this to look up? sub.example.com:123
// ae_loc.fqdn = url.host; // Use this to look up? sub.example.com:123
// ae_loc.url_hostname = data.url.hostname; // sub.example.com
// ae_loc.url_origin = data.url.origin; // Use this to look up? https://sub.example.com:123
// ae_loc.site_domain = data.url.origin;
// console.log(`ae_loc = `, ae_loc);
site_domain_results = handle_load_ae_obj_id__site_domain({fqdn: url.host, try_cache: false});
if (site_domain_results) {
console.log(`site_domain_results = `, site_domain_results);
ae_core = {
'account_id': site_domain_results.account_id_random,
'site_id': site_domain_results.site_id_random,
'site_domain_id': site_domain_results.site_domain_id_random,
'enable': site_domain_results.enable,
'style_href': site_domain_results.style_href,
'google_tracking_id': site_domain_results.google_tracking_id,
}
}
access_code_li_json = handle_get_data_store_obj_w_code({code: 'hub__page__access_code_li_json', data_type: 'json'});
if (access_code_li_json) {
console.log(`access_code_li_json = `, access_code_li_json);
ae_core = {
...ae_core,
'access_code_li': access_code_li_json,
}
}
}
// ae_core = {
// 'account_id': site_domain_results.account_id_random
// }
let data_struct = {
params: params,
sections: [
{ slug: 'new', title: 'New Test' },
{ slug: 'manage', title: 'Manage Test' },
{ slug: 'test', title: 'Test Test' },
],
url: url,
ae_core: ae_core,
};
return data_struct;
}
// export const ssr = false;
// export const prerender = true;
// export const prerender = true