Skip to content

Commit 1b5318a

Browse files
committed
refactor: move export csv logic into action handler
1 parent 3cacd93 commit 1b5318a

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

src/constants.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub enum Action {
3434
ResetError,
3535
AddSecret,
3636
CheckSecret,
37-
ExportCSV(PathBuf),
37+
ExportCSV(Option<PathBuf>),
3838
}
3939

4040
pub const MAX_TASK_TITLE_LENGTH: i32 = 40;

src/controller.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,14 @@ impl Controller {
171171
Action::ExportCSV(app_config_path) => {
172172
self.handle_action(Action::GetTasks);
173173
let task_list = self.state.get_task_list();
174-
let acp = app_config_path.join(constants::CSV_NAME);
174+
let acp = match app_config_path {
175+
Some(acp_option) => acp_option.join(constants::CSV_NAME),
176+
None => {
177+
let temp_acp = get_app_config_path().expect("Could not get app config path");
178+
temp_acp.join(constants::CSV_NAME)
179+
}
180+
};
181+
175182
let write_result = write_tasks_into_csv_file(task_list, &acp);
176183

177184
match write_result {
@@ -192,18 +199,7 @@ impl Controller {
192199
Screen::Main => match key_code {
193200
KeyCode::Char('a') => Action::OpenAddScreen,
194201
KeyCode::Char('x') => Action::RemoveTask,
195-
// in order to test it
196-
KeyCode::Char('e') => {
197-
let app_config_path = get_app_config_path();
198-
match app_config_path {
199-
Ok(acp) => Action::ExportCSV(acp),
200-
Err(_) => {
201-
self.state
202-
.set_error(String::from("Could not save csv file"));
203-
Action::Empty
204-
}
205-
}
206-
}
202+
KeyCode::Char('e') => Action::ExportCSV(None),
207203
KeyCode::Up => Action::MenuUp,
208204
KeyCode::Down => Action::MenuDown,
209205
KeyCode::Esc => Action::Exit,
@@ -356,7 +352,7 @@ mod tests {
356352

357353
let mut csv_path = PathBuf::new();
358354
csv_path.push("./test/csv/");
359-
controller.handle_action(Action::ExportCSV(csv_path.clone()));
355+
controller.handle_action(Action::ExportCSV(Some(csv_path.clone())));
360356
let csv_file_exist = file_exists(&csv_path, constants::CSV_NAME);
361357
assert_eq!(csv_file_exist, true);
362358

0 commit comments

Comments
 (0)