add template builder and form handler for adding wifi ap
This commit is contained in:
parent
8cd8ee5dd6
commit
e91c40355a
|
@ -0,0 +1,96 @@
|
|||
use maud::{html, Markup, PreEscaped};
|
||||
use peach_network::network;
|
||||
use rouille::{post_input, try_or_400, Request, Response};
|
||||
|
||||
use crate::{
|
||||
templates,
|
||||
utils::{
|
||||
flash::{FlashRequest, FlashResponse},
|
||||
theme,
|
||||
},
|
||||
};
|
||||
|
||||
// ROUTE: /settings/network/wifi/add
|
||||
|
||||
fn render_ssid_input(selected_ap: Option<String>) -> Markup {
|
||||
html! {
|
||||
(PreEscaped("<!-- input for network ssid -->"))
|
||||
input id="ssid" name="ssid" class="center input" type="text" placeholder="SSID" title="Network name (SSID) for WiFi access point" value=[selected_ap] autofocus;
|
||||
}
|
||||
}
|
||||
|
||||
fn render_password_input() -> Markup {
|
||||
html! {
|
||||
(PreEscaped("<!-- input for network password -->"))
|
||||
input id="pass" name="pass" class="center input" type="password" placeholder="Password" title="Password for WiFi access point";
|
||||
}
|
||||
}
|
||||
|
||||
fn render_buttons() -> Markup {
|
||||
html! {
|
||||
(PreEscaped("<!-- BUTTONS -->"))
|
||||
div id="buttons" {
|
||||
input id="addWifi" class="button button-primary center" title="Add" type="submit" value="Add";
|
||||
a class="button button-secondary center" href="/settings/network" title="Cancel" { "Cancel" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// WiFi access point credentials form template builder.
|
||||
pub fn build_template(request: &Request, selected_ap: Option<String>) -> PreEscaped<String> {
|
||||
let (flash_name, flash_msg) = request.retrieve_flash();
|
||||
|
||||
let form_template = html! {
|
||||
(PreEscaped("<!-- WIFI ADD CREDENTIALS FORM -->"))
|
||||
div class="card center" {
|
||||
form id="wifiCreds" action="/settings/network/wifi/add" method="post" {
|
||||
(render_ssid_input(selected_ap))
|
||||
(render_password_input())
|
||||
(render_buttons())
|
||||
}
|
||||
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
|
||||
(PreEscaped("<!-- FLASH MESSAGE -->"))
|
||||
(templates::flash::build_template(name, msg))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let body = templates::nav::build_template(
|
||||
form_template,
|
||||
"Add WiFi Network",
|
||||
Some("/settings/network"),
|
||||
);
|
||||
|
||||
let theme = theme::get_theme();
|
||||
|
||||
templates::base::build_template(body, theme)
|
||||
}
|
||||
|
||||
/// Parse the SSID and password for an access point and save the new credentials.
|
||||
pub fn handle_form(request: &Request) -> Response {
|
||||
let data = try_or_400!(post_input!(request, {
|
||||
ssid: String,
|
||||
pass: String,
|
||||
}));
|
||||
|
||||
let (name, msg) = match network::add("wlan0", &data.ssid, &data.pass) {
|
||||
Ok(_) => match network::reconfigure() {
|
||||
Ok(_) => ("success".to_string(), "Added WiFi credentials".to_string()),
|
||||
Err(err) => (
|
||||
"error".to_string(),
|
||||
format!(
|
||||
"Added WiFi credentials but failed to reconfigure interface: {}",
|
||||
err
|
||||
),
|
||||
),
|
||||
},
|
||||
Err(err) => (
|
||||
"error".to_string(),
|
||||
format!("Failed to add WiFi credentials for {}: {}", &data.ssid, err),
|
||||
),
|
||||
};
|
||||
|
||||
let (flash_name, flash_msg) = (format!("flash_name={}", name), format!("flash_msg={}", msg));
|
||||
|
||||
Response::redirect_303("/settings/network/wifi/add").add_flash(flash_name, flash_msg)
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
pub mod add_ap;
|
||||
pub mod configure_dns;
|
||||
// TODO: uncomment this once data usage feature is in place
|
||||
// pub mod data_usage_limits;
|
||||
|
|
Loading…
Reference in New Issue