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

@@ -17,7 +17,9 @@ async function handle_load_ae_obj_id__site_domain({ api_cfg, fqdn, try_cache=fal
let no_account_id = false;
if (!api_cfg.account_id) {
no_account_id = true;
// api_cfg.headers['x_account_id'] = 'nothing here';
}
no_account_id = true;
let params = {};
@@ -30,7 +32,7 @@ async function handle_load_ae_obj_id__site_domain({ api_cfg, fqdn, try_cache=fal
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: 1
log_lvl: 0
})
.then(function (site_domain_obj_get_result) {
if (site_domain_obj_get_result) {

View File

@@ -137,7 +137,7 @@ export let ae_app_local_data_struct: key_val = {
'testing': {},
},
}
console.log(`AE Stores - App Local Storage Data:`, ae_app_local_data_struct);
// console.log(`AE Stores - App Local Storage Data:`, ae_app_local_data_struct);
// This works, but does not uses local storage:
// export let ae_loc = writable(ae_app_local_data_struct);

View File

@@ -92,12 +92,10 @@ export let get_object = async function get_object({api_cfg, endpoint='', headers
/* other custom settings */
});
axios_api.defaults.headers = api_cfg['headers'];
console.log(axios_api.defaults.headers);
console.log('axios_api.defaults.headers:', axios_api.defaults.headers);
console.log('Additional headers:', headers);
// console.log(headers);
console.log('Clean the headers. No _underscores_!')
// console.log('Clean the headers. No _underscores_!')
let headers_cleaned = {};
for (const prop in headers) {
// No underscores allowed in the header parameters!
@@ -115,7 +113,7 @@ export let get_object = async function get_object({api_cfg, endpoint='', headers
}
}
headers = headers_cleaned;
console.log('Cleaned additional headers:', headers);
console.log('All headers cleaned:', headers);
console.log('URL params:');
for (const prop in params) {
@@ -152,7 +150,7 @@ export let get_object = async function get_object({api_cfg, endpoint='', headers
let percent_completed = Math.round(
(progressEvent.loaded * 100) / progressEvent.total
);
console.log('GET Data Timestamp:', progressEvent.timeStamp, 'Total:', progressEvent.total, 'Loaded:', progressEvent.loaded, 'Percent Completed', percent_completed);
// console.log('GET Data Timestamp:', progressEvent.timeStamp, 'Total:', progressEvent.total, 'Loaded:', progressEvent.loaded, 'Percent Completed', percent_completed);
temp_get_object_percent_completed = percent_completed;
}
@@ -615,7 +613,17 @@ export let get_ae_obj_id_crud = async function get_ae_obj_id_crud({
if (no_account_id) {
headers['x-no-account-id'] = 'Nothing to See Here';
delete headers['x-account-id'];
delete api_cfg['headers']['x-account-id'];
// headers['x-account-id'] = null;
// headers['x-account-id'] = '_XY7DXtc9Mxx';
// params['x_no_account_id_token'] = 'Nothing to See Here';
// Remove the x-account-id header
// if (headers['x-account-id']) {
// delete headers['x-account-id'];
// }
// headers['x-account-id'] = null;
// headers['x-no-account-id-token'] = 'Nothing to See Here'; // get_object() will fix the underscores to dashes
}
@@ -1328,13 +1336,13 @@ export let get_data_store_obj_w_code = async function get_data_store_obj_w_code(
if (data_store_obj_get_promise === false) {
console.log('Data Store - RUN AGAIN WITH BACKUP');
let original_api_base_url = api_cfg['base_url'];
// let original_api_base_url = api_cfg['base_url'];
let temp_api = api_cfg;
temp_api['base_url'] = temp_api['base_url_bak']
// let temp_api = api_cfg;
// temp_api['base_url'] = temp_api['base_url_bak']
data_store_obj_get_promise = await api.get_object({api_cfg: temp_api, endpoint: endpoint, headers: headers, params: params, timeout: 6000, log_lvl: log_lvl});
temp_api['base_url'] = original_api_base_url;
// data_store_obj_get_promise = await api.get_object({api_cfg: temp_api, endpoint: endpoint, headers: headers, params: params, timeout: 6000, log_lvl: log_lvl});
// temp_api['base_url'] = original_api_base_url;
}
let data_store_obj = data_store_obj_get_promise;

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;