move add watch button to template
This commit is contained in:
parent
8708271d9d
commit
5e998dfe86
3 changed files with 35 additions and 21 deletions
|
@ -8,7 +8,7 @@ use julid::Julid;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use sqlx::{query, query_as, query_scalar, SqlitePool};
|
use sqlx::{query, query_as, query_scalar, SqlitePool};
|
||||||
|
|
||||||
use super::templates::{AddNewWatchPage, GetWatchPage, SearchWatchesPage};
|
use super::templates::{AddNewWatchPage, AddWatchButton, GetWatchPage, SearchWatchesPage};
|
||||||
use crate::{
|
use crate::{
|
||||||
util::{empty_string_as_none, year_to_epoch},
|
util::{empty_string_as_none, year_to_epoch},
|
||||||
AuthSession, MyWatchesPage, ShowKind, Watch, WatchQuest,
|
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_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";
|
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 {
|
if let Some(user) = auth.user {
|
||||||
let watch = Julid::from_string(&watch).unwrap();
|
let watch = Julid::from_string(&watch).unwrap();
|
||||||
let public = query.public;
|
let public = query.public;
|
||||||
let quest: Option<WatchQuest> =
|
let quest: Option<WatchQuest> = query_as(GET_QUEST_WITH_PUBLICITY_QUERY)
|
||||||
query_as("select * from watch_quests where user = ? and watch = ? and public = ?")
|
.bind(user.id)
|
||||||
.bind(user.id)
|
.bind(watch)
|
||||||
.bind(watch)
|
.bind(public)
|
||||||
.bind(public)
|
.fetch_optional(&pool)
|
||||||
.fetch_optional(&pool)
|
.await
|
||||||
.await
|
.map_err(|e| {
|
||||||
.map_err(|e| {
|
tracing::error!("Got error from checking watch status: {e:?}");
|
||||||
tracing::error!("Got error from checking watch status: {e:?}");
|
})?;
|
||||||
})?;
|
|
||||||
match quest {
|
match quest {
|
||||||
Some(_) => Ok("✓".into_response()),
|
Some(_) => Ok("✓".into_response()),
|
||||||
_ => Ok(format!(
|
None => Ok(AddWatchButton {
|
||||||
"<form id=\"add-watch-{watch}\">
|
watch,
|
||||||
<input type=\"hidden\" name=\"watch\" value=\"{watch}\">
|
public,
|
||||||
<input type=\"hidden\" name=\"public\" value=\"true\">
|
watched_already: false,
|
||||||
<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>
|
|
||||||
"
|
|
||||||
)
|
|
||||||
.into_response()),
|
.into_response()),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
|
use julid::Julid;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{OptionalOptionalUser, User, Watch};
|
use crate::{OptionalOptionalUser, User, Watch};
|
||||||
|
@ -30,3 +31,11 @@ pub struct GetWatchPage {
|
||||||
pub struct AddNewWatchPage {
|
pub struct AddNewWatchPage {
|
||||||
pub user: Option<User>,
|
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,
|
||||||
|
}
|
||||||
|
|
7
templates/elements/add_watch_button.html
Normal file
7
templates/elements/add_watch_button.html
Normal 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>
|
Loading…
Reference in a new issue