| 1 |
|
| 2 |
|
| 3 |
use super::{ |
| 4 |
App, AppEvent, DataPayload, KeyCode, KeyEvent, MnwApiClient, Screen, load_analytics, |
| 5 |
load_transactions, mpsc, |
| 6 |
}; |
| 7 |
|
| 8 |
pub(crate) async fn handle_analytics_input( |
| 9 |
key: KeyEvent, |
| 10 |
app: &mut App, |
| 11 |
screen: &mut Screen, |
| 12 |
api: &MnwApiClient, |
| 13 |
tx: &mpsc::Sender<AppEvent>, |
| 14 |
) { |
| 15 |
match key.code { |
| 16 |
KeyCode::Char('j') | KeyCode::Down if app.analytics_show_transactions => { |
| 17 |
app.move_down(screen); |
| 18 |
} |
| 19 |
KeyCode::Char('k') | KeyCode::Up if app.analytics_show_transactions => { |
| 20 |
app.move_up(screen); |
| 21 |
} |
| 22 |
KeyCode::Esc => { |
| 23 |
if app.analytics_show_transactions { |
| 24 |
app.analytics_show_transactions = false; |
| 25 |
app.selected_index = 0; |
| 26 |
} else { |
| 27 |
*screen = Screen::Home; |
| 28 |
app.analytics_data = None; |
| 29 |
app.analytics_status = None; |
| 30 |
app.selected_index = 0; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
KeyCode::Char('1') => { |
| 35 |
app.analytics_range = "7d".to_string(); |
| 36 |
reload_analytics(app, api, tx); |
| 37 |
} |
| 38 |
KeyCode::Char('2') => { |
| 39 |
app.analytics_range = "30d".to_string(); |
| 40 |
reload_analytics(app, api, tx); |
| 41 |
} |
| 42 |
KeyCode::Char('3') => { |
| 43 |
app.analytics_range = "90d".to_string(); |
| 44 |
reload_analytics(app, api, tx); |
| 45 |
} |
| 46 |
KeyCode::Char('4') => { |
| 47 |
app.analytics_range = "all".to_string(); |
| 48 |
reload_analytics(app, api, tx); |
| 49 |
} |
| 50 |
KeyCode::Char('t' | 'T') => { |
| 51 |
if app.analytics_show_transactions { |
| 52 |
app.analytics_show_transactions = false; |
| 53 |
} else { |
| 54 |
app.analytics_show_transactions = true; |
| 55 |
app.selected_index = 0; |
| 56 |
if app.transactions.is_empty() { |
| 57 |
app.loading = true; |
| 58 |
let api = api.clone(); |
| 59 |
let user_id = app.user.user_id.clone(); |
| 60 |
let tx = tx.clone(); |
| 61 |
tokio::spawn(async move { |
| 62 |
load_transactions(&api, &user_id, &tx).await; |
| 63 |
}); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
KeyCode::Char('e' | 'E') => { |
| 68 |
app.analytics_status = Some("Exporting...".to_string()); |
| 69 |
let api = api.clone(); |
| 70 |
let user_id = app.user.user_id.clone(); |
| 71 |
let tx = tx.clone(); |
| 72 |
tokio::spawn(async move { |
| 73 |
match api.export_sales_csv(&user_id).await { |
| 74 |
Ok(result) => { |
| 75 |
let _ = tx |
| 76 |
.send(AppEvent::DataLoaded(DataPayload::ExportCsv { |
| 77 |
csv: result.csv, |
| 78 |
row_count: result.row_count, |
| 79 |
})) |
| 80 |
.await; |
| 81 |
} |
| 82 |
Err(e) => { |
| 83 |
let _ = tx |
| 84 |
.send(AppEvent::DataLoaded(DataPayload::GenericError { |
| 85 |
error: e.to_string(), |
| 86 |
})) |
| 87 |
.await; |
| 88 |
} |
| 89 |
} |
| 90 |
}); |
| 91 |
} |
| 92 |
KeyCode::Char('r' | 'R') => { |
| 93 |
reload_analytics(app, api, tx); |
| 94 |
} |
| 95 |
_ => {} |
| 96 |
} |
| 97 |
} |
| 98 |
fn reload_analytics(app: &mut App, api: &MnwApiClient, tx: &mpsc::Sender<AppEvent>) { |
| 99 |
app.loading = true; |
| 100 |
app.analytics_status = None; |
| 101 |
let api = api.clone(); |
| 102 |
let user_id = app.user.user_id.clone(); |
| 103 |
let range = app.analytics_range.clone(); |
| 104 |
let tx = tx.clone(); |
| 105 |
tokio::spawn(async move { |
| 106 |
load_analytics(&api, &user_id, &range, &tx).await; |
| 107 |
}); |
| 108 |
} |
| 109 |
|