| 1 | use calling::{MeetingOptions, OptionWrite}; |
| 2 | |
| 3 | #[derive(Debug, Clone, PartialEq)] |
| 4 | pub struct OptionsDialog { |
| 5 | pub original: Option<MeetingOptions>, |
| 6 | pub draft: Option<MeetingOptions>, |
| 7 | pub saving: bool, |
| 8 | pub error: Option<String>, |
| 9 | } |
| 10 | |
| 11 | impl OptionsDialog { |
| 12 | pub fn loading() -> Self { |
| 13 | OptionsDialog { |
| 14 | original: None, |
| 15 | draft: None, |
| 16 | saving: false, |
| 17 | error: None, |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | pub fn loaded(&mut self, result: Result<MeetingOptions, String>) { |
| 22 | match result { |
| 23 | Ok(options) => { |
| 24 | self.original = Some(options.clone()); |
| 25 | self.draft = Some(options); |
| 26 | self.error = None; |
| 27 | } |
| 28 | Err(error) => self.error = Some(error), |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | pub fn is_loading(&self) -> bool { |
| 33 | self.draft.is_none() && self.error.is_none() |
| 34 | } |
| 35 | |
| 36 | pub fn changes(&self) -> Vec<OptionWrite> { |
| 37 | match (&self.original, &self.draft) { |
| 38 | (Some(original), Some(draft)) => original.changes(draft), |
| 39 | _ => Vec::new(), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn can_save(&self) -> bool { |
| 44 | !self.saving && !self.changes().is_empty() |
| 45 | } |
| 46 | |
| 47 | pub fn begin_save(&mut self) -> Option<Vec<OptionWrite>> { |
| 48 | if !self.can_save() { |
| 49 | return None; |
| 50 | } |
| 51 | self.saving = true; |
| 52 | self.error = None; |
| 53 | Some(self.changes()) |
| 54 | } |
| 55 | |
| 56 | pub fn saved(&mut self, result: Result<MeetingOptions, String>) -> bool { |
| 57 | self.saving = false; |
| 58 | match result { |
| 59 | Ok(_) => true, |
| 60 | Err(error) => { |
| 61 | self.error = Some(error); |
| 62 | false |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #[cfg(test)] |
| 69 | mod tests { |
| 70 | use serde_json::json; |
| 71 | |
| 72 | use super::*; |
| 73 | |
| 74 | fn options() -> MeetingOptions { |
| 75 | let option = |
| 76 | |name: &str, value: serde_json::Value| json!({"name": name, "currentValue": value}); |
| 77 | MeetingOptions::from_response(&json!({"options": [ |
| 78 | option("AutoAdmittedUsers", json!("EveryoneInCompany")), |
| 79 | option("PresenterOption", json!("Everyone")), |
| 80 | option("AttendeeRestrictions", json!("UnRestricted")), |
| 81 | option("AllowMeetingChat", json!("Enabled")), |
| 82 | option("AllowTeamsMeetingReactions", json!(true)), |
| 83 | option("AutoRecordAndTranscribeMode", json!("Off")), |
| 84 | ]})) |
| 85 | .unwrap() |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn a_dialog_loads_then_saves_only_what_changed() { |
| 90 | let mut dialog = OptionsDialog::loading(); |
| 91 | assert!(dialog.is_loading()); |
| 92 | assert!(dialog.begin_save().is_none()); |
| 93 | dialog.loaded(Ok(options())); |
| 94 | assert!(!dialog.is_loading()); |
| 95 | assert!(!dialog.can_save()); |
| 96 | dialog.draft.as_mut().unwrap().reactions = false; |
| 97 | let writes = dialog.begin_save().expect("a change to save"); |
| 98 | assert_eq!(writes.len(), 1); |
| 99 | assert_eq!(writes[0].name, "AllowTeamsMeetingReactions"); |
| 100 | assert!(dialog.saving); |
| 101 | assert!(dialog.begin_save().is_none()); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn errors_stay_inline_and_a_failed_save_can_be_retried() { |
| 106 | let mut dialog = OptionsDialog::loading(); |
| 107 | dialog.loaded(Err("The options could not be read".into())); |
| 108 | assert!(!dialog.is_loading()); |
| 109 | assert_eq!( |
| 110 | dialog.error.as_deref(), |
| 111 | Some("The options could not be read") |
| 112 | ); |
| 113 | dialog.loaded(Ok(options())); |
| 114 | assert_eq!(dialog.error, None); |
| 115 | dialog.draft.as_mut().unwrap().meeting_chat = "Disabled".into(); |
| 116 | dialog.begin_save(); |
| 117 | assert!(!dialog.saved(Err("the service refused AllowMeetingChat".into()))); |
| 118 | assert!(!dialog.saving); |
| 119 | assert_eq!( |
| 120 | dialog.error.as_deref(), |
| 121 | Some("the service refused AllowMeetingChat") |
| 122 | ); |
| 123 | assert!(dialog.can_save()); |
| 124 | dialog.begin_save(); |
| 125 | assert!(dialog.saved(Ok(options()))); |
| 126 | } |
| 127 | } |