Skip to content

Commit

Permalink
Add hook to determine location (#101)
Browse files Browse the repository at this point in the history
* Add hook to determine location

* feat: create action transfer as community (#112)

* feat: deposit from Kusama to Kreivo (#114)

---------

Co-authored-by: Brayan Vargas <[email protected]>
  • Loading branch information
ail3ngrimaldi and b-avb authored Oct 10, 2024
1 parent 97921a6 commit 8944002
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/hooks/use_location.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use dioxus::prelude::*;
use serde::{Deserialize, Serialize};
use gloo::storage::{LocalStorage, Storage};
use wasm_bindgen_futures::spawn_local;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Location {
pub city: String,
pub country: String,
}

#[derive(Clone, Debug)]
pub enum LocationError {
FetchError,
StorageError,
}

pub fn use_location() -> UseLocationState {
let mut location = use_signal(|| None);
let mut error = use_signal(|| None);

use_coroutine(move |_:UnboundedReceiver<()>| async move {
if let Some(stored_location) = LocalStorage::get("user_location").ok() {
location.set(Some(stored_location));
log::info!("{}", "Location loaded from storage");
} else {
match fetch_and_store_location().await {
Ok(loc) => {
location.set(Some(loc.clone()));
error.set(None);
log::info!("{}", &format!("Location fetched: {}, {}", loc.city, loc.country));
}
Err(err) => {
location.set(None);
error.set(Some(err.clone()));
log::error!("{}", &format!("Error fetching location: {:?}", err));
}
}
}
});

UseLocationState { location, error }
}

#[derive(Clone, Copy)]
pub struct UseLocationState {
location: Signal<Option<Location>>,
error: Signal<Option<LocationError>>,
}

impl UseLocationState {
pub fn get_location(&self) -> Option<Location> {
self.location.read().clone()
}

pub fn get_error(&self) -> Option<LocationError> {
self.error.read().clone()
}

pub fn is_loading(&self) -> bool {
self.location.read().is_none() && self.error.read().is_none()
}
}

async fn fetch_and_store_location() -> Result<Location, LocationError> {
let client = reqwest::Client::new();
let res = client
.get("https://ipapi.co/json/")
.send()
.await
.map_err(|_| LocationError::FetchError)?;

let location: Location = res.json().await.map_err(|_| LocationError::FetchError)?;

LocalStorage::set("user_location", &location).map_err(|_| LocationError::StorageError)?;

Ok(location)
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub mod hooks {
pub mod use_deposit;
pub mod use_initiative;
pub mod use_language;
pub mod use_location;
pub mod use_market_client;
pub mod use_notification;
pub mod use_onboard;
Expand Down

0 comments on commit 8944002

Please sign in to comment.