web: Add name support
Signed-off-by: Ben Aaron Goldberg <ben@benaaron.dev>
This commit is contained in:
parent
82fc7fc10f
commit
b7311d790c
|
@ -11,12 +11,14 @@ use pronouns_today::{InstanceSettings, Pronoun};
|
|||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct IndexTemplate<'a> {
|
||||
name: Option<String>,
|
||||
short: String,
|
||||
pronouns: Vec<(usize, &'a Pronoun)>,
|
||||
}
|
||||
|
||||
fn render_page(pronoun: &Pronoun, settings: &InstanceSettings) -> String {
|
||||
fn render_page(pronoun: &Pronoun, settings: &InstanceSettings, name: Option<String>) -> String {
|
||||
IndexTemplate {
|
||||
name,
|
||||
short: pronoun.render_threeform(),
|
||||
pronouns: settings.pronoun_list.iter().enumerate().collect(),
|
||||
}
|
||||
|
@ -32,24 +34,34 @@ async fn default(settings: web::Data<InstanceSettings>) -> Result<impl Responder
|
|||
.map_err(ErrorBadRequest)?;
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("text/html")
|
||||
.body(render_page(&pronoun, &settings)))
|
||||
.body(render_page(&pronoun, &settings, None)))
|
||||
}
|
||||
|
||||
#[post("/")]
|
||||
async fn create_link(
|
||||
settings: web::Data<InstanceSettings>,
|
||||
form: web::Form<HashMap<usize, u8>>,
|
||||
form: web::Form<HashMap<String, String>>,
|
||||
) -> Result<impl Responder> {
|
||||
eprintln!("create {:?}", form);
|
||||
let weights: Vec<_> = (0..settings.pronoun_list.len())
|
||||
.map(|i| form.get(&i).map(|v| *v).unwrap_or(0))
|
||||
.collect();
|
||||
let mut weights = vec![0; settings.pronoun_list.len()];
|
||||
for (k, v) in form.iter() {
|
||||
if let Ok(i) = k.parse::<usize>() {
|
||||
let w = v.parse::<u8>().map_err(ErrorBadRequest)?;
|
||||
if i < weights.len() - 1 {
|
||||
weights[i] = w;
|
||||
}
|
||||
}
|
||||
}
|
||||
let prefs = InstanceSettings::create_preferences(&weights);
|
||||
eprintln!("prefs: {:?}", prefs);
|
||||
let pref_string = prefs.as_prefstring();
|
||||
eprintln!("prefstring: {}", pref_string);
|
||||
let url = match form.get("name") {
|
||||
Some(name) if !name.is_empty() => format!("/{}/{}", name, pref_string),
|
||||
_ => format!("/{}", pref_string),
|
||||
};
|
||||
Ok(HttpResponse::SeeOther()
|
||||
.header(LOCATION, format!("/{}", pref_string))
|
||||
.header(LOCATION, url)
|
||||
.finish())
|
||||
}
|
||||
|
||||
|
@ -64,7 +76,7 @@ async fn only_prefs(
|
|||
.map_err(ErrorBadRequest)?;
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("text/html")
|
||||
.body(render_page(&pronoun, &settings)))
|
||||
.body(render_page(&pronoun, &settings, None)))
|
||||
}
|
||||
|
||||
#[get("/{name}/{prefs}")]
|
||||
|
@ -78,7 +90,7 @@ async fn prefs_and_name(
|
|||
.map_err(ErrorBadRequest)?;
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("text/html")
|
||||
.body(render_page(&pronoun, &settings)))
|
||||
.body(render_page(&pronoun, &settings, Some(name))))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
|
|
|
@ -11,12 +11,20 @@
|
|||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>My pronouns today are: {{ short }}</h1>
|
||||
<h1>
|
||||
{% if name.is_some() %}
|
||||
{{ name.as_ref().unwrap() }}
|
||||
{% else %}
|
||||
My
|
||||
{% endif %}
|
||||
pronouns today are: {{ short }}</h1>
|
||||
|
||||
<label for="form-toggle">Create your own custom link!</label>
|
||||
<input type="checkbox" id="form-toggle" style="display: none;"/>
|
||||
<div id="form">
|
||||
<form action="/" method="post">
|
||||
<label for="name">Name</label>
|
||||
<input type="text" id="name" name="name"/><br/>
|
||||
{% for item in pronouns %}
|
||||
<label for="{{ item.0 }}">{{ item.1.render_threeform() }}</label>
|
||||
<input type="number" id="{{ item.0 }}" name="{{ item.0 }}" value="1"/><br/>
|
||||
|
|
Loading…
Reference in New Issue