1- use serde:: Deserialize ;
2- use web_sys:: { Storage , Window } ;
1+ use serde:: { Deserialize , Serialize } ;
2+ use web_sys:: Window ;
33
44use crate :: { Api , Coordinates } ;
55
@@ -9,6 +9,12 @@ struct GeoLocationApiData {
99 longitude : f32 ,
1010}
1111
12+ #[ derive( Debug , Clone , PartialEq , Deserialize , Default , Serialize ) ]
13+ struct LocationStorage {
14+ latitude : f32 ,
15+ longitude : f32 ,
16+ }
17+
1218const STORAGE_KEY : & str = "coordinates" ;
1319
1420async fn fetch_location ( ) -> GeoLocationApiData {
@@ -30,11 +36,15 @@ fn fetch_from_local(key: &str) -> Option<Coordinates> {
3036 if let Some ( storage) = maybe_storage {
3137 let maybe_data = storage. get_item ( key) . unwrap ( ) ;
3238 if let Some ( data) = maybe_data {
33- let coordinates_result: Result < Coordinates , serde_json:: Error > =
39+ let coordinates_result: Result < LocationStorage , serde_json:: Error > =
3440 serde_json:: from_str ( & data) ;
3541
3642 if coordinates_result. is_ok ( ) {
37- return Some ( coordinates_result. unwrap ( ) ) ;
43+ let location_storage = coordinates_result. unwrap ( ) ;
44+ return Some ( Coordinates {
45+ latitude : location_storage. latitude ,
46+ longitude : location_storage. longitude ,
47+ } ) ;
3848 }
3949 }
4050 }
@@ -45,7 +55,12 @@ fn store_into_local(key: &str, coordinates: Coordinates) {
4555 let window: Window = web_sys:: window ( ) . unwrap ( ) ;
4656 let maybe_storage = window. local_storage ( ) . unwrap ( ) ;
4757 if let Some ( storage) = maybe_storage {
48- let data_string = serde_json:: to_string ( & coordinates) . unwrap ( ) ;
58+ let location_storage = LocationStorage {
59+ latitude : coordinates. latitude ,
60+ longitude : coordinates. longitude ,
61+ } ;
62+
63+ let data_string = serde_json:: to_string ( & location_storage) . unwrap ( ) ;
4964 storage. set_item ( key, & data_string) . unwrap ( ) ;
5065 }
5166}
@@ -56,6 +71,7 @@ pub async fn set_location(api: Api<'_>) {
5671
5772 if let Some ( local) = from_location {
5873 api. set_coordinates ( local) ;
74+ return ;
5975 }
6076
6177 let data = fetch_location ( ) . await ;
0 commit comments