From 272ff38d9ae97baca01356cfc36eb2377467fd92 Mon Sep 17 00:00:00 2001 From: Adrian Wozniak Date: Tue, 7 Apr 2020 14:46:56 +0200 Subject: [PATCH] rewite update to ws --- jirs-client/src/api.rs | 18 ---------------- jirs-client/src/api_handlers.rs | 38 --------------------------------- jirs-client/src/lib.rs | 1 - jirs-client/src/modal/mod.rs | 8 ++----- jirs-client/src/project.rs | 33 +++++++++++++++++----------- jirs-data/src/lib.rs | 15 ++++++++++++- 6 files changed, 37 insertions(+), 76 deletions(-) delete mode 100644 jirs-client/src/api_handlers.rs diff --git a/jirs-client/src/api.rs b/jirs-client/src/api.rs index 98eba7e9..d7b52226 100644 --- a/jirs-client/src/api.rs +++ b/jirs-client/src/api.rs @@ -14,24 +14,6 @@ pub fn send_ws_msg(msg: WsMsg) { send_bin_code(data); } -pub async fn update_issue( - host_url: String, - id: i32, - payload: UpdateIssuePayload, -) -> Result { - match host_client(host_url, format!("/issues/{id}", id = id).as_str()) { - Ok(client) => { - client - .method(Method::Put) - .header("Content-Type", "application/json") - .body_json(&payload) - .fetch_string(Msg::IssueUpdateResult) - .await - } - Err(e) => return Ok(Msg::InternalFailure(e)), - } -} - pub async fn delete_issue(host_url: String, id: i32) -> Result { match host_client(host_url, format!("/issues/{id}", id = id).as_str()) { Ok(client) => { diff --git a/jirs-client/src/api_handlers.rs b/jirs-client/src/api_handlers.rs deleted file mode 100644 index a21cea18..00000000 --- a/jirs-client/src/api_handlers.rs +++ /dev/null @@ -1,38 +0,0 @@ -use seed::fetch::{FetchObject, ResponseWithDataResult}; -use seed::*; - -use jirs_data::{FullIssue, Issue}; - -use crate::model::Model; - -pub fn update_issue_response(fetched: &FetchObject, model: &mut Model) { - if let FetchObject { - result: - Ok(ResponseWithDataResult { - data: Ok(body), - status, - .. - }), - .. - } = fetched - { - if status.is_error() { - return; - } - match serde_json::from_str::<'_, FullIssue>(body.as_str()) { - Ok(issue) => { - let mut issues: Vec = vec![]; - std::mem::swap(&mut model.issues, &mut issues); - for i in issues.into_iter() { - if i.id != issue.id { - model.issues.push(i); - } - } - model.issues.push(issue.into()); - } - Err(error) => { - error!(error); - } - } - } -} diff --git a/jirs-client/src/lib.rs b/jirs-client/src/lib.rs index ff88be1f..ce932627 100644 --- a/jirs-client/src/lib.rs +++ b/jirs-client/src/lib.rs @@ -11,7 +11,6 @@ use crate::shared::read_auth_token; use crate::shared::styled_select::StyledSelectChange; mod api; -mod api_handlers; mod login; mod modal; mod model; diff --git a/jirs-client/src/modal/mod.rs b/jirs-client/src/modal/mod.rs index 24271c9f..f1d010b3 100644 --- a/jirs-client/src/modal/mod.rs +++ b/jirs-client/src/modal/mod.rs @@ -2,7 +2,7 @@ use seed::{prelude::*, *}; use jirs_data::{Issue, IssueType, UpdateIssuePayload}; -use crate::api::update_issue; +use crate::api::send_ws_msg; use crate::model::{AddIssueModal, EditIssueModal, ModalType, Page}; use crate::shared::styled_modal::{StyledModal, Variant as ModalVariant}; use crate::shared::styled_select::StyledSelectChange; @@ -98,11 +98,7 @@ pub fn update(msg: &Msg, model: &mut model::Model, orders: &mut impl Orders project_id: Some(issue.project_id.clone()), user_ids: Some(issue.user_ids.clone()), }; - orders.skip().perform_cmd(update_issue( - model.host_url.clone(), - *issue_id, - form, - )); + send_ws_msg(jirs_data::WsMsg::IssueUpdateRequest(form)); } _ => {} } diff --git a/jirs-client/src/project.rs b/jirs-client/src/project.rs index cf4b3f04..601f4f40 100644 --- a/jirs-client/src/project.rs +++ b/jirs-client/src/project.rs @@ -21,6 +21,26 @@ pub fn update(msg: Msg, model: &mut crate::model::Model, orders: &mut impl Order send_ws_msg(jirs_data::WsMsg::ProjectIssuesRequest); send_ws_msg(jirs_data::WsMsg::ProjectUsersRequest); } + Msg::WsMsg(WsMsg::IssueUpdated(issue)) => { + let mut old: Vec = vec![]; + std::mem::swap(&mut old, &mut model.issues); + for is in old { + if is.id == issue.id { + model.issues.push(issue.clone()) + } else { + model.issues.push(is); + } + } + } + Msg::WsMsg(WsMsg::IssueDeleted(id)) => { + let mut old: Vec = vec![]; + std::mem::swap(&mut old, &mut model.issues); + for is in old { + if is.id != id { + model.issues.push(is); + } + } + } Msg::ToggleAboutTooltip => { model.project_page.about_tooltip_visible = !model.project_page.about_tooltip_visible; } @@ -96,17 +116,10 @@ pub fn update(msg: Msg, model: &mut crate::model::Model, orders: &mut impl Order user_ids: Some(issue.user_ids.clone()), }; model.project_page.dragged_issue_id = None; - orders.skip().perform_cmd(crate::api::update_issue( - model.host_url.clone(), - issue.id, - payload, - )); + send_ws_msg(WsMsg::IssueUpdateRequest(payload)); } _ => error!("Drag stopped before drop :("), }, - Msg::IssueUpdateResult(fetched) => { - crate::api_handlers::update_issue_response(&fetched, model); - } Msg::DeleteIssue(issue_id) => { orders .skip() @@ -253,10 +266,6 @@ fn project_board_lists(model: &Model) -> Node { } fn project_issue_list(model: &Model, status: jirs_data::IssueStatus) -> Node { - let project = match model.project.as_ref() { - Some(p) => p, - _ => return empty![], - }; let issues: Vec> = model .issues .iter() diff --git a/jirs-data/src/lib.rs b/jirs-data/src/lib.rs index c85aba5b..aa84259a 100644 --- a/jirs-data/src/lib.rs +++ b/jirs-data/src/lib.rs @@ -361,7 +361,7 @@ pub struct UpdateCommentPayload { pub body: String, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] pub struct CreateIssuePayload { pub title: String, @@ -391,12 +391,25 @@ pub struct UpdateProjectPayload { pub enum WsMsg { Ping, Pong, + + // auth AuthorizeRequest(Uuid), AuthorizeLoaded(Result), + AuthorizeExpired, + + // project page ProjectRequest, ProjectLoaded(Project), ProjectIssuesRequest, ProjectIssuesLoaded(Vec), ProjectUsersRequest, ProjectUsersLoaded(Vec), + + // issue + IssueUpdateRequest(UpdateIssuePayload), + IssueUpdated(Issue), + IssueDeleteRequest(i32), + IssueDeleted(i32), + IssueCreateRequest(CreateIssuePayload), + IssueCreated(Issue), }