app: migrate user creation to GraphQL

This commit is contained in:
Valentin Tolmer 2021-08-30 09:50:10 +02:00 committed by nitnelave
parent 37f61ce212
commit 2aca9dbe62
3 changed files with 44 additions and 33 deletions

View File

@ -0,0 +1,6 @@
mutation CreateUser($user: UserInput!) {
createUser(user: $user) {
id
creationDate
}
}

View File

@ -1,7 +1,7 @@
use crate::cookies::set_cookie; use crate::cookies::set_cookie;
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use graphql_client::GraphQLQuery; use graphql_client::GraphQLQuery;
use lldap_model::*; use lldap_model::{login, registration, JWTClaims};
use yew::callback::Callback; use yew::callback::Callback;
use yew::format::Json; use yew::format::Json;
@ -231,16 +231,4 @@ impl HostService {
"Could not logout", "Could not logout",
) )
} }
pub fn create_user(
request: CreateUserRequest,
callback: Callback<Result<()>>,
) -> Result<FetchTask> {
call_server_empty_response_with_error_message(
"/api/users/create",
&request,
callback,
"Could not create a user",
)
}
} }

View File

@ -1,6 +1,7 @@
use crate::api::HostService; use crate::api::HostService;
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use lldap_model::*; use graphql_client::GraphQLQuery;
use lldap_model::{opaque, registration};
use yew::prelude::*; use yew::prelude::*;
use yew::services::{fetch::FetchTask, ConsoleService}; use yew::services::{fetch::FetchTask, ConsoleService};
use yew_router::{ use yew_router::{
@ -8,6 +9,15 @@ use yew_router::{
route::Route, route::Route,
}; };
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "../schema.graphql",
query_path = "queries/create_user.graphql",
response_derives = "Debug",
custom_scalars_module = "crate::graphql"
)]
pub struct CreateUser;
pub struct CreateUserForm { pub struct CreateUserForm {
link: ComponentLink<Self>, link: ComponentLink<Self>,
route_dispatcher: RouteAgentDispatcher, route_dispatcher: RouteAgentDispatcher,
@ -19,7 +29,7 @@ pub struct CreateUserForm {
} }
pub enum Msg { pub enum Msg {
CreateUserResponse(Result<()>), CreateUserResponse(Result<create_user::ResponseData>),
SubmitForm, SubmitForm,
SuccessfulCreation, SuccessfulCreation,
RegistrationStartResponse(Result<Box<registration::ServerRegistrationStartResponse>>), RegistrationStartResponse(Result<Box<registration::ServerRegistrationStartResponse>>),
@ -35,26 +45,33 @@ impl CreateUserForm {
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<()> { fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<()> {
match msg { match msg {
Msg::SubmitForm => { Msg::SubmitForm => {
let req = CreateUserRequest { let req = create_user::Variables {
user_id: get_element("username") user: create_user::UserInput {
id: get_element("username")
.filter(not_empty) .filter(not_empty)
.ok_or_else(|| anyhow!("Missing username"))?, .ok_or_else(|| anyhow!("Missing username"))?,
email: get_element("email") email: get_element("email")
.filter(not_empty) .filter(not_empty)
.ok_or_else(|| anyhow!("Missing email"))?, .ok_or_else(|| anyhow!("Missing email"))?,
display_name: get_element("displayname").filter(not_empty), displayName: get_element("displayname").filter(not_empty),
first_name: get_element("firstname").filter(not_empty), firstName: get_element("firstname").filter(not_empty),
last_name: get_element("lastname").filter(not_empty), lastName: get_element("lastname").filter(not_empty),
},
}; };
self._task = Some( self._task = Some(HostService::graphql_query::<CreateUser>(
HostService::create_user(req, self.link.callback(Msg::CreateUserResponse)) req,
.context("Error trying to create user")?, self.link.callback(Msg::CreateUserResponse),
); "Error trying to create user",
)?);
} }
Msg::CreateUserResponse(r) => { Msg::CreateUserResponse(r) => {
if r.is_err() { match r {
return r; Err(e) => return Err(e),
} Ok(r) => ConsoleService::log(&format!(
"Created user '{}' at '{}'",
&r.create_user.id, &r.create_user.creation_date
)),
};
let user_id = get_element("username") let user_id = get_element("username")
.filter(not_empty) .filter(not_empty)
.ok_or_else(|| anyhow!("Missing username"))?; .ok_or_else(|| anyhow!("Missing username"))?;
@ -183,7 +200,7 @@ impl Component for CreateUserForm {
<label for="password">{"Password:"}</label> <label for="password">{"Password:"}</label>
<input type="password" id="password" /> <input type="password" id="password" />
</div> </div>
<button type="submit">{"Login"}</button> <button type="submit">{"Submit"}</button>
<div> <div>
{ if let Some(e) = &self.error { { if let Some(e) = &self.error {
html! { e.to_string() } html! { e.to_string() }