diff options
author | Colin Reeder <colin@vpzom.click> | 2020-07-08 20:10:12 -0600 |
---|---|---|
committer | Colin Reeder <colin@vpzom.click> | 2020-07-08 20:10:12 -0600 |
commit | c036d7d704d69eaae9534e8be66ba547bc8eeb2c (patch) | |
tree | 467eb6ba38cbb647f64f2860daaa1eb123f05aff | |
parent | ad306994f663550731f0c6a519f645b0d22cbc0a (diff) |
Support unliking
-rw-r--r-- | src/components/mod.rs | 18 | ||||
-rw-r--r-- | src/routes/mod.rs | 33 | ||||
-rw-r--r-- | src/routes/posts.rs | 51 |
3 files changed, 94 insertions, 8 deletions
diff --git a/src/components/mod.rs b/src/components/mod.rs index 2ffd97c..545ab56 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -20,9 +20,21 @@ pub fn Comment<'comment, 'base_data>( if base_data.login.is_some() { Some(render::rsx! { <> - <form method={"POST"} action={format!("/comments/{}/like", comment.id)} style={"display: inline"}> - <BoolSubmitButton value={comment.your_vote.is_some()} do_text={"Like"} done_text={"Liked"} /> - </form> + { + if comment.your_vote.is_some() { + render::rsx! { + <form method={"POST"} action={format!("/comments/{}/unlike", comment.id)}> + <button type={"submit"}>{"Unlike"}</button> + </form> + } + } else { + render::rsx! { + <form method={"POST"} action={format!("/comments/{}/like", comment.id)}> + <button type={"submit"}>{"Like"}</button> + </form> + } + } + } <a href={format!("/comments/{}", comment.id)}>{"reply"}</a> </> }) diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 283d496..5090b32 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -307,6 +307,35 @@ async fn handler_comment_like( .body("Successfully liked.".into())?) } +async fn handler_comment_unlike( + params: (i64,), + ctx: Arc<crate::RouteContext>, + req: hyper::Request<hyper::Body>, +) -> Result<hyper::Response<hyper::Body>, crate::Error> { + let (comment_id,) = params; + + let cookies = get_cookie_map_for_req(&req)?; + + res_to_error( + ctx.http_client + .request(with_auth( + hyper::Request::post(format!( + "{}/api/unstable/comments/{}/unlike", + ctx.backend_host, comment_id + )) + .body(Default::default())?, + &cookies, + )?) + .await?, + ) + .await?; + + Ok(hyper::Response::builder() + .status(hyper::StatusCode::SEE_OTHER) + .header(hyper::header::LOCATION, format!("/comments/{}", comment_id)) + .body("Successfully unliked.".into())?) +} + async fn handler_comment_submit_reply( params: (i64,), ctx: Arc<crate::RouteContext>, @@ -847,6 +876,10 @@ pub fn route_root() -> crate::RouteNode<()> { crate::RouteNode::new().with_handler_async("POST", handler_comment_like), ) .with_child( + "unlike", + crate::RouteNode::new().with_handler_async("POST", handler_comment_unlike), + ) + .with_child( "submit_reply", crate::RouteNode::new() .with_handler_async("POST", handler_comment_submit_reply), diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 0a41490..fd6cf26 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -2,7 +2,7 @@ use super::{ fetch_base_data, get_cookie_map_for_headers, get_cookie_map_for_req, html_response, res_to_error, with_auth, }; -use crate::components::{BoolSubmitButton, Comment, CommunityLink, Content, HTPage, UserLink}; +use crate::components::{Comment, CommunityLink, Content, HTPage, UserLink}; use crate::resp_types::RespPostInfo; use crate::util::author_is_me; use std::sync::Arc; @@ -51,10 +51,18 @@ async fn page_post( {" "} { if base_data.login.is_some() { - Some(render::rsx! { - <form method={"POST"} action={format!("/posts/{}/like", post_id)}> - <BoolSubmitButton value={post.your_vote.is_some()} do_text={"Like"} done_text={"Liked"} /> - </form> + Some(if post.your_vote.is_some() { + render::rsx! { + <form method={"POST"} action={format!("/posts/{}/unlike", post_id)}> + <button type={"submit"}>{"Unlike"}</button> + </form> + } + } else { + render::rsx! { + <form method={"POST"} action={format!("/posts/{}/like", post_id)}> + <button type={"submit"}>{"Like"}</button> + </form> + } }) } else { None @@ -216,6 +224,35 @@ async fn handler_post_like( .body("Successfully liked.".into())?) } +async fn handler_post_unlike( + params: (i64,), + ctx: Arc<crate::RouteContext>, + req: hyper::Request<hyper::Body>, +) -> Result<hyper::Response<hyper::Body>, crate::Error> { + let (post_id,) = params; + + let cookies = get_cookie_map_for_req(&req)?; + + res_to_error( + ctx.http_client + .request(with_auth( + hyper::Request::post(format!( + "{}/api/unstable/posts/{}/unlike", + ctx.backend_host, post_id + )) + .body(Default::default())?, + &cookies, + )?) + .await?, + ) + .await?; + + Ok(hyper::Response::builder() + .status(hyper::StatusCode::SEE_OTHER) + .header(hyper::header::LOCATION, format!("/posts/{}", post_id)) + .body("Successfully unliked.".into())?) +} + async fn handler_post_submit_reply( params: (i64,), ctx: Arc<crate::RouteContext>, @@ -269,6 +306,10 @@ pub fn route_posts() -> crate::RouteNode<()> { crate::RouteNode::new().with_handler_async("POST", handler_post_like), ) .with_child( + "unlike", + crate::RouteNode::new().with_handler_async("POST", handler_post_unlike), + ) + .with_child( "submit_reply", crate::RouteNode::new().with_handler_async("POST", handler_post_submit_reply), ), |