2021-09-12 09:54:47 +00:00
|
|
|
use crate::{
|
2021-09-17 17:43:22 +00:00
|
|
|
components::{
|
|
|
|
add_user_to_group::AddUserToGroupComponent,
|
|
|
|
router::{AppRoute, NavButton},
|
|
|
|
},
|
2021-09-12 09:54:47 +00:00
|
|
|
infra::api::HostService,
|
|
|
|
};
|
2021-09-16 21:22:11 +00:00
|
|
|
use anyhow::{anyhow, bail, Error, Result};
|
2021-08-30 07:08:42 +00:00
|
|
|
use graphql_client::GraphQLQuery;
|
2021-09-12 09:54:47 +00:00
|
|
|
use yew::{
|
|
|
|
prelude::*,
|
|
|
|
services::{fetch::FetchTask, ConsoleService},
|
|
|
|
};
|
2021-07-05 07:21:13 +00:00
|
|
|
|
2021-08-30 07:08:42 +00:00
|
|
|
#[derive(GraphQLQuery)]
|
|
|
|
#[graphql(
|
|
|
|
schema_path = "../schema.graphql",
|
|
|
|
query_path = "queries/get_user_details.graphql",
|
2021-09-17 17:43:22 +00:00
|
|
|
response_derives = "Debug, Hash, PartialEq, Eq, Clone",
|
2021-09-12 09:54:47 +00:00
|
|
|
custom_scalars_module = "crate::infra::graphql"
|
2021-08-30 07:08:42 +00:00
|
|
|
)]
|
|
|
|
pub struct GetUserDetails;
|
|
|
|
|
2021-09-17 17:43:22 +00:00
|
|
|
pub type User = get_user_details::GetUserDetailsUser;
|
|
|
|
pub type Group = get_user_details::GetUserDetailsUserGroups;
|
2021-08-30 07:08:42 +00:00
|
|
|
|
2021-09-02 16:11:28 +00:00
|
|
|
#[derive(GraphQLQuery)]
|
|
|
|
#[graphql(
|
|
|
|
schema_path = "../schema.graphql",
|
|
|
|
query_path = "queries/update_user.graphql",
|
|
|
|
response_derives = "Debug",
|
|
|
|
variables_derives = "Clone",
|
2021-09-12 09:54:47 +00:00
|
|
|
custom_scalars_module = "crate::infra::graphql"
|
2021-09-02 16:11:28 +00:00
|
|
|
)]
|
|
|
|
pub struct UpdateUser;
|
|
|
|
|
2021-09-16 21:22:11 +00:00
|
|
|
#[derive(GraphQLQuery)]
|
|
|
|
#[graphql(
|
|
|
|
schema_path = "../schema.graphql",
|
|
|
|
query_path = "queries/remove_user_from_group.graphql",
|
|
|
|
response_derives = "Debug",
|
|
|
|
variables_derives = "Clone",
|
|
|
|
custom_scalars_module = "crate::infra::graphql"
|
|
|
|
)]
|
|
|
|
pub struct RemoveUserFromGroup;
|
|
|
|
|
2021-07-05 07:21:13 +00:00
|
|
|
pub struct UserDetails {
|
|
|
|
link: ComponentLink<Self>,
|
2021-09-16 21:22:11 +00:00
|
|
|
/// Which user this is about.
|
2021-07-05 07:21:13 +00:00
|
|
|
username: String,
|
2021-09-16 21:22:11 +00:00
|
|
|
/// The user info. If none, the error is in `error`. If `error` is None, then we haven't
|
|
|
|
/// received the server response yet.
|
2021-09-02 16:11:28 +00:00
|
|
|
user: Option<User>,
|
|
|
|
// Needed for the form.
|
|
|
|
node_ref: NodeRef,
|
2021-09-16 21:22:11 +00:00
|
|
|
/// Error message displayed to the user.
|
|
|
|
error: Option<Error>,
|
|
|
|
/// The request, while we're waiting for the server to reply.
|
2021-09-02 16:11:28 +00:00
|
|
|
update_request: Option<update_user::UpdateUserInput>,
|
2021-09-16 21:22:11 +00:00
|
|
|
/// True iff we just finished updating the user, to display a successful message.
|
2021-09-02 16:11:28 +00:00
|
|
|
update_successful: bool,
|
2021-09-16 21:22:11 +00:00
|
|
|
is_admin: bool,
|
|
|
|
/// The group that we're requesting to remove, if any.
|
|
|
|
group_to_remove: Option<Group>,
|
2021-07-05 07:21:13 +00:00
|
|
|
// Used to keep the request alive long enough.
|
|
|
|
_task: Option<FetchTask>,
|
|
|
|
}
|
|
|
|
|
2021-09-16 21:22:11 +00:00
|
|
|
/// State machine describing the possible transitions of the component state.
|
|
|
|
/// It starts out by fetching the user's details from the backend when loading.
|
2021-07-05 07:21:13 +00:00
|
|
|
pub enum Msg {
|
2021-09-16 21:22:11 +00:00
|
|
|
/// Received the user details response, either the user data or an error.
|
2021-08-30 07:08:42 +00:00
|
|
|
UserDetailsResponse(Result<get_user_details::ResponseData>),
|
2021-09-16 21:22:11 +00:00
|
|
|
/// The user changed some fields and submitted the form for update.
|
|
|
|
SubmitUserUpdateForm,
|
|
|
|
/// Response after updating the user's details.
|
2021-09-02 16:11:28 +00:00
|
|
|
UpdateFinished(Result<update_user::ResponseData>),
|
2021-09-16 21:22:11 +00:00
|
|
|
SubmitRemoveGroup(Group),
|
|
|
|
RemoveGroupResponse(Result<remove_user_from_group::ResponseData>),
|
2021-09-17 17:43:22 +00:00
|
|
|
OnError(Error),
|
|
|
|
OnUserAddedToGroup(Group),
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(yew::Properties, Clone, PartialEq)]
|
|
|
|
pub struct Props {
|
|
|
|
pub username: String,
|
2021-09-16 21:22:11 +00:00
|
|
|
pub is_admin: bool,
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 16:11:28 +00:00
|
|
|
#[allow(clippy::ptr_arg)]
|
|
|
|
fn not_empty(s: &String) -> bool {
|
|
|
|
!s.is_empty()
|
|
|
|
}
|
|
|
|
|
2021-07-05 07:21:13 +00:00
|
|
|
impl UserDetails {
|
|
|
|
fn get_user_details(&mut self) {
|
2021-08-30 07:08:42 +00:00
|
|
|
self._task = HostService::graphql_query::<GetUserDetails>(
|
|
|
|
get_user_details::Variables {
|
2021-08-30 07:49:13 +00:00
|
|
|
id: self.username.clone(),
|
2021-08-30 07:08:42 +00:00
|
|
|
},
|
2021-07-05 07:21:13 +00:00
|
|
|
self.link.callback(Msg::UserDetailsResponse),
|
2021-08-30 07:08:42 +00:00
|
|
|
"Error trying to fetch user details",
|
|
|
|
)
|
|
|
|
.map_err(|e| {
|
|
|
|
ConsoleService::log(&e.to_string());
|
|
|
|
e
|
|
|
|
})
|
|
|
|
.ok();
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
2021-09-16 21:22:11 +00:00
|
|
|
|
|
|
|
fn submit_user_update_form(&mut self) -> Result<bool> {
|
|
|
|
let base_user = self.user.as_ref().unwrap();
|
|
|
|
let mut user_input = update_user::UpdateUserInput {
|
|
|
|
id: self.username.clone(),
|
|
|
|
email: None,
|
|
|
|
displayName: None,
|
|
|
|
firstName: None,
|
|
|
|
lastName: None,
|
|
|
|
};
|
|
|
|
let mut should_send_form = false;
|
|
|
|
let email = get_element("email")
|
|
|
|
.filter(not_empty)
|
|
|
|
.ok_or_else(|| anyhow!("Missing email"))?;
|
|
|
|
if base_user.email != email {
|
|
|
|
should_send_form = true;
|
|
|
|
user_input.email = Some(email);
|
|
|
|
}
|
|
|
|
if base_user.display_name != get_element_or_empty("display_name") {
|
|
|
|
should_send_form = true;
|
|
|
|
user_input.displayName = Some(get_element_or_empty("display_name"));
|
|
|
|
}
|
|
|
|
if base_user.first_name != get_element_or_empty("first_name") {
|
|
|
|
should_send_form = true;
|
|
|
|
user_input.firstName = Some(get_element_or_empty("first_name"));
|
|
|
|
}
|
|
|
|
if base_user.last_name != get_element_or_empty("last_name") {
|
|
|
|
should_send_form = true;
|
|
|
|
user_input.lastName = Some(get_element_or_empty("last_name"));
|
|
|
|
}
|
|
|
|
if !should_send_form {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
self.update_request = Some(user_input.clone());
|
|
|
|
let req = update_user::Variables { user: user_input };
|
|
|
|
self._task = Some(HostService::graphql_query::<UpdateUser>(
|
|
|
|
req,
|
|
|
|
self.link.callback(Msg::UpdateFinished),
|
|
|
|
"Error trying to update user",
|
|
|
|
)?);
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn user_update_finished(&mut self, r: Result<update_user::ResponseData>) -> Result<bool> {
|
|
|
|
match r {
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(_) => {
|
|
|
|
ConsoleService::log("Successfully updated user");
|
|
|
|
self.update_successful = true;
|
|
|
|
let User {
|
|
|
|
id,
|
|
|
|
display_name,
|
|
|
|
first_name,
|
|
|
|
last_name,
|
|
|
|
email,
|
|
|
|
creation_date,
|
|
|
|
groups,
|
|
|
|
} = self.user.take().unwrap();
|
|
|
|
let new_user = self.update_request.take().unwrap();
|
|
|
|
self.user = Some(User {
|
|
|
|
id,
|
|
|
|
email: new_user.email.unwrap_or(email),
|
|
|
|
display_name: new_user.displayName.unwrap_or(display_name),
|
|
|
|
first_name: new_user.firstName.unwrap_or(first_name),
|
|
|
|
last_name: new_user.lastName.unwrap_or(last_name),
|
|
|
|
creation_date,
|
|
|
|
groups,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn submit_remove_group(&mut self, group: Group) -> Result<bool> {
|
|
|
|
self._task = HostService::graphql_query::<RemoveUserFromGroup>(
|
|
|
|
remove_user_from_group::Variables {
|
|
|
|
user: self.username.clone(),
|
|
|
|
group: group.id,
|
|
|
|
},
|
|
|
|
self.link.callback(Msg::RemoveGroupResponse),
|
|
|
|
"Error trying to initiate removing the user from a group",
|
|
|
|
)
|
|
|
|
.map_err(|e| {
|
|
|
|
ConsoleService::log(&e.to_string());
|
|
|
|
e
|
|
|
|
})
|
|
|
|
.ok();
|
|
|
|
self.group_to_remove = Some(group);
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2021-09-02 16:11:28 +00:00
|
|
|
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
|
|
|
|
self.update_successful = false;
|
|
|
|
match msg {
|
2021-09-16 21:22:11 +00:00
|
|
|
Msg::UserDetailsResponse(response) => match response {
|
|
|
|
Ok(user) => self.user = Some(user.user),
|
|
|
|
Err(e) => {
|
|
|
|
self.user = None;
|
|
|
|
bail!("Error getting user details: {}", e);
|
2021-09-02 16:11:28 +00:00
|
|
|
}
|
2021-09-16 21:22:11 +00:00
|
|
|
},
|
|
|
|
Msg::SubmitUserUpdateForm => return self.submit_user_update_form(),
|
|
|
|
Msg::UpdateFinished(r) => return self.user_update_finished(r),
|
|
|
|
Msg::SubmitRemoveGroup(group) => return self.submit_remove_group(group),
|
|
|
|
Msg::RemoveGroupResponse(response) => {
|
|
|
|
response?;
|
|
|
|
let group = self.group_to_remove.take().unwrap();
|
|
|
|
// Remove the group from the user and add it to the dropdown.
|
|
|
|
self.user.as_mut().unwrap().groups.retain(|g| g != &group);
|
2021-09-17 17:43:22 +00:00
|
|
|
}
|
|
|
|
Msg::OnError(e) => return Err(e),
|
|
|
|
Msg::OnUserAddedToGroup(group) => {
|
|
|
|
self.user.as_mut().unwrap().groups.push(group);
|
2021-09-16 21:22:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view_form(&self, u: &User) -> Html {
|
|
|
|
html! {
|
|
|
|
<form ref=self.node_ref.clone() onsubmit=self.link.callback(|e: FocusEvent| { e.prevent_default(); Msg::SubmitUserUpdateForm })>
|
|
|
|
<div>
|
|
|
|
<span>{"User ID: "}</span>
|
|
|
|
<span>{&u.id}</span>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label for="email">{"Email: "}</label>
|
|
|
|
<input type="text" id="email" value={u.email.clone()} />
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label for="display_name">{"Display name: "}</label>
|
|
|
|
<input type="text" id="display_name" value={u.display_name.clone()} />
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label for="first_name">{"First name: "}</label>
|
|
|
|
<input type="text" id="first_name" value={u.first_name.clone()} />
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<label for="last_name">{"Last name: "}</label>
|
|
|
|
<input type="text" id="last_name" value={u.last_name.clone()} />
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<span>{"Creation date: "}</span>
|
|
|
|
<span>{&u.creation_date.with_timezone(&chrono::Local)}</span>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<button type="submit">{"Update"}</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn view_messages(&self, error: &Option<Error>) -> Html {
|
|
|
|
if self.update_successful {
|
|
|
|
html! {
|
|
|
|
<span>{"Update successful!"}</span>
|
|
|
|
}
|
|
|
|
} else if let Some(e) = error {
|
|
|
|
html! {
|
|
|
|
<div>
|
|
|
|
<span>{"Error: "}{e.to_string()}</span>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
html! {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view_group_memberships(&self, u: &User) -> Html {
|
|
|
|
let make_group_row = |group: &Group| {
|
|
|
|
let id = group.id;
|
|
|
|
let display_name = group.display_name.clone();
|
|
|
|
html! {
|
2021-09-17 19:27:00 +00:00
|
|
|
<tr key="groupRow_".to_string() + &display_name>
|
2021-09-16 21:22:11 +00:00
|
|
|
<td>{&group.display_name}</td>
|
|
|
|
{ if self.is_admin { html! {
|
|
|
|
<td><button onclick=self.link.callback(move |_| Msg::SubmitRemoveGroup(Group{id, display_name: display_name.clone()}))>{"-"}</button></td>
|
|
|
|
}} else { html!{} }
|
2021-09-02 16:11:28 +00:00
|
|
|
}
|
2021-09-16 21:22:11 +00:00
|
|
|
</tr>
|
2021-09-02 16:11:28 +00:00
|
|
|
}
|
2021-09-16 21:22:11 +00:00
|
|
|
};
|
|
|
|
html! {
|
|
|
|
<div>
|
|
|
|
<span>{"Group memberships"}</span>
|
|
|
|
<table>
|
2021-09-17 17:43:22 +00:00
|
|
|
<tr key="headerRow">
|
2021-09-16 21:22:11 +00:00
|
|
|
<th>{"Group"}</th>
|
|
|
|
{ if self.is_admin { html!{ <th></th> }} else { html!{} }}
|
|
|
|
</tr>
|
|
|
|
{u.groups.iter().map(make_group_row).collect::<Vec<_>>()}
|
2021-09-17 17:43:22 +00:00
|
|
|
<tr key="groupToAddRow">
|
|
|
|
{self.view_add_group_button(u)}
|
|
|
|
</tr>
|
2021-09-16 21:22:11 +00:00
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 17:43:22 +00:00
|
|
|
fn view_add_group_button(&self, u: &User) -> Html {
|
|
|
|
if self.is_admin {
|
2021-09-16 21:22:11 +00:00
|
|
|
html! {
|
2021-09-17 17:43:22 +00:00
|
|
|
<AddUserToGroupComponent
|
|
|
|
user=u.clone()
|
|
|
|
on_error=self.link.callback(Msg::OnError)
|
|
|
|
on_user_added_to_group=self.link.callback(Msg::OnUserAddedToGroup)/>
|
2021-09-16 21:22:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-17 17:43:22 +00:00
|
|
|
html! {}
|
2021-09-02 16:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_element(name: &str) -> Option<String> {
|
|
|
|
use wasm_bindgen::JsCast;
|
2021-09-17 17:43:22 +00:00
|
|
|
Some(
|
|
|
|
web_sys::window()?
|
|
|
|
.document()?
|
|
|
|
.get_element_by_id(name)?
|
|
|
|
.dyn_into::<web_sys::HtmlInputElement>()
|
|
|
|
.ok()?
|
|
|
|
.value(),
|
|
|
|
)
|
2021-09-02 16:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_element_or_empty(name: &str) -> String {
|
|
|
|
get_element(name).unwrap_or_default()
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for UserDetails {
|
|
|
|
type Message = Msg;
|
|
|
|
type Properties = Props;
|
|
|
|
|
|
|
|
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
2021-09-16 21:22:11 +00:00
|
|
|
let mut table = Self {
|
2021-07-05 07:21:13 +00:00
|
|
|
link,
|
|
|
|
username: props.username,
|
2021-09-02 16:11:28 +00:00
|
|
|
node_ref: NodeRef::default(),
|
2021-07-05 07:21:13 +00:00
|
|
|
_task: None,
|
|
|
|
user: None,
|
2021-09-02 16:11:28 +00:00
|
|
|
error: None,
|
|
|
|
update_request: None,
|
|
|
|
update_successful: false,
|
2021-09-16 21:22:11 +00:00
|
|
|
is_admin: props.is_admin,
|
|
|
|
group_to_remove: None,
|
2021-07-05 07:21:13 +00:00
|
|
|
};
|
|
|
|
table.get_user_details();
|
|
|
|
table
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
2021-09-02 16:11:28 +00:00
|
|
|
self.error = None;
|
|
|
|
match self.handle_msg(msg) {
|
|
|
|
Err(e) => {
|
|
|
|
ConsoleService::error(&e.to_string());
|
|
|
|
self.error = Some(e);
|
2021-07-05 07:21:13 +00:00
|
|
|
true
|
|
|
|
}
|
2021-09-02 16:11:28 +00:00
|
|
|
Ok(b) => b,
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
2021-09-02 16:11:28 +00:00
|
|
|
match (&self.user, &self.error) {
|
|
|
|
(None, None) => html! {{"Loading..."}},
|
|
|
|
(None, Some(e)) => html! {<div>{"Error: "}{e.to_string()}</div>},
|
|
|
|
(Some(u), error) => {
|
2021-07-05 07:21:13 +00:00
|
|
|
html! {
|
2021-09-16 21:22:11 +00:00
|
|
|
<div>
|
|
|
|
{self.view_form(u)}
|
|
|
|
{self.view_messages(error)}
|
|
|
|
{self.view_group_memberships(u)}
|
2021-09-12 09:54:47 +00:00
|
|
|
<div>
|
|
|
|
<NavButton route=AppRoute::ChangePassword(self.username.clone())>{"Change password"}</NavButton>
|
|
|
|
</div>
|
2021-09-16 21:22:11 +00:00
|
|
|
</div>
|
2021-07-05 07:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|