Fix search pages

This commit is contained in:
spikecodes 2021-01-02 22:37:54 -08:00
parent 5d26b5c764
commit a3384cbaa6
3 changed files with 33 additions and 18 deletions

View File

@ -4,20 +4,26 @@ use actix_web::{HttpRequest, HttpResponse};
use askama::Template;
// STRUCTS
struct SearchParams {
q: String,
sort: String,
t: String,
before: String,
after: String,
restrict_sr: String,
}
#[derive(Template)]
#[template(path = "search.html", escape = "none")]
struct SearchTemplate {
posts: Vec<Post>,
query: String,
sub: String,
sort: (String, String),
ends: (String, String),
params: SearchParams,
}
// SERVICES
pub async fn find(req: HttpRequest) -> HttpResponse {
let path = format!("{}.json?{}", req.path(), req.query_string());
let q = param(&path, "q");
let sort = if param(&path, "sort").is_empty() {
"relevance".to_string()
} else {
@ -29,10 +35,15 @@ pub async fn find(req: HttpRequest) -> HttpResponse {
Ok(posts) => HttpResponse::Ok().content_type("text/html").body(
SearchTemplate {
posts: posts.0,
query: q,
sub,
sort: (sort, param(&path, "t")),
ends: (param(&path, "after"), posts.1),
params: SearchParams {
q: param(&path, "q"),
sort,
t: param(&path, "t"),
before: param(&path, "after"),
after: posts.1,
restrict_sr: param(&path, "restrict_sr"),
},
}
.render()
.unwrap(),

View File

@ -97,7 +97,7 @@ pub struct ErrorTemplate {
// Grab a query param from a url
pub fn param(path: &str, value: &str) -> String {
let url = Url::parse(format!("https://reddit.com/{}", path).as_str()).unwrap();
let url = Url::parse(format!("https://libredd.it/{}", path).as_str()).unwrap();
let pairs: std::collections::HashMap<_, _> = url.query_pairs().into_owned().collect();
pairs.get(value).unwrap_or(&String::new()).to_owned()
}

View File

@ -1,22 +1,22 @@
{% extends "base.html" %}
{% import "utils.html" as utils %}
{% block title %}Libreddit: search results - {{ query }}{% endblock %}
{% block title %}Libreddit: search results - {{ params.q }}{% endblock %}
{% block content %}
<div id="column_one">
<form id="search_sort">
<input id="search" type="text" name="q" placeholder="Search" value="{{ query }}">
<input id="search" type="text" name="q" placeholder="Search" value="{{ params.q }}">
{% if sub != "" %}
<div id="inside">
<input type="checkbox" name="restrict_sr" id="restrict_sr">
<input type="checkbox" name="restrict_sr" id="restrict_sr" {% if params.restrict_sr != "" %}checked{% endif %}>
<label for="restrict_sr">in r/{{ sub }}</label>
</div>
{% endif %}
<select id="sort_options" name="sort">
{% call utils::options(sort.0, ["relevance", "hot", "top", "new", "comments"], "") %}
</select>{% if sort.0 != "new" %}<select id="timeframe" name="t">
{% call utils::options(sort.1, ["hour", "day", "week", "month", "year", "all"], "all") %}
{% call utils::options(params.sort, ["relevance", "hot", "top", "new", "comments"], "") %}
</select>{% if params.sort != "new" %}<select id="timeframe" name="t">
{% call utils::options(params.t, ["hour", "day", "week", "month", "year", "all"], "all") %}
</select>{% endif %}<input id="sort_submit" type="submit" value="&rarr;">
</form>
{% for post in posts %}
@ -62,12 +62,16 @@
{% endfor %}
<footer>
{% if ends.0 != "" %}
<a href="?sort={{ sort.0 }}{% if sort.0 == "top" %}&t={{ sort.1 }}{% endif %}&before={{ ends.0 }}">PREV</a>
{% if params.before != "" %}
<a href="?q={{ params.q }}&restrict_sr={{ params.restrict_sr }}
&sort={{ params.sort }}&t={{ params.t }}
&before={{ params.before }}">PREV</a>
{% endif %}
{% if ends.1 != "" %}
<a href="?sort={{ sort.0 }}{% if sort.0 == "top" %}&t={{ sort.1 }}{% endif %}&after={{ ends.1 }}">NEXT</a>
{% if params.after != "" %}
<a href="?q={{ params.q }}&restrict_sr={{ params.restrict_sr }}
&sort={{ params.sort }}&t={{ params.t }}
&after={{ params.after }}">NEXT</a>
{% endif %}
</footer>
</div>