Skip to content

Commit

Permalink
feat(client): introduce "UserRegister" for client (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai authored Jul 12, 2024
1 parent 1d7f8c4 commit f8541bf
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ name = "townhall_client"
path = "src/lib.rs"

[dependencies]
chrono = { workspace = true }
graphql_client = { workspace = true, features = ["reqwest"] }
pxid = { workspace = true, features = ["serde"] }
reqwest = { version = "0.11", features = ["blocking", "json"] }
serde = { workspace = true }

core = { path = "../core" }
20 changes: 20 additions & 0 deletions crates/client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
mod modules;

pub use modules::*;

use auth::AuthClient;

pub struct Client {
pub auth: AuthClient,
}

impl Default for Client {
fn default() -> Self {
Self::new()
}
}

impl Client {
pub fn new() -> Self {
Self {
auth: AuthClient::new(),
}
}
}
32 changes: 32 additions & 0 deletions crates/client/src/modules/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
pub mod token_create;
pub mod user_register;

use reqwest::Client;

pub struct AuthClient {
client: Client,
}

impl Default for AuthClient {
fn default() -> Self {
Self::new()
}
}

impl AuthClient {
pub fn new() -> Self {
Self {
client: Client::new(),
}
}

pub async fn token_create(&self, email: String, password: String) -> token_create::TokenCreate {
token_create::token_create(&self.client, email, password).await
}

pub async fn user_register(
&self,
input: user_register::UserRegisterInput,
) -> user_register::UserRegister {
user_register::user_register(&self.client, input).await
}
}
18 changes: 13 additions & 5 deletions crates/client/src/modules/auth/token_create/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ use graphql_client::reqwest::post_graphql;
use graphql_client::GraphQLQuery;
use reqwest::Client;

use token_create::{TokenCreateTokenCreateError, TokenCreateTokenCreateToken};

#[derive(GraphQLQuery)]
#[graphql(
response_derives = "Debug",
schema_path = "schema.json",
query_path = "src/modules/auth/token_create/TokenCreate.gql"
)]
pub struct TokenCreate;
pub struct TokenCreate {
pub token: Option<TokenCreateTokenCreateToken>,
pub error: Option<TokenCreateTokenCreateError>,
}

pub async fn token_create(email: String, password: String) -> token_create::TokenCreateTokenCreate {
let client = Client::builder().build().unwrap();
pub async fn token_create(client: &Client, email: String, password: String) -> TokenCreate {
let variables = token_create::Variables { email, password };
let res = post_graphql::<TokenCreate, _>(&client, "http://127.0.0.1:7878/graphql", variables)
let res = post_graphql::<TokenCreate, _>(client, "http://127.0.0.1:7878/graphql", variables)
.await
.unwrap();
let data = res.data.unwrap().token_create;

res.data.unwrap().token_create
TokenCreate {
token: data.token,
error: data.error,
}
}
17 changes: 17 additions & 0 deletions crates/client/src/modules/auth/user_register/UserRegister.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mutation UserRegister($input: UserRegisterInput!) {
userRegister(input: $input) {
user {
id
name
surname
username
email
createdAt
updatedAt
}
error {
code
message
}
}
}
35 changes: 35 additions & 0 deletions crates/client/src/modules/auth/user_register/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use graphql_client::reqwest::post_graphql;
use graphql_client::GraphQLQuery;
use pxid::Pxid;
use reqwest::Client;

use townhall::user::model::Email;
use user_register::{UserRegisterUserRegisterError, UserRegisterUserRegisterUser};

pub use crate::auth::user_register::user_register::UserRegisterInput;

use crate::DateTime;

#[derive(GraphQLQuery)]
#[graphql(
response_derives = "Debug",
schema_path = "schema.json",
query_path = "src/modules/auth/user_register/UserRegister.gql"
)]
pub struct UserRegister {
pub user: Option<UserRegisterUserRegisterUser>,
pub error: Option<UserRegisterUserRegisterError>,
}

pub async fn user_register(client: &Client, input: UserRegisterInput) -> UserRegister {
let variables = user_register::Variables { input };
let res = post_graphql::<UserRegister, _>(client, "http://127.0.0.1:7878/graphql", variables)
.await
.unwrap();
let data = res.data.unwrap().user_register;

UserRegister {
user: data.user,
error: data.error,
}
}
2 changes: 2 additions & 0 deletions crates/client/src/modules/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod auth;

pub(crate) type DateTime = chrono::DateTime<chrono::Utc>;
8 changes: 6 additions & 2 deletions crates/web/src/views/login.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use leptos::{component, create_action, create_signal, view, IntoView, Show, SignalGet, SignalSet};

use townhall_client::auth::token_create::token_create;
use townhall_client::Client;

use crate::components::text_field::{TextField, TextFieldType};

Expand All @@ -9,7 +9,11 @@ pub fn Login() -> impl IntoView {
let (error_getter, error_setter) = create_signal::<Option<String>>(None);

let submit = create_action(move |_| async move {
let res = token_create("john@test.com".into(), "12345".into()).await;
let client = Client::new();
let res = client
.auth
.token_create("john@test.com".into(), "12345".into())
.await;

if let Some(ref error) = res.error {
error_setter.set(Some(error.message.to_owned()));
Expand Down

0 comments on commit f8541bf

Please sign in to comment.