Skip to content

Commit

Permalink
refactor(ui): pass server address to api
Browse files Browse the repository at this point in the history
  • Loading branch information
DefectingCat committed Feb 12, 2025
1 parent 39dfa8b commit 49558bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
18 changes: 8 additions & 10 deletions venus-ui/src/components/home_page/subscripiton.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
use crate::{
api::{axios, BaseResponse, RequestApi},
components::subscription_card::{SubCardForm, SubscriptionCard},
consts::USER_KEY,
hooks::use_global_user,
utils::error_to_string,
User,
};
use gloo::{
net::http::Method,
storage::{LocalStorage, Storage},
};
use gloo::net::http::Method;
use leptos::{logging, prelude::*};
use web_sys::MouseEvent;

async fn add_subscription(subs_form: SubCardForm) -> Result<BaseResponse<()>, String> {
let user = LocalStorage::get::<User>(USER_KEY).unwrap_or_default();
let address = format!("{}{}", user.server, RequestApi::AddSubscription);
async fn add_subscription(subs_form: (SubCardForm, User)) -> Result<BaseResponse<()>, String> {
let address = format!("{}{}", subs_form.1.server, RequestApi::AddSubscription);
let resquest = axios(&address, Method::POST)
.header("Content-Type", "application/json")
.body(serde_json::to_string(&subs_form).map_err(error_to_string)?)
.body(serde_json::to_string(&subs_form.0).map_err(error_to_string)?)
.map_err(error_to_string)?
.send()
.await;
Expand All @@ -35,9 +31,11 @@ pub fn Subscription() -> impl IntoView {
url: "".into(),
});

let user = use_global_user();

let form_ref: NodeRef<leptos::html::Form> = NodeRef::new();
let add_action: Action<SubCardForm, Result<BaseResponse<()>, String>, SyncStorage> =
Action::new_unsync(|form: &SubCardForm| add_subscription(form.clone()));
Action::new_unsync(move |form: &SubCardForm| add_subscription((form.clone(), user.get())));
let add_loading = add_action.pending();
let add_result = add_action.value();
let handle_submit = move |e: MouseEvent| {
Expand Down
10 changes: 8 additions & 2 deletions venus-ui/src/hooks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use leptos::prelude::use_context;
use leptos::prelude::{use_context, RwSignal};

use crate::GlobalUI;
use crate::{GlobalUI, User};

pub fn use_global_ui() -> GlobalUI {
use_context::<GlobalUI>().expect("GlobalUI state is not set")
}

pub fn use_global_user() -> RwSignal<User> {
use_context::<GlobalUI>()
.expect("GlobalUI state is not set")
.user
}

0 comments on commit 49558bd

Please sign in to comment.