Front page config and settings note

This commit is contained in:
spikecodes 2021-01-08 20:55:40 -08:00
parent ef2f9ad12b
commit b8cdc605a2
6 changed files with 32 additions and 7 deletions

View File

@ -13,6 +13,7 @@ struct SettingsTemplate {
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
pub struct SettingsForm { pub struct SettingsForm {
front_page: Option<String>,
layout: Option<String>, layout: Option<String>,
comment_sort: Option<String>, comment_sort: Option<String>,
hide_nsfw: Option<String>, hide_nsfw: Option<String>,
@ -30,8 +31,8 @@ pub async fn get(req: HttpRequest) -> HttpResponse {
pub async fn set(req: HttpRequest, form: Form<SettingsForm>) -> HttpResponse { pub async fn set(req: HttpRequest, form: Form<SettingsForm>) -> HttpResponse {
let mut res = HttpResponse::Found(); let mut res = HttpResponse::Found();
let names = vec!["layout", "comment_sort", "hide_nsfw"]; let names = vec!["front_page", "layout", "comment_sort", "hide_nsfw"];
let values = vec![&form.layout, &form.comment_sort, &form.hide_nsfw]; let values = vec![&form.front_page, &form.layout, &form.comment_sort, &form.hide_nsfw];
for (i, name) in names.iter().enumerate() { for (i, name) in names.iter().enumerate() {
match values[i] { match values[i] {

View File

@ -1,5 +1,5 @@
// CRATES // CRATES
use crate::utils::{error, fetch_posts, format_num, format_url, param, prefs, request, rewrite_url, val, Post, Preferences, Subreddit}; use crate::utils::{cookie, error, fetch_posts, format_num, format_url, param, prefs, request, rewrite_url, val, Post, Preferences, Subreddit};
use actix_web::{HttpRequest, HttpResponse, Result}; use actix_web::{HttpRequest, HttpResponse, Result};
use askama::Template; use askama::Template;
@ -25,10 +25,15 @@ struct WikiTemplate {
// SERVICES // SERVICES
pub async fn page(req: HttpRequest) -> HttpResponse { pub async fn page(req: HttpRequest) -> HttpResponse {
let path = format!("{}.json?{}", req.path(), req.query_string()); let path = format!("{}.json?{}", req.path(), req.query_string());
let sub_name = req.match_info().get("sub").unwrap_or("popular").to_string(); let default = cookie(&req, "front_page");
let sub_name = req
.match_info()
.get("sub")
.unwrap_or(if default.is_empty() { "popular" } else { default.as_str() })
.to_string();
let sort = req.match_info().get("sort").unwrap_or("hot").to_string(); let sort = req.match_info().get("sort").unwrap_or("hot").to_string();
let sub = if !&sub_name.contains('+') && sub_name != "popular" { let sub = if !&sub_name.contains('+') && sub_name != "popular" && sub_name != "all" {
subreddit(&sub_name).await.unwrap_or_default() subreddit(&sub_name).await.unwrap_or_default()
} else { } else {
Subreddit::default() Subreddit::default()

View File

@ -31,7 +31,7 @@ pub async fn profile(req: HttpRequest) -> HttpResponse {
match posts { match posts {
Ok((posts, after)) => { Ok((posts, after)) => {
let s = UserTemplate { let s = UserTemplate {
user: user.unwrap(), user: user.unwrap_or_default(),
posts, posts,
sort: (sort, param(&path, "t")), sort: (sort, param(&path, "t")),
ends: (param(&path, "after"), after), ends: (param(&path, "after"), after),
@ -42,7 +42,7 @@ pub async fn profile(req: HttpRequest) -> HttpResponse {
HttpResponse::Ok().content_type("text/html").body(s) HttpResponse::Ok().content_type("text/html").body(s)
} }
// If there is an error show error page // If there is an error show error page
Err(msg) => error(msg.to_string()).await, Err(msg) => {dbg!(msg);error(msg.to_string()).await},
} }
} }

View File

@ -51,6 +51,7 @@ pub struct Comment {
pub replies: Vec<Comment>, pub replies: Vec<Comment>,
} }
#[derive(Default)]
// User struct containing metadata about user // User struct containing metadata about user
pub struct User { pub struct User {
pub name: String, pub name: String,
@ -93,6 +94,7 @@ pub struct ErrorTemplate {
} }
pub struct Preferences { pub struct Preferences {
pub front_page: String,
pub layout: String, pub layout: String,
pub hide_nsfw: String, pub hide_nsfw: String,
pub comment_sort: String, pub comment_sort: String,
@ -105,6 +107,7 @@ pub struct Preferences {
// Build preferences from cookies // Build preferences from cookies
pub fn prefs(req: HttpRequest) -> Preferences { pub fn prefs(req: HttpRequest) -> Preferences {
Preferences { Preferences {
front_page: cookie(&req, "front_page"),
layout: cookie(&req, "layout"), layout: cookie(&req, "layout"),
hide_nsfw: cookie(&req, "hide_nsfw"), hide_nsfw: cookie(&req, "hide_nsfw"),
comment_sort: cookie(&req, "comment_sort"), comment_sort: cookie(&req, "comment_sort"),

View File

@ -70,6 +70,7 @@ main {
footer { footer {
display: flex; display: flex;
justify-content: center; justify-content: center;
margin-top: 20px;
} }
footer > a { footer > a {
@ -631,6 +632,13 @@ input[type="submit"]:hover { color: var(--accent); }
align-items: center; align-items: center;
} }
#settings_note {
font-size: 14px;
max-width: 300px;
margin-top: 10px;
opacity: 0.75;
}
#prefs { #prefs {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@ -645,6 +653,7 @@ input[type="submit"]:hover { color: var(--accent); }
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
width: 100%; width: 100%;
height: 35px;
align-items: center; align-items: center;
} }

View File

@ -11,6 +11,12 @@
<main> <main>
<form id="settings" action="/settings" method="POST"> <form id="settings" action="/settings" method="POST">
<div id="prefs"> <div id="prefs">
<div id="front_page">
<label for="front_page">Front page:</label>
<select name="front_page">
{% call utils::options(prefs.front_page, ["popular", "all"], "popular") %}
</select>
</div>
<div id="layout"> <div id="layout">
<label for="layout">Layout:</label> <label for="layout">Layout:</label>
<select name="layout"> <select name="layout">
@ -28,6 +34,7 @@
<input type="checkbox" name="hide_nsfw" {% if prefs.hide_nsfw == "on" %}checked{% endif %}> <input type="checkbox" name="hide_nsfw" {% if prefs.hide_nsfw == "on" %}checked{% endif %}>
</div> </div>
</div> </div>
<p id="settings_note"><b>Note:</b> settings are saved in browser cookies. Clearing your cookie data will reset them.</p>
<input id="save" type="submit" value="Save"> <input id="save" type="submit" value="Save">
</form> </form>
</main> </main>