-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
149 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
mod update_events; | ||
use update_events::*; | ||
|
||
mod update_library; | ||
use update_library::*; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use chrono::DateTime; | ||
use futures::{future, FutureExt, TryFutureExt}; | ||
|
||
use crate::models::common::{eq_update, Loadable}; | ||
use crate::models::ctx::CtxError; | ||
use crate::runtime::msg::{Action, ActionCtx, Internal, Msg}; | ||
use crate::runtime::{Effect, EffectFuture, Effects, Env, EnvFutureExt}; | ||
use crate::types::api::{ | ||
fetch_api, APIRequest, APIResult, GetModalResponse, GetNotificationResponse, | ||
}; | ||
use crate::types::events::Events; | ||
|
||
pub fn update_events<E: Env + 'static>(events: &mut Events, msg: &Msg) -> Effects { | ||
match msg { | ||
Msg::Action(Action::Ctx(ActionCtx::GetEvents)) => { | ||
let modal_effects = eq_update(&mut events.modal, Loadable::Loading); | ||
let notification_effects = eq_update(&mut events.notification, Loadable::Loading); | ||
let requests_effects = Effects::many(vec![get_modal::<E>(), get_notification::<E>()]); | ||
|
||
modal_effects | ||
.join(notification_effects) | ||
.join(requests_effects) | ||
} | ||
Msg::Internal(Internal::GetModalResult(_, result)) => match result { | ||
Ok(response) => eq_update(&mut events.modal, Loadable::Ready(response.to_owned())), | ||
Err(error) => eq_update(&mut events.modal, Loadable::Err(error.to_owned())), | ||
}, | ||
Msg::Internal(Internal::GetNotificationResult(_, result)) => match result { | ||
Ok(response) => eq_update( | ||
&mut events.notification, | ||
Loadable::Ready(response.to_owned()), | ||
), | ||
Err(error) => eq_update(&mut events.notification, Loadable::Err(error.to_owned())), | ||
}, | ||
_ => Effects::none().unchanged(), | ||
} | ||
} | ||
|
||
fn get_modal<E: Env + 'static>() -> Effect { | ||
let request = APIRequest::GetModal { | ||
date: DateTime::from(E::now()), | ||
}; | ||
|
||
EffectFuture::Concurrent( | ||
fetch_api::<E, _, _, GetModalResponse>(&request) | ||
.map_err(CtxError::from) | ||
.and_then(|result| match result { | ||
APIResult::Ok { result } => future::ok(result), | ||
APIResult::Err { error } => future::err(CtxError::from(error)), | ||
}) | ||
.map(move |result| Msg::Internal(Internal::GetModalResult(request, result))) | ||
.boxed_env(), | ||
) | ||
.into() | ||
} | ||
|
||
fn get_notification<E: Env + 'static>() -> Effect { | ||
let request = APIRequest::GetNotification { | ||
date: DateTime::from(E::now()), | ||
}; | ||
|
||
EffectFuture::Concurrent( | ||
fetch_api::<E, _, _, GetNotificationResponse>(&request) | ||
.map_err(CtxError::from) | ||
.and_then(|result| match result { | ||
APIResult::Ok { result } => future::ok(result), | ||
APIResult::Err { error } => future::err(CtxError::from(error)), | ||
}) | ||
.map(move |result| Msg::Internal(Internal::GetNotificationResult(request, result))) | ||
.boxed_env(), | ||
) | ||
.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use serde::Serialize; | ||
|
||
use crate::{ | ||
models::{common::Loadable, ctx::CtxError}, | ||
types::api::{GetModalResponse, GetNotificationResponse}, | ||
}; | ||
|
||
#[derive(Default, PartialEq, Eq, Serialize, Clone, Debug)] | ||
pub struct Events { | ||
pub modal: Loadable<GetModalResponse, CtxError>, | ||
pub notification: Loadable<GetNotificationResponse, CtxError>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod events; | ||
pub use events::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod addon; | ||
pub mod api; | ||
pub mod events; | ||
pub mod library; | ||
pub mod notifications; | ||
pub mod profile; | ||
|