bitque/jirs-client/src/api_handlers.rs

88 lines
2.2 KiB
Rust
Raw Normal View History

2020-03-31 22:05:18 +02:00
use seed::fetch::{FetchObject, ResponseWithDataResult};
2020-03-31 23:49:46 +02:00
use seed::*;
2020-03-31 22:05:18 +02:00
use jirs_data::{FullProjectResponse, Issue};
use crate::model::Model;
pub fn current_user_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::<'_, jirs_data::User>(body.as_str()) {
Ok(user) => {
model.user = Some(user);
}
_ => (),
}
}
}
pub fn current_project_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::<'_, FullProjectResponse>(body.as_str()) {
Ok(project_response) => {
model.project = Some(project_response.project);
}
_ => (),
}
}
}
pub fn update_issue_response(fetched: &FetchObject<String>, model: &mut Model) {
2020-03-31 23:49:46 +02:00
log!("update_issue_response");
log!(fetched);
2020-03-31 22:05:18 +02:00
if let FetchObject {
result:
Ok(ResponseWithDataResult {
data: Ok(body),
status,
..
}),
..
} = fetched
{
if status.is_error() {
return;
}
match (
serde_json::from_str::<'_, Issue>(body.as_str()),
model.project.as_mut(),
) {
(Ok(issue), Some(project)) => {
let mut issues: Vec<Issue> = vec![];
for i in project.issues.iter() {
if i.id != issue.id {
issues.push(i.clone());
}
}
issues.push(issue);
project.issues = issues;
}
_ => (),
}
}
}