use crate::get_host_info;
use bounce::Atom;
use dialtone_common::rest::sites::site_data::PublicSiteInfo;
use dialtone_reqwest::site_connection::SiteConnection;
use gloo::console::log;
use gloo_storage::Storage;
const SITE_CONNECTION_STORAGE: &str = "SiteConnection";
const SITE_INFO_STORAGE: &str = "SiteInfo";
#[derive(PartialEq, Atom)]
pub struct AppSiteConnection(pub SiteConnection);
impl Default for AppSiteConnection {
fn default() -> Self {
gloo_storage::LocalStorage::get(SITE_CONNECTION_STORAGE).map_or_else(
|_| {
log!("constructing new site connection");
let (site_connection, host_name) = get_host_info();
Self(SiteConnection::new_site(
&site_connection.host_addr,
&host_name,
))
},
|v| Self(v),
)
}
}
impl AppSiteConnection {
pub fn save(&self) {
let result = gloo_storage::LocalStorage::set(SITE_CONNECTION_STORAGE, &self.0);
if result.is_err() {
log!("WARNING: unable to save site connection to local storage.");
}
}
pub fn reset() {
gloo_storage::LocalStorage::delete(SITE_CONNECTION_STORAGE);
}
}
#[derive(PartialEq, Atom)]
pub struct AppPublicSiteInfo(pub Option<PublicSiteInfo>);
impl Default for AppPublicSiteInfo {
fn default() -> Self {
gloo_storage::LocalStorage::get(SITE_INFO_STORAGE)
.map_or_else(|_| Self(None), |v| Self(Some(v)))
}
}
impl AppPublicSiteInfo {
pub fn save(&self) {
let result = gloo_storage::LocalStorage::set(SITE_INFO_STORAGE, &self.0);
if result.is_err() {
log!("WARNING: unable to save site information to local storage.");
}
}
pub fn reset() {
gloo_storage::LocalStorage::delete(SITE_INFO_STORAGE);
}
}