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);
}
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> {
match host_client(host_url, format!("/issues/{id}", id = id).as_str()) {
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;
mod api;
mod api_handlers;
mod login;
mod modal;
mod model;

View File

@ -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<Msg>
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));
}
_ => {}
}

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::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 => {
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<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
.issues
.iter()

View File

@ -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<User, String>),
AuthorizeExpired,
// project page
ProjectRequest,
ProjectLoaded(Project),
ProjectIssuesRequest,
ProjectIssuesLoaded(Vec<Issue>),
ProjectUsersRequest,
ProjectUsersLoaded(Vec<User>),
// issue
IssueUpdateRequest(UpdateIssuePayload),
IssueUpdated(Issue),
IssueDeleteRequest(i32),
IssueDeleted(i32),
IssueCreateRequest(CreateIssuePayload),
IssueCreated(Issue),
}