move add watch button to template

This commit is contained in:
Joe Ardent 2024-01-01 17:27:17 -08:00
parent 8708271d9d
commit 5e998dfe86
3 changed files with 35 additions and 21 deletions

View file

@ -8,7 +8,7 @@ use julid::Julid;
use serde::Deserialize;
use sqlx::{query, query_as, query_scalar, SqlitePool};
use super::templates::{AddNewWatchPage, GetWatchPage, SearchWatchesPage};
use super::templates::{AddNewWatchPage, AddWatchButton, GetWatchPage, SearchWatchesPage};
use crate::{
util::{empty_string_as_none, year_to_epoch},
AuthSession, MyWatchesPage, ShowKind, Watch, WatchQuest,
@ -19,6 +19,10 @@ use crate::{
//-************************************************************************
const GET_SAVED_WATCHES_QUERY: &str = "select * from watches inner join watch_quests quests on quests.user = $1 and quests.watch = watches.id";
const GET_QUEST_WITH_PUBLICITY_QUERY: &str =
"select * from watch_quests where user = ? and watch = ? and public = ?";
const GET_QUEST_WITH_WATCHED_QUERY: &str =
"select * from watch_quests where user = ? and watch = ? and watched = ?";
const GET_WATCH_QUERY: &str = "select * from watches where id = $1";
@ -290,28 +294,22 @@ pub async fn get_watch_status(
if let Some(user) = auth.user {
let watch = Julid::from_string(&watch).unwrap();
let public = query.public;
let quest: Option<WatchQuest> =
query_as("select * from watch_quests where user = ? and watch = ? and public = ?")
.bind(user.id)
.bind(watch)
.bind(public)
.fetch_optional(&pool)
.await
.map_err(|e| {
tracing::error!("Got error from checking watch status: {e:?}");
})?;
let quest: Option<WatchQuest> = query_as(GET_QUEST_WITH_PUBLICITY_QUERY)
.bind(user.id)
.bind(watch)
.bind(public)
.fetch_optional(&pool)
.await
.map_err(|e| {
tracing::error!("Got error from checking watch status: {e:?}");
})?;
match quest {
Some(_) => Ok("&#10003;".into_response()),
_ => Ok(format!(
"<form id=\"add-watch-{watch}\">
<input type=\"hidden\" name=\"watch\" value=\"{watch}\">
<input type=\"hidden\" name=\"public\" value=\"true\">
<input type=\"hidden\" name=\"watched_already\" value=\"false\">
<button hx-post=\"/add/watch\" hx-target=\"#add-watch-{watch}\" hx-trigger=\"click\"
hx-swap=\"outerHTML\">wanna watch?</button>
</form>
"
)
None => Ok(AddWatchButton {
watch,
public,
watched_already: false,
}
.into_response()),
}
} else {

View file

@ -1,4 +1,5 @@
use askama::Template;
use julid::Julid;
use serde::{Deserialize, Serialize};
use crate::{OptionalOptionalUser, User, Watch};
@ -30,3 +31,11 @@ pub struct GetWatchPage {
pub struct AddNewWatchPage {
pub user: Option<User>,
}
#[derive(Debug, Default, Template, Deserialize, Serialize, PartialEq, Eq)]
#[template(path = "elements/add_watch_button.html")]
pub struct AddWatchButton {
pub watch: Julid,
pub public: bool,
pub watched_already: bool,
}

View file

@ -0,0 +1,7 @@
<form id="add-watch-{{self.watch}}">
<input type="hidden" name="watch" value="{{self.watch}}">
<input type="hidden" name="public" value="{{self.public}}">
<input type="hidden" name="watched_already" value="{{self.watched_already}}">
<button hx-post="/add/watch" hx-target="#add-watch-{{watch}}" hx-trigger="click" hx-swap="outerHTML">wanna
watch?</button>
</form>