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)]
|
#[derive(Template)]
|
||||||
#[template(path = "index.html")]
|
#[template(path = "index.html")]
|
||||||
struct IndexTemplate<'a> {
|
struct IndexTemplate<'a> {
|
||||||
|
name: Option<String>,
|
||||||
short: String,
|
short: String,
|
||||||
pronouns: Vec<(usize, &'a Pronoun)>,
|
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 {
|
IndexTemplate {
|
||||||
|
name,
|
||||||
short: pronoun.render_threeform(),
|
short: pronoun.render_threeform(),
|
||||||
pronouns: settings.pronoun_list.iter().enumerate().collect(),
|
pronouns: settings.pronoun_list.iter().enumerate().collect(),
|
||||||
}
|
}
|
||||||
|
@ -32,24 +34,34 @@ async fn default(settings: web::Data<InstanceSettings>) -> Result<impl Responder
|
||||||
.map_err(ErrorBadRequest)?;
|
.map_err(ErrorBadRequest)?;
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("text/html")
|
.content_type("text/html")
|
||||||
.body(render_page(&pronoun, &settings)))
|
.body(render_page(&pronoun, &settings, None)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/")]
|
#[post("/")]
|
||||||
async fn create_link(
|
async fn create_link(
|
||||||
settings: web::Data<InstanceSettings>,
|
settings: web::Data<InstanceSettings>,
|
||||||
form: web::Form<HashMap<usize, u8>>,
|
form: web::Form<HashMap<String, String>>,
|
||||||
) -> Result<impl Responder> {
|
) -> Result<impl Responder> {
|
||||||
eprintln!("create {:?}", form);
|
eprintln!("create {:?}", form);
|
||||||
let weights: Vec<_> = (0..settings.pronoun_list.len())
|
let mut weights = vec![0; settings.pronoun_list.len()];
|
||||||
.map(|i| form.get(&i).map(|v| *v).unwrap_or(0))
|
for (k, v) in form.iter() {
|
||||||
.collect();
|
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);
|
let prefs = InstanceSettings::create_preferences(&weights);
|
||||||
eprintln!("prefs: {:?}", prefs);
|
eprintln!("prefs: {:?}", prefs);
|
||||||
let pref_string = prefs.as_prefstring();
|
let pref_string = prefs.as_prefstring();
|
||||||
eprintln!("prefstring: {}", pref_string);
|
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()
|
Ok(HttpResponse::SeeOther()
|
||||||
.header(LOCATION, format!("/{}", pref_string))
|
.header(LOCATION, url)
|
||||||
.finish())
|
.finish())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +76,7 @@ async fn only_prefs(
|
||||||
.map_err(ErrorBadRequest)?;
|
.map_err(ErrorBadRequest)?;
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("text/html")
|
.content_type("text/html")
|
||||||
.body(render_page(&pronoun, &settings)))
|
.body(render_page(&pronoun, &settings, None)))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/{name}/{prefs}")]
|
#[get("/{name}/{prefs}")]
|
||||||
|
@ -78,7 +90,7 @@ async fn prefs_and_name(
|
||||||
.map_err(ErrorBadRequest)?;
|
.map_err(ErrorBadRequest)?;
|
||||||
Ok(HttpResponse::Ok()
|
Ok(HttpResponse::Ok()
|
||||||
.content_type("text/html")
|
.content_type("text/html")
|
||||||
.body(render_page(&pronoun, &settings)))
|
.body(render_page(&pronoun, &settings, Some(name))))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
|
|
|
@ -11,12 +11,20 @@
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<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>
|
<label for="form-toggle">Create your own custom link!</label>
|
||||||
<input type="checkbox" id="form-toggle" style="display: none;"/>
|
<input type="checkbox" id="form-toggle" style="display: none;"/>
|
||||||
<div id="form">
|
<div id="form">
|
||||||
<form action="/" method="post">
|
<form action="/" method="post">
|
||||||
|
<label for="name">Name</label>
|
||||||
|
<input type="text" id="name" name="name"/><br/>
|
||||||
{% for item in pronouns %}
|
{% for item in pronouns %}
|
||||||
<label for="{{ item.0 }}">{{ item.1.render_threeform() }}</label>
|
<label for="{{ item.0 }}">{{ item.1.render_threeform() }}</label>
|
||||||
<input type="number" id="{{ item.0 }}" name="{{ item.0 }}" value="1"/><br/>
|
<input type="number" id="{{ item.0 }}" name="{{ item.0 }}" value="1"/><br/>
|
||||||
|
|
Loading…
Reference in a new issue