Merge branch 'master' of git.sr.ht:~tsumanu/jirs

This commit is contained in:
Adrian Wozniak 2020-05-06 22:25:07 +02:00
commit 04e4b657df
6 changed files with 96 additions and 1 deletions

View File

@ -5,12 +5,13 @@ use jirs_data::*;
use crate::api::send_ws_msg;
use crate::model::{Model, Page, PageContent, ProfilePage};
use crate::shared::styled_button::StyledButton;
use crate::shared::styled_field::StyledField;
use crate::shared::styled_form::StyledForm;
use crate::shared::styled_image_input::StyledImageInput;
use crate::shared::styled_input::StyledInput;
use crate::shared::{inner_layout, ToNode};
use crate::{FieldId, Msg, HOST_URL};
use crate::{FieldId, Msg, PageChanged, ProfilePageChange, HOST_URL};
pub fn update(msg: Msg, model: &mut crate::model::Model, orders: &mut impl Orders<Msg>) {
let user = match model.user {
@ -59,6 +60,12 @@ pub fn update(msg: Msg, model: &mut crate::model::Model, orders: &mut impl Order
}
}
}
Msg::PageChanged(PageChanged::Profile(ProfilePageChange::SubmitForm)) => {
send_ws_msg(WsMsg::ProfileUpdate(
profile_page.email.value.clone(),
profile_page.name.value.clone(),
))
}
_ => (),
}
}
@ -99,11 +106,27 @@ pub fn view(model: &Model) -> Node<Msg> {
.build()
.into_node();
let submit = StyledButton::build()
.primary()
.text("Save")
.on_click(mouse_ev(Ev::Click, |ev| {
ev.prevent_default();
Msg::PageChanged(PageChanged::Profile(ProfilePageChange::SubmitForm))
}))
.build()
.into_node();
let submit_field = StyledField::build().input(submit).build().into_node();
let content = StyledForm::build()
.heading("Profile")
.on_submit(ev(Ev::Submit, |ev| {
ev.prevent_default();
Msg::PageChanged(PageChanged::Profile(ProfilePageChange::SubmitForm))
}))
.add_field(avatar)
.add_field(username_field)
.add_field(email_field)
.add_field(submit_field)
.build()
.into_node();
inner_layout(model, "profile", vec![content], empty![])

View File

@ -708,4 +708,6 @@ pub enum WsMsg {
// users
AvatarUrlChanged(UserId, String),
ProfileUpdate(EmailString, UsernameString),
ProfileUpdated,
}

View File

@ -8,6 +8,11 @@ repository = "https://gitlab.com/adrian.wozniak/jirs"
license = "MPL-2.0"
#license-file = "../LICENSE"
[target.x86_64-unknown-linux-gnu]
rustflags = [
"-C", "link-arg=-fuse-ld=lld",
]
[[bin]]
name = "jirs_server"
path = "./src/main.rs"

View File

@ -240,6 +240,43 @@ impl Handler<UpdateAvatarUrl> for DbExecutor {
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ProfileUpdate {
pub user_id: UserId,
pub name: String,
pub email: String,
}
impl Message for ProfileUpdate {
type Result = Result<User, ServiceErrors>;
}
impl Handler<ProfileUpdate> for DbExecutor {
type Result = Result<User, ServiceErrors>;
fn handle(&mut self, msg: ProfileUpdate, _ctx: &mut Self::Context) -> Self::Result {
use crate::schema::users::dsl::{email, id, name, users};
let conn = &self
.pool
.get()
.map_err(|_| ServiceErrors::DatabaseConnectionLost)?;
let update_query = diesel::update(users)
.set((email.eq(msg.email), name.eq(msg.name)))
.filter(id.eq(msg.user_id));
debug!("{}", diesel::debug_query::<Pg, _>(&update_query));
update_query
.execute(conn)
.map_err(|e| ServiceErrors::DatabaseQueryFailed(format!("{}", e)))?;
let user_query = users.find(msg.user_id);
debug!("{}", diesel::debug_query::<Pg, _>(&user_query));
user_query
.first(conn)
.map_err(|e| ServiceErrors::DatabaseQueryFailed(format!("{}", e)))
}
}
#[cfg(test)]
mod tests {
use crate::db::build_pool;

View File

@ -145,6 +145,11 @@ impl WebSocketActor {
WsMsg::InvitationRevokeRequest(id) => self.handle_msg(RevokeInvitation { id }, ctx)?,
WsMsg::InvitedUsersRequest => self.handle_msg(LoadInvitedUsers, ctx)?,
// users
WsMsg::ProfileUpdate(email, name) => {
self.handle_msg(ProfileUpdate { email, name }, ctx)?
}
// else fail
_ => {
error!("No handle for {:?} specified", msg);

View File

@ -61,3 +61,26 @@ impl WsHandler<LoadInvitedUsers> for WebSocketActor {
Ok(Some(WsMsg::InvitedUsersLoaded(users)))
}
}
pub struct ProfileUpdate {
pub name: String,
pub email: String,
}
impl WsHandler<ProfileUpdate> for WebSocketActor {
fn handle_msg(&mut self, msg: ProfileUpdate, _ctx: &mut Self::Context) -> WsResult {
let user_id = self.require_user()?.id;
let ProfileUpdate { name, email } = msg;
match block_on(self.db.send(crate::db::users::ProfileUpdate {
user_id,
name,
email,
})) {
Ok(Ok(_users)) => (),
_ => return Ok(None),
};
Ok(Some(WsMsg::ProfileUpdated))
}
}