Fix no poll showing up if no one voted in it yet
This commit is contained in:
parent
28812699c7
commit
cdb1b35ced
1 changed files with 179 additions and 159 deletions
54
src/main.rs
54
src/main.rs
|
@ -1,4 +1,5 @@
|
|||
use axum::extract::{Path, State};
|
||||
use axum::http::header::SET_COOKIE;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::{AppendHeaders, Html, IntoResponse, Redirect, Response};
|
||||
use axum::Form;
|
||||
|
@ -6,11 +7,10 @@ use axum::{
|
|||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use axum::http::header::SET_COOKIE;
|
||||
use axum_extra::extract::CookieJar;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct Page {
|
||||
|
@ -212,14 +212,15 @@ async fn view_current_poll(State(state): State<AppState>) -> Response {
|
|||
.filter(|poll| poll.id == current_poll)
|
||||
.collect();
|
||||
if !current_poll.is_empty() {
|
||||
if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) {
|
||||
let poll_config = current_poll.first().unwrap();
|
||||
let mut choices = vec![];
|
||||
|
||||
let find_vote_choice = |id: i32| {
|
||||
if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) {
|
||||
for choice in &poll.choices {
|
||||
if choice.id == id {
|
||||
return Some(choice);
|
||||
return Some(choice.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -253,7 +254,6 @@ async fn view_current_poll(State(state): State<AppState>) -> Response {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(StatusCode::NOT_FOUND, "No poll is open right now!").into_response()
|
||||
}
|
||||
|
@ -263,7 +263,10 @@ struct SubmitForm {
|
|||
choice: i32,
|
||||
}
|
||||
|
||||
async fn vote_current_poll(State(state): State<AppState>, Form(form): Form<SubmitForm>) -> Response {
|
||||
async fn vote_current_poll(
|
||||
State(state): State<AppState>,
|
||||
Form(form): Form<SubmitForm>,
|
||||
) -> Response {
|
||||
let id = form.choice;
|
||||
tracing::info!("Submitting vote for choice {id}");
|
||||
|
||||
|
@ -279,7 +282,10 @@ async fn vote_current_poll(State(state): State<AppState>, Form(form): Form<Submi
|
|||
state.vote_poll(current_poll.first().unwrap().id, id);
|
||||
state.save_polls();
|
||||
|
||||
let headers = AppendHeaders([(SET_COOKIE, format!("poll_{}={}", current_poll.first().unwrap().id, id))]);
|
||||
let headers = AppendHeaders([(
|
||||
SET_COOKIE,
|
||||
format!("poll_{}={}", current_poll.first().unwrap().id, id),
|
||||
)]);
|
||||
|
||||
// make sure to redirect the iframe back to results
|
||||
return (headers, Redirect::to("/polls/frame/results")).into_response();
|
||||
|
@ -302,14 +308,15 @@ async fn poll_frame_vote(State(state): State<AppState>, jar: CookieJar) -> Html<
|
|||
.filter(|poll| poll.id == current_poll)
|
||||
.collect();
|
||||
if !current_poll.is_empty() {
|
||||
if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) {
|
||||
let poll_config = current_poll.first().unwrap();
|
||||
let mut choices = vec![];
|
||||
|
||||
let find_vote_choice = |id: i32| {
|
||||
if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) {
|
||||
for choice in &poll.choices {
|
||||
if choice.id == id {
|
||||
return Some(choice);
|
||||
return Some(choice.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -359,10 +366,18 @@ async fn poll_frame_vote(State(state): State<AppState>, jar: CookieJar) -> Html<
|
|||
|
||||
html.push_str("<form action=\"/polls/vote\" method=\"post\">");
|
||||
html.push_str("<fieldset>");
|
||||
let inert = if existing_choice.is_some() { "inert" } else { "" };
|
||||
let inert = if existing_choice.is_some() {
|
||||
"inert"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
html.push_str(&format!("<div {}>", inert));
|
||||
for choice in choices {
|
||||
let checked = if Some(choice.id) == existing_choice { "checked" } else { "" };
|
||||
let checked = if Some(choice.id) == existing_choice {
|
||||
"checked"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
html.push_str(&format!("<input type=\"radio\" name=\"choice\" id=\"choice{}\" value=\"{}\" {}>", choice.id, choice.id, checked));
|
||||
html.push_str(&format!(
|
||||
"<label for=\"choice{}\">{}</label>",
|
||||
|
@ -372,7 +387,9 @@ async fn poll_frame_vote(State(state): State<AppState>, jar: CookieJar) -> Html<
|
|||
}
|
||||
html.push_str("</div>");
|
||||
html.push_str(&format!("<input type=\"submit\" {}/>", inert));
|
||||
html.push_str("<input form=\"results\" type=\"submit\" value=\"View Results\"/>");
|
||||
html.push_str(
|
||||
"<input form=\"results\" type=\"submit\" value=\"View Results\"/>",
|
||||
);
|
||||
html.push_str("</fieldset>");
|
||||
html.push_str("</form>");
|
||||
|
||||
|
@ -381,7 +398,6 @@ async fn poll_frame_vote(State(state): State<AppState>, jar: CookieJar) -> Html<
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Html("<html><p>No poll is open right now!</p></html>".to_string())
|
||||
}
|
||||
|
@ -396,14 +412,15 @@ async fn poll_frame_results(State(state): State<AppState>, jar: CookieJar) -> Ht
|
|||
.filter(|poll| poll.id == current_poll)
|
||||
.collect();
|
||||
if !current_poll.is_empty() {
|
||||
if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) {
|
||||
let poll_config = current_poll.first().unwrap();
|
||||
let mut choices = vec![];
|
||||
|
||||
let find_vote_choice = |id: i32| {
|
||||
if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) {
|
||||
for choice in &poll.choices {
|
||||
if choice.id == id {
|
||||
return Some(choice);
|
||||
return Some(choice.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -455,7 +472,11 @@ async fn poll_frame_results(State(state): State<AppState>, jar: CookieJar) -> Ht
|
|||
html.push_str("<fieldset>");
|
||||
html.push_str("<div inert>");
|
||||
for choice in choices {
|
||||
let checked = if Some(choice.id) == existing_choice { "checked" } else { "" };
|
||||
let checked = if Some(choice.id) == existing_choice {
|
||||
"checked"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
html.push_str(&format!("<input type=\"radio\" name=\"choice\" id=\"choice{}\" value=\"{}\" {}>", choice.id, choice.id, checked));
|
||||
html.push_str(&format!(
|
||||
|
@ -475,7 +496,6 @@ async fn poll_frame_results(State(state): State<AppState>, jar: CookieJar) -> Ht
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Html("<html><p>No poll is open right now!</p></html>".to_string())
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue