feat: added a session struct to communicate with the api and added it as context
This commit is contained in:
parent
63beba63c8
commit
8c756a4eb6
|
@ -17,6 +17,12 @@ version = "1.0.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.75"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anymap2"
|
name = "anymap2"
|
||||||
version = "0.13.0"
|
version = "0.13.0"
|
||||||
|
@ -121,6 +127,7 @@ dependencies = [
|
||||||
name = "frontend"
|
name = "frontend"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"gloo 0.10.0",
|
"gloo 0.10.0",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
"web-sys",
|
"web-sys",
|
||||||
|
|
|
@ -11,3 +11,4 @@ yew-router = "0.17.0"
|
||||||
web-sys = { version = "0.3.64", features = ["HtmlInputElement"] }
|
web-sys = { version = "0.3.64", features = ["HtmlInputElement"] }
|
||||||
wasm-bindgen = "0.2.87"
|
wasm-bindgen = "0.2.87"
|
||||||
gloo = "0.10.0"
|
gloo = "0.10.0"
|
||||||
|
anyhow = "1.0.75"
|
|
@ -0,0 +1,34 @@
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq)]
|
||||||
|
pub struct Session {
|
||||||
|
base_url: String,
|
||||||
|
auth_token: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Session {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
base_url: "https://api.nerdcult.net".to_owned(),
|
||||||
|
auth_token: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Session {
|
||||||
|
pub fn new(base_url: String) -> Self {
|
||||||
|
Self {
|
||||||
|
base_url,
|
||||||
|
auth_token: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn login(&mut self, username: &str, password: &str) -> Result<()> {
|
||||||
|
gloo::console::log!("Login: ", username, " || ", password);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_authenticated(&self) -> bool {
|
||||||
|
self.auth_token.is_some()
|
||||||
|
}
|
||||||
|
}
|
42
src/main.rs
42
src/main.rs
|
@ -1,10 +1,10 @@
|
||||||
|
mod backend;
|
||||||
mod content;
|
mod content;
|
||||||
mod state;
|
|
||||||
mod topbar;
|
mod topbar;
|
||||||
|
|
||||||
|
use backend::Session;
|
||||||
use content::{error, home::Home};
|
use content::{error, home::Home};
|
||||||
use state::State;
|
use std::ops::Deref;
|
||||||
use std::rc::Rc;
|
|
||||||
use topbar::TopBar;
|
use topbar::TopBar;
|
||||||
use yew::prelude::*;
|
use yew::prelude::*;
|
||||||
use yew_router::prelude::*;
|
use yew_router::prelude::*;
|
||||||
|
@ -34,19 +34,37 @@ fn switch(routes: Route) -> Html {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq)]
|
||||||
|
pub struct Callbacks {
|
||||||
|
pub sign_in_callback: Callback<(String, String)>,
|
||||||
|
}
|
||||||
|
|
||||||
#[function_component]
|
#[function_component]
|
||||||
fn App() -> Html {
|
fn App() -> Html {
|
||||||
let state = use_memo(|_| State::default(), ());
|
let state = use_state(Session::default);
|
||||||
|
|
||||||
|
let cloned_state = state.clone();
|
||||||
|
let callbacks = use_state(move || Callbacks {
|
||||||
|
sign_in_callback: Callback::from(move |data: (String, String)| {
|
||||||
|
let mut new_state = cloned_state.deref().clone();
|
||||||
|
new_state
|
||||||
|
.login(data.0.as_str(), data.1.as_str())
|
||||||
|
.expect("Login failed");
|
||||||
|
cloned_state.set(new_state);
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
html! {
|
html! {
|
||||||
<ContextProvider<Rc<State>> context={state}>
|
<ContextProvider<Session> context={state.deref().clone()}>
|
||||||
<TopBar/>
|
<ContextProvider<Callbacks> context={callbacks.deref().clone()}>
|
||||||
<main>
|
<TopBar/>
|
||||||
<BrowserRouter>
|
<main>
|
||||||
<Switch<Route> render={switch} />
|
<BrowserRouter>
|
||||||
</BrowserRouter>
|
<Switch<Route> render={switch} />
|
||||||
</main>
|
</BrowserRouter>
|
||||||
</ContextProvider<Rc<State>>>
|
</main>
|
||||||
|
</ContextProvider<Callbacks>>
|
||||||
|
</ContextProvider<Session>>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
src/state.rs
10
src/state.rs
|
@ -1,10 +0,0 @@
|
||||||
#[derive(Clone, PartialEq)]
|
|
||||||
pub struct State {
|
|
||||||
pub auth_token: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for State {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self { auth_token: None }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,6 +2,7 @@ mod signin;
|
||||||
mod signup;
|
mod signup;
|
||||||
|
|
||||||
use crate::topbar::Tab;
|
use crate::topbar::Tab;
|
||||||
|
use crate::Callbacks;
|
||||||
use signin::SignIn;
|
use signin::SignIn;
|
||||||
use signup::SignUp;
|
use signup::SignUp;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
@ -49,6 +50,7 @@ impl Reducible for State {
|
||||||
#[function_component]
|
#[function_component]
|
||||||
pub fn TopBar() -> Html {
|
pub fn TopBar() -> Html {
|
||||||
let state = use_reducer(State::default);
|
let state = use_reducer(State::default);
|
||||||
|
let callbacks = use_context::<Callbacks>().expect("Callbacks context not available!");
|
||||||
|
|
||||||
let sign_in_onclick = {
|
let sign_in_onclick = {
|
||||||
let state = state.clone();
|
let state = state.clone();
|
||||||
|
@ -75,7 +77,7 @@ pub fn TopBar() -> Html {
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
if state.sign_in {
|
if state.sign_in {
|
||||||
<SignIn/>
|
<SignIn callback={callbacks.sign_in_callback}/>
|
||||||
}
|
}
|
||||||
|
|
||||||
if state.sign_up {
|
if state.sign_up {
|
||||||
|
|
|
@ -9,8 +9,13 @@ struct Data {
|
||||||
pub password: String,
|
pub password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Properties, PartialEq)]
|
||||||
|
pub struct Props {
|
||||||
|
pub callback: Callback<(String, String)>,
|
||||||
|
}
|
||||||
|
|
||||||
#[function_component]
|
#[function_component]
|
||||||
pub fn SignIn() -> Html {
|
pub fn SignIn(props: &Props) -> Html {
|
||||||
let state = use_state(|| Data::default());
|
let state = use_state(|| Data::default());
|
||||||
|
|
||||||
let cloned_state = state.clone();
|
let cloned_state = state.clone();
|
||||||
|
@ -40,12 +45,13 @@ pub fn SignIn() -> Html {
|
||||||
});
|
});
|
||||||
|
|
||||||
let cloned_state = state.clone();
|
let cloned_state = state.clone();
|
||||||
|
let callback = props.callback.clone();
|
||||||
let onsubmit = Callback::from(move |event: SubmitEvent| {
|
let onsubmit = Callback::from(move |event: SubmitEvent| {
|
||||||
event.prevent_default();
|
event.prevent_default();
|
||||||
|
|
||||||
let data = cloned_state.deref().clone();
|
let data = cloned_state.deref().clone();
|
||||||
gloo::console::log!("username: ", data.username);
|
|
||||||
gloo::console::log!("password: ", data.password);
|
callback.emit((data.username, data.password));
|
||||||
});
|
});
|
||||||
|
|
||||||
html! {
|
html! {
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
mod guest;
|
mod guest;
|
||||||
mod user;
|
mod user;
|
||||||
|
|
||||||
use crate::state::State;
|
use crate::backend::Session;
|
||||||
use std::rc::Rc;
|
|
||||||
use yew::prelude::*;
|
use yew::prelude::*;
|
||||||
|
|
||||||
#[derive(Properties, PartialEq)]
|
#[derive(Properties, PartialEq)]
|
||||||
|
@ -27,10 +26,10 @@ fn Tab(props: &TabProps) -> Html {
|
||||||
|
|
||||||
#[function_component]
|
#[function_component]
|
||||||
pub fn TopBar() -> Html {
|
pub fn TopBar() -> Html {
|
||||||
let ctx = use_context::<Rc<State>>().expect("No context found!");
|
let session = use_context::<Session>().unwrap_or_default();
|
||||||
|
|
||||||
html! {
|
html! {
|
||||||
if ctx.auth_token.is_some() {
|
if session.is_authenticated() {
|
||||||
<user::TopBar/>
|
<user::TopBar/>
|
||||||
} else {
|
} else {
|
||||||
<guest::TopBar/>
|
<guest::TopBar/>
|
||||||
|
|
Loading…
Reference in New Issue