From d405e70b780e1b8bc7e2014962624911b42b32ad Mon Sep 17 00:00:00 2001 From: Colin Reeder Date: Tue, 28 Jul 2020 20:54:14 -0600 Subject: Add sorting options for community posts --- res/lang/en.ftl | 3 +++ res/lang/eo.ftl | 3 +++ res/main.css | 8 ++++++++ src/main.rs | 26 ++++++++++++++++++++++++++ src/routes/communities.rs | 33 +++++++++++++++++++++++++++++++-- 5 files changed, 71 insertions(+), 2 deletions(-) diff --git a/res/lang/en.ftl b/res/lang/en.ftl index d6f0d43..2f0aee7 100644 --- a/res/lang/en.ftl +++ b/res/lang/en.ftl @@ -58,6 +58,9 @@ reply = reply reply_submit = Reply reply_to = Reply to score = { $score } likes +sort = Sort: +sort_hot = hot +sort_new = new submit = Submit submitted = Submitted text_with_markdown = Text (markdown supported) diff --git a/res/lang/eo.ftl b/res/lang/eo.ftl index af95189..f767b96 100644 --- a/res/lang/eo.ftl +++ b/res/lang/eo.ftl @@ -58,6 +58,9 @@ reply = respondi reply_submit = Respondi reply_to = Respondo al score = { $score } ŝatantoj +sort = Ordigi: +sort_hot = furora +sort_new = nova submit = Sendi submitted = Afiŝita text_with_markdown = Teksto (markdown estas permesita) diff --git a/res/main.css b/res/main.css index 148a792..f036c13 100644 --- a/res/main.css +++ b/res/main.css @@ -62,6 +62,14 @@ body { padding-left: 5px; } +.sortOptions { + margin-top: 1em; +} + +.sortOptions > * { + margin-right: .5em; +} + @media (max-width: 768px) { .communitySidebar { display: none; /* TODO still show this somewhere */ diff --git a/src/main.rs b/src/main.rs index 2d40dd5..034990b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![allow(unused_braces)] use crate::resp_types::RespLoginInfo; +use serde_derive::Deserialize; use std::borrow::Cow; use std::collections::HashMap; use std::sync::Arc; @@ -11,6 +12,31 @@ mod resp_types; mod routes; mod util; +#[derive(Deserialize, PartialEq, Clone, Copy)] +#[serde(rename_all = "snake_case")] +pub enum SortType { + Hot, + New, +} + +impl SortType { + pub const VALUES: &'static [SortType] = &[SortType::Hot, SortType::New]; + + pub fn as_str(&self) -> &'static str { + match self { + SortType::Hot => "hot", + SortType::New => "new", + } + } + + pub fn lang_key(&self) -> &'static str { + match self { + SortType::Hot => "sort_hot", + SortType::New => "sort_new", + } + } +} + pub type HttpClient = hyper::Client>; pub struct RouteContext { diff --git a/src/routes/communities.rs b/src/routes/communities.rs index 442b958..7a1e906 100644 --- a/src/routes/communities.rs +++ b/src/routes/communities.rs @@ -93,6 +93,18 @@ async fn page_community( ) -> Result, crate::Error> { let (community_id,) = params; + fn default_sort() -> crate::SortType { + crate::SortType::Hot + } + + #[derive(Deserialize)] + struct Query { + #[serde(default = "default_sort")] + sort: crate::SortType, + } + + let query: Query = serde_urlencoded::from_str(req.uri().query().unwrap_or(""))?; + let lang = crate::get_lang_for_req(&req); let cookies = get_cookie_map_for_req(&req)?; @@ -130,8 +142,10 @@ async fn page_community( ctx.http_client .request(for_client( hyper::Request::get(format!( - "{}/api/unstable/communities/{}/posts", - ctx.backend_host, community_id + "{}/api/unstable/communities/{}/posts?sort={}", + ctx.backend_host, + community_id, + query.sort.as_str(), )) .body(Default::default())?, req.headers(), @@ -215,6 +229,21 @@ async fn page_community( }

{community_info.description.as_ref()}

+
+ {lang.tr("sort", None)} + { + crate::SortType::VALUES.iter() + .map(|value| { + let name = lang.tr(value.lang_key(), None); + if query.sort == *value { + render::rsx! { {name} } + } else { + render::rsx! { {name} } + } + }) + .collect::>() + } +
{ if posts.is_empty() { Some(render::rsx! {

{lang.tr("nothing", None)}

}) -- cgit v1.2.3