have drop-down for public/private in add-watch from search.

This commit is contained in:
Joe Ardent 2024-01-01 22:44:29 -08:00
parent 5e998dfe86
commit dab2dc4081
5 changed files with 18 additions and 31 deletions

View File

@ -19,10 +19,7 @@ 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_QUEST_QUERY: &str = "select * from watch_quests where user = ? and watch = ?";
const GET_WATCH_QUERY: &str = "select * from watches where id = $1";
@ -82,11 +79,6 @@ pub struct SearchQuery {
pub year: Option<i64>,
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
pub struct StatusQuery {
pub public: bool,
}
// kinda the main form?
#[derive(Debug, Default, Deserialize, PartialEq, Eq)]
pub struct PostAddNewWatch {
@ -106,7 +98,6 @@ pub struct PostAddNewWatch {
pub struct PostAddExistingWatch {
pub watch: String,
pub public: bool,
pub watched_already: bool,
}
//-************************************************************************
@ -139,7 +130,7 @@ pub async fn post_add_new_watch(
let quest = WatchQuest {
user: user.id,
public: !form.private,
watched: form.watched_already,
watched: false,
watch: watch_id,
};
add_watch_quest_impl(&pool, &quest)
@ -182,7 +173,7 @@ pub async fn post_add_watch_quest(
user: user.id,
watch: Julid::from_string(&form.watch).unwrap(),
public: form.public,
watched: form.watched_already,
watched: false,
};
add_watch_quest_impl(&pool, &quest)
.await
@ -288,29 +279,26 @@ pub async fn get_search_watch(
pub async fn get_watch_status(
auth: AuthSession,
State(pool): State<SqlitePool>,
query: Query<StatusQuery>,
Path(watch): Path<String>,
) -> Result<impl IntoResponse, ()> {
if let Some(user) = auth.user {
let watch = Julid::from_string(&watch).unwrap();
let public = query.public;
let quest: Option<WatchQuest> = query_as(GET_QUEST_WITH_PUBLICITY_QUERY)
let quest: Option<WatchQuest> = query_as(GET_QUEST_QUERY)
.bind(user.id)
.bind(watch)
.bind(public)
.fetch_optional(&pool)
.await
.map_err(|e| {
tracing::error!("Got error from checking watch status: {e:?}");
})?;
let checkmark = "&#10003;";
match quest {
Some(_) => Ok("&#10003;".into_response()),
None => Ok(AddWatchButton {
watch,
public,
watched_already: false,
Some(quest) if quest.watched => Ok(format!("{checkmark} watched").into_response()),
Some(quest) => {
let public = if quest.public { "public" } else { "private" };
Ok(format!("{checkmark} ({public})").into_response())
}
.into_response()),
None => Ok(AddWatchButton { watch }.into_response()),
}
} else {
Ok("<a href='/login'>Login to add</a>".into_response())

View File

@ -36,6 +36,4 @@ pub struct AddNewWatchPage {
#[template(path = "elements/add_watch_button.html")]
pub struct AddWatchButton {
pub watch: Julid,
pub public: bool,
pub watched_already: bool,
}

View File

@ -1,7 +1,8 @@
<form id="add-watch-{{self.watch}}">
<button hx-post="/add/watch" hx-target="#add-watch-{{watch}}" hx-trigger="click" hx-swap="outerHTML">add</button>
<select name="public" id="add-public-watch">
<option value="true">public</option>
<option value="false">private</option>
</select>
<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>

View File

@ -13,7 +13,7 @@
<th>Title</th>
<th>Type</th>
<th>Year</th>
<th>Public watchlist?</th>
<th>Watching?</th>
</tr>
</thead>
<tbody>

View File

@ -4,8 +4,8 @@
<td> {% call m::get_or_default(watch.year(), "when??") -%}</td>
<td>
<span id="add-watch-{{watch.id}}">
<span hx-get="/watch/status/{{watch.id}}?public=true" hx-target="this" hx-trigger="load, reveal"
hx-swap="outerHTML">wanna watch?</span>
<span hx-get="/watch/status/{{watch.id}}" hx-target="this" hx-trigger="load, reveal"
hx-swap="outerHTML">???</span>
</span>
</td>
</tr>