Should have saved my progress earlier. Trying to redo things without using localStorage initially. Shared data...

This commit is contained in:
Scott Idem
2024-03-27 11:36:06 -04:00
parent a8a2131361
commit 3082c07e3e
5 changed files with 109 additions and 33 deletions

View File

@@ -61,6 +61,9 @@ import Element_access_type from '$lib/element_access_type.svelte';
import Element_app_cfg from '$lib/element_app_cfg.svelte';
import Element_data_store from '$lib/element_data_store.svelte';
// let account_id = localStorage.getItem('ae_account_id');
// console.log(`account_id = `, account_id);
// Quickly save the data passed from the parent(s) to the Svelte stores, localStorage, and other. This should catch anything that is a child of this layout.svelte file.
$slct.account_id = data.account_id;
console.log(`$slct.account_id = `, $slct.account_id);
@@ -175,7 +178,7 @@ onMount(() => {
<svelte:head>
<title>{ae_acct.loc.title}</title>
<title>{ae_acct.loc.title ?? 'loading...'}</title>
<link rel="stylesheet" href="{ae_acct.loc.site_style_href}">
<!-- <link rel="manifest" href="/manifest.json"> -->
</svelte:head>

View File

@@ -2,12 +2,52 @@
// console.log(`ae_root +layout.ts start`);
import { get } from 'svelte/store';
import { browser } from '$app/environment';
// import { api } from '$lib/api';
import { ae_loc, ae_sess, ae_api, slct, slct_trigger } from '$lib/ae_stores';
import { core_func } from '$lib/ae_core_functions';
import type { key_val } from '$lib/ae_stores';
import { PUBLIC_AE_API_PROTOCOL, PUBLIC_AE_API_SERVER, PUBLIC_AE_API_BAK_SERVER, PUBLIC_AE_API_PORT, PUBLIC_AE_API_PATH, PUBLIC_AE_API_SECRET_KEY, PUBLIC_AE_API_CRUD_SUPER_KEY, PUBLIC_AE_NO_ACCOUNT_ID, PUBLIC_AE_NO_ACCOUNT_ID_TOKEN } from '$env/static/public';
const api_base_url = `${PUBLIC_AE_API_PROTOCOL}://${PUBLIC_AE_API_SERVER}:${PUBLIC_AE_API_PORT}${PUBLIC_AE_API_PATH}`;
const api_base_url_bak = `${PUBLIC_AE_API_PROTOCOL}://${PUBLIC_AE_API_BAK_SERVER}:${PUBLIC_AE_API_PORT}${PUBLIC_AE_API_PATH}`;
const api_secret_key = PUBLIC_AE_API_SECRET_KEY;
const api_crud_super_key = PUBLIC_AE_API_CRUD_SUPER_KEY;
let ae_account_id: null|string = null;
const ae_no_account_id = PUBLIC_AE_NO_ACCOUNT_ID;
const ae_no_account_id_token = PUBLIC_AE_NO_ACCOUNT_ID_TOKEN;
let ae_api_init: key_val = {
'ver': '2024-03-27_11',
'base_url': api_base_url,
'base_url_bak': api_base_url_bak,
'api_secret_key': api_secret_key, // 'YOUR_API_SECRET_KEY',
'api_secret_key_bak': api_secret_key, // 'YOUR_API_SECRET_KEY',
'api_crud_super_key': api_crud_super_key, // 'YOUR_SUPER_KEY' 'zp5PtX4zUsI'
'headers': {},
'account_id': ae_account_id,
};
let ae_api_headers: key_val = {};
ae_api_headers['Access-Control-Allow-Origin'] = '*';
ae_api_headers['content-type'] = 'application/json';
ae_api_headers['x-aether-api-key'] = ae_api_init.api_secret_key;
ae_api_headers['x-aether-api-token'] = 'fake-temp-token';
ae_api_headers['x-aether-api-expire-on'] = '';
if (ae_account_id) {
ae_api_headers['x-account-id'] = ae_account_id;
} else {
// ae_api_headers['x-account-id'] = ;
}
if (ae_no_account_id) {
ae_api_headers['x-no-account-id'] = ae_no_account_id;
}
ae_api_init['headers'] = ae_api_headers;
// export const prerender = false;
// There is not an initial data store from SvelteKit for this, so we will just use the API to get the data.
@@ -18,8 +58,10 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
// console.log(`Svelte root layout.ts route:`, route);
// console.log(`Svelte root layout.ts url:`, url);
let account_id: Promise<any>;
let ae_acct: key_val = {
api: {},
api: ae_api_init,
ds: {},
loc: {
'account_id': '',
@@ -31,28 +73,28 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
slct: {},
}
let ae_loc_tmp = get(ae_loc);
// let ae_loc_tmp = get(ae_loc);
// console.log(`ae_loc = `, ae_loc_tmp);
let ae_api_tmp = get(ae_api);
// let ae_api_tmp = get(ae_api);
// console.log(`ae_api = `, ae_api_tmp);
let ds_code_li: null|key_val = null;
if (ae_loc_tmp && ae_loc_tmp.ds) {
ds_code_li = ae_loc_tmp.ds;
}
// if (ae_loc_tmp && ae_loc_tmp.ds) {
// ds_code_li = ae_loc_tmp.ds;
// }
// console.log(`ae_ ds_code_li = `, ds_code_li);
let ds_code: null|string = null;
let data_struct = {
// ae_acct should only be updated and referenced by the corresponding account_id.
account_id: null,
ae_acct: {
// '_XY7DXtc9MY': ae_acct,
},
account_id: null,
ae_loc: ae_loc_tmp,
ae_api: ae_api_tmp,
ae_api: ae_api_init,
ae_ds: {},
ae_hub: {}, // was ae_core
ae_m_sponsorships: {},
@@ -79,7 +121,8 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
submenu: {},
};
let loading_results = null;
// let loading_results = null;
// First do a site_domain look up to check if it is valid and get the account_id.
if (!url.host) {
@@ -95,7 +138,11 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
// const res = await fetch(`https://api.example.com/data`);
loading_results = await core_func.handle_load_ae_obj_id__site_domain({api_cfg: ae_api_tmp, fqdn: url.host, try_cache: false})
ae_api_tmp['account_id'] = null;
ae_api_tmp['headers']['x-account-id'] = '';
ae_api_tmp['headers']['x-no-account-id'] = 'nothing to see here'
account_id = await core_func.handle_load_ae_obj_id__site_domain({api_cfg: ae_api_tmp, fqdn: url.host, try_cache: false})
.then(function (site_domain_results) {
if (site_domain_results) {
@@ -118,9 +165,13 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
ae_loc_tmp['site_style_href'] = site_domain_results.style_href;
ae_loc_tmp['site_google_tracking_id'] = site_domain_results.google_tracking_id;
ae_loc_tmp['site_cfg_json'] = site_domain_results.cfg_json;
console.log(`root layout.ts: Returning account_id = `, site_domain_results.account_id_random);
return site_domain_results.account_id_random;
}
return true;
return null;
});
// .then(async function (results) {
// return true;
@@ -129,17 +180,29 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
// return true;
// });
// if (browser) {
// localStorage.setItem('ae_account_id', await account_id);
// }
// NOTE: We need to wait for the account_id to be returned before we can continue. It is required for the api_cfg.
let ds_type: null|string = 'json';
ds_code = 'hub__page__access_code_li_json';
ds_code_li[ds_code] = await core_func.handle_load_ae_obj_code__data_store({api_cfg: ae_api_tmp, code: ds_code, data_type: ds_type})
.then(function (ds_results) {
if (ds_results) {
// console.log(`ae_ ds_results = `, ds_results);
return ds_results;
}
if (account_id) {
console.log(`INFO: ae_ account_id = `, account_id);
ds_code_li[ds_code] = core_func.handle_load_ae_obj_code__data_store({api_cfg: ae_api_tmp, code: ds_code, data_type: ds_type})
.then(function (ds_results) {
if (ds_results) {
// console.log(`ae_ ds_results = `, ds_results);
return ds_results;
}
}
);
} else {
console.log(`ERROR: No account_id was returned!!!`);
// return false;
}
);
// ds_type = 'text';
// ds_code = 'hub__site__appshell_header';
@@ -159,7 +222,7 @@ export async function load({ fetch, params, parent, route, url }) { // params, r
ae_acct['api'] = ae_api_tmp;
ae_acct['loc'] = ae_loc_tmp;
ae_acct['ds'] = ds_code_li;
console.log(`ae_acct = `, ae_acct);
// console.log(`ae_acct = `, ae_acct);
data_struct.ae_acct[ae_loc_tmp.account_id] = ae_acct;
// data_struct.ae_acct = ae_acct;