BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / call / options_dialog.rs

options_dialog.rs

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