rewite update to ws

This commit is contained in:
Adrian Wozniak 2020-04-07 14:46:56 +02:00
parent 033b5d9640
commit 272ff38d9a
6 changed files with 37 additions and 76 deletions

View File

@ -14,24 +14,6 @@ pub fn send_ws_msg(msg: WsMsg) {
send_bin_code(data); send_bin_code(data);
} }
pub async fn update_issue(
host_url: String,
id: i32,
payload: UpdateIssuePayload,
) -> Result<Msg, Msg> {
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<Msg, Msg> { pub async fn delete_issue(host_url: String, id: i32) -> Result<Msg, Msg> {
match host_client(host_url, format!("/issues/{id}", id = id).as_str()) { match host_client(host_url, format!("/issues/{id}", id = id).as_str()) {
Ok(client) => { Ok(client) => {

View File

@ -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<String>, 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<Issue> = 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);
}
}
}
}

View File

@ -11,7 +11,6 @@ use crate::shared::read_auth_token;
use crate::shared::styled_select::StyledSelectChange; use crate::shared::styled_select::StyledSelectChange;
mod api; mod api;
mod api_handlers;
mod login; mod login;
mod modal; mod modal;
mod model; mod model;

View File

@ -2,7 +2,7 @@ use seed::{prelude::*, *};
use jirs_data::{Issue, IssueType, UpdateIssuePayload}; 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::model::{AddIssueModal, EditIssueModal, ModalType, Page};
use crate::shared::styled_modal::{StyledModal, Variant as ModalVariant}; use crate::shared::styled_modal::{StyledModal, Variant as ModalVariant};
use crate::shared::styled_select::StyledSelectChange; use crate::shared::styled_select::StyledSelectChange;
@ -98,11 +98,7 @@ pub fn update(msg: &Msg, model: &mut model::Model, orders: &mut impl Orders<Msg>
project_id: Some(issue.project_id.clone()), project_id: Some(issue.project_id.clone()),
user_ids: Some(issue.user_ids.clone()), user_ids: Some(issue.user_ids.clone()),
}; };
orders.skip().perform_cmd(update_issue( send_ws_msg(jirs_data::WsMsg::IssueUpdateRequest(form));
model.host_url.clone(),
*issue_id,
form,
));
} }
_ => {} _ => {}
} }

View File

@ -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::ProjectIssuesRequest);
send_ws_msg(jirs_data::WsMsg::ProjectUsersRequest); send_ws_msg(jirs_data::WsMsg::ProjectUsersRequest);
} }
Msg::WsMsg(WsMsg::IssueUpdated(issue)) => {
let mut old: Vec<Issue> = 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<Issue> = vec![];
std::mem::swap(&mut old, &mut model.issues);
for is in old {
if is.id != id {
model.issues.push(is);
}
}
}
Msg::ToggleAboutTooltip => { Msg::ToggleAboutTooltip => {
model.project_page.about_tooltip_visible = !model.project_page.about_tooltip_visible; 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()), user_ids: Some(issue.user_ids.clone()),
}; };
model.project_page.dragged_issue_id = None; model.project_page.dragged_issue_id = None;
orders.skip().perform_cmd(crate::api::update_issue( send_ws_msg(WsMsg::IssueUpdateRequest(payload));
model.host_url.clone(),
issue.id,
payload,
));
} }
_ => error!("Drag stopped before drop :("), _ => error!("Drag stopped before drop :("),
}, },
Msg::IssueUpdateResult(fetched) => {
crate::api_handlers::update_issue_response(&fetched, model);
}
Msg::DeleteIssue(issue_id) => { Msg::DeleteIssue(issue_id) => {
orders orders
.skip() .skip()
@ -253,10 +266,6 @@ fn project_board_lists(model: &Model) -> Node<Msg> {
} }
fn project_issue_list(model: &Model, status: jirs_data::IssueStatus) -> Node<Msg> { fn project_issue_list(model: &Model, status: jirs_data::IssueStatus) -> Node<Msg> {
let project = match model.project.as_ref() {
Some(p) => p,
_ => return empty![],
};
let issues: Vec<Node<Msg>> = model let issues: Vec<Node<Msg>> = model
.issues .issues
.iter() .iter()

View File

@ -361,7 +361,7 @@ pub struct UpdateCommentPayload {
pub body: String, pub body: String,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CreateIssuePayload { pub struct CreateIssuePayload {
pub title: String, pub title: String,
@ -391,12 +391,25 @@ pub struct UpdateProjectPayload {
pub enum WsMsg { pub enum WsMsg {
Ping, Ping,
Pong, Pong,
// auth
AuthorizeRequest(Uuid), AuthorizeRequest(Uuid),
AuthorizeLoaded(Result<User, String>), AuthorizeLoaded(Result<User, String>),
AuthorizeExpired,
// project page
ProjectRequest, ProjectRequest,
ProjectLoaded(Project), ProjectLoaded(Project),
ProjectIssuesRequest, ProjectIssuesRequest,
ProjectIssuesLoaded(Vec<Issue>), ProjectIssuesLoaded(Vec<Issue>),
ProjectUsersRequest, ProjectUsersRequest,
ProjectUsersLoaded(Vec<User>), ProjectUsersLoaded(Vec<User>),
// issue
IssueUpdateRequest(UpdateIssuePayload),
IssueUpdated(Issue),
IssueDeleteRequest(i32),
IssueDeleted(i32),
IssueCreateRequest(CreateIssuePayload),
IssueCreated(Issue),
} }