update
This commit is contained in:
parent
ca24591d9d
commit
995a8b3476
16 changed files with 789 additions and 246 deletions
src
54
src/api.rs
54
src/api.rs
|
@ -1,10 +1,11 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use dioxus::signals::Readable;
|
||||
use dioxus::signals::{Readable, Writable};
|
||||
use dioxus_sdk::storage::use_persistent;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::setup::Credentials;
|
||||
use crate::try_recover_api;
|
||||
|
||||
use reqwest::header::{HeaderMap, HeaderValue};
|
||||
use reqwest::Client;
|
||||
|
@ -12,11 +13,11 @@ use serde::de::DeserializeOwned;
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub fn get_item(item: &str) -> Option<Item> {
|
||||
crate::API
|
||||
.read()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.get_item(item.to_string())
|
||||
if let Some(api) = crate::API.read().as_ref() {
|
||||
api.get_item(item.to_string())
|
||||
} else {
|
||||
try_recover_api().get_item(item.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn api_get_auth<T>(path: String) -> Result<T, reqwest::Error>
|
||||
|
@ -88,8 +89,18 @@ impl API {
|
|||
let mut items: HashMap<String, Vec<Item>> =
|
||||
api_get_auth("/items".to_string()).await.unwrap();
|
||||
let items = items.remove("items").unwrap();
|
||||
let locations = api_get_auth("/locations".to_string()).await.unwrap();
|
||||
let flow_info = api_get_auth("/flows".to_string()).await.unwrap();
|
||||
let locations: HashMap<String, Location> =
|
||||
api_get_auth("/locations".to_string()).await.unwrap();
|
||||
let flow_info: HashMap<String, FlowInfo> =
|
||||
api_get_auth("/flows".to_string()).await.unwrap();
|
||||
|
||||
let mut p_items = use_persistent("api_items", || Vec::<Item>::new());
|
||||
p_items.set(items.clone());
|
||||
let mut p_locations =
|
||||
use_persistent("api_locations", || HashMap::<String, Location>::new());
|
||||
p_locations.set(locations.clone());
|
||||
let mut p_flowinfo = use_persistent("api_flow_info", || HashMap::<String, FlowInfo>::new());
|
||||
p_flowinfo.set(flow_info.clone());
|
||||
|
||||
Self {
|
||||
instance,
|
||||
|
@ -99,6 +110,24 @@ impl API {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn try_recover() -> Self {
|
||||
Self {
|
||||
instance: use_persistent("creds", || Credentials::default())
|
||||
.read()
|
||||
.instance_url
|
||||
.clone(),
|
||||
items: use_persistent("api_items", || Vec::<Item>::new())
|
||||
.read()
|
||||
.clone(),
|
||||
locations: use_persistent("api_locations", || HashMap::<String, Location>::new())
|
||||
.read()
|
||||
.clone(),
|
||||
flow_info: use_persistent("api_flow_info", || HashMap::<String, FlowInfo>::new())
|
||||
.read()
|
||||
.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_items(&self) -> &[Item] {
|
||||
&self.items
|
||||
}
|
||||
|
@ -358,7 +387,7 @@ impl API {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
||||
pub struct FlowInfo {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
|
@ -376,6 +405,13 @@ pub struct Item {
|
|||
pub variants: HashMap<String, ItemVariant>,
|
||||
}
|
||||
|
||||
impl Item {
|
||||
pub fn get_variant(&self, variant: &str) -> Option<ItemVariant> {
|
||||
let var = self.variants.iter().find(|x| *x.0 == variant)?;
|
||||
Some(var.1.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
||||
pub struct ItemVariant {
|
||||
pub item: String,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue