Initial rebuild of position size calculator #37
@ -1,6 +1,6 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use gloo::storage::Storage;
|
use gloo::storage::{errors::StorageError, Storage};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use yew::Reducible;
|
use yew::Reducible;
|
||||||
|
|
||||||
@ -32,6 +32,7 @@ impl Default for Account {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub enum AccountAction {
|
pub enum AccountAction {
|
||||||
|
Load,
|
||||||
SetPlaces { places: usize },
|
SetPlaces { places: usize },
|
||||||
SetCurrency { currency: Currency },
|
SetCurrency { currency: Currency },
|
||||||
SetExchangeRates { exchange_rates: ExchangeRates },
|
SetExchangeRates { exchange_rates: ExchangeRates },
|
||||||
@ -44,7 +45,9 @@ impl Reducible for Account {
|
|||||||
type Action = AccountAction;
|
type Action = AccountAction;
|
||||||
|
|
||||||
fn reduce(self: Rc<Self>, action: Self::Action) -> Rc<Self> {
|
fn reduce(self: Rc<Self>, action: Self::Action) -> Rc<Self> {
|
||||||
match action {
|
let account = match action {
|
||||||
|
AccountAction::Load => Self::load(),
|
||||||
|
|
||||||
AccountAction::SetPlaces { places } => Self {
|
AccountAction::SetPlaces { places } => Self {
|
||||||
places,
|
places,
|
||||||
..(*self).clone()
|
..(*self).clone()
|
||||||
@ -74,8 +77,10 @@ impl Reducible for Account {
|
|||||||
position_risk: risk,
|
position_risk: risk,
|
||||||
..(*self).clone()
|
..(*self).clone()
|
||||||
},
|
},
|
||||||
}
|
};
|
||||||
.into()
|
|
||||||
|
account.save();
|
||||||
|
account.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,10 +88,17 @@ impl Account {
|
|||||||
pub fn load() -> Self {
|
pub fn load() -> Self {
|
||||||
match gloo::storage::LocalStorage::get("trading.account") {
|
match gloo::storage::LocalStorage::get("trading.account") {
|
||||||
Ok(stored) => stored,
|
Ok(stored) => stored,
|
||||||
Err(err) => {
|
Err(err) => match err {
|
||||||
|
StorageError::KeyNotFound(_) => {
|
||||||
|
log::info!("No stored trading account found, using defaults");
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => {
|
||||||
log::error!("Failed to retrieve trading account from local storage: {err:?}");
|
log::error!("Failed to retrieve trading account from local storage: {err:?}");
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user