Add date editing for posts
This commit is contained in:
parent
d0a4b0f6a6
commit
5848b7360c
|
@ -75,6 +75,19 @@ impl BlogDb {
|
|||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
/// Delete post by id
|
||||
pub async fn set_post_date<S, T>(&self, id: S, date: T) -> anyhow::Result<()>
|
||||
where
|
||||
S: AsRef<str>,
|
||||
T: AsRef<str>,
|
||||
{
|
||||
sqlx::query("UPDATE posts SET date=? WHERE id=?")
|
||||
.bind(date.as_ref())
|
||||
.bind(id.as_ref())
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -23,7 +23,7 @@ main {
|
|||
}
|
||||
|
||||
.admin-widget:not(.admin-widget-user) {
|
||||
max-inline-size: 50%;
|
||||
max-inline-size: calc(50% - (0.5 * var(--default-padding)));
|
||||
}
|
||||
|
||||
.admin-widget-user {
|
||||
|
@ -122,8 +122,8 @@ main {
|
|||
.blog-admin {
|
||||
box-sizing: border-box;
|
||||
display: flex;
|
||||
gap: var(--default-padding);
|
||||
flex-wrap: wrap;
|
||||
gap: var(--default-padding);
|
||||
box-sizing: border-box;
|
||||
min-block-size: 0;
|
||||
flex: 1;
|
||||
|
@ -246,7 +246,7 @@ main {
|
|||
a.entry-content {
|
||||
color: inherit;
|
||||
text-decoration: inherit;
|
||||
inline-size: 90%;
|
||||
max-inline-size: 90%;
|
||||
|
||||
transition: all 0.3s ease;
|
||||
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
const editDateButton = document.getElementById("date-update");
|
||||
const postActions = document.getElementById("post-actions");
|
||||
const children = postActions.innerHTML;
|
||||
|
||||
dateInput = `<button class="form-action" formaction="/admin" formmethod="get" formnovalidate>✕</button><input class="form-action" type="date" name="date" placeholder="Enter Date" autocomplete="off" aria-label="New Date" required /> <button class="form-action" type="submit" formaction="/api/posts/date">Update Date</button>`;
|
||||
|
||||
editDateButton.addEventListener("click", (ev) => {
|
||||
ev.preventDefault();
|
||||
console.log(ev, postActions, dateInput);
|
||||
postActions.innerHTML = dateInput;
|
||||
});
|
|
@ -295,8 +295,7 @@ async fn admin_entries(entry_type: EntryType, session_id: i64, db: &BlogDb) -> S
|
|||
let is_empty: bool;
|
||||
let entries_html = match entry_type {
|
||||
EntryType::Post => {
|
||||
let mut entries = db.get_posts().await.unwrap_or(vec![]);
|
||||
entries.reverse();
|
||||
let entries = db.get_posts().await.unwrap_or(vec![]);
|
||||
let mut entry_list_html = String::new();
|
||||
is_empty = entries.is_empty();
|
||||
if !is_empty {
|
||||
|
|
|
@ -38,6 +38,7 @@ pub(super) fn api(state: BlogState) -> Router {
|
|||
struct Entries {
|
||||
#[serde(default)]
|
||||
item: Vec<String>,
|
||||
date: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
@ -45,6 +46,7 @@ struct Entries {
|
|||
enum PostsEndpoints {
|
||||
delete,
|
||||
unpublish,
|
||||
date,
|
||||
}
|
||||
|
||||
async fn posts(
|
||||
|
@ -71,6 +73,12 @@ async fn posts(
|
|||
state.index_delete(term);
|
||||
}
|
||||
}
|
||||
PostsEndpoints::date => {
|
||||
let date = &data.date.clone().unwrap_or("1970-01-01".to_string());
|
||||
for post_id in data.item.iter() {
|
||||
let _ = db.set_post_date(post_id, &date).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
state.index_commit();
|
||||
let mut headers = HeaderMap::new();
|
||||
|
|
|
@ -3,11 +3,14 @@
|
|||
<h1>Published Posts</h1>
|
||||
</div>
|
||||
<form method="post">
|
||||
<div class="form-actions">
|
||||
<div class="form-actions" id="post-actions">
|
||||
<button class="form-action" type="submit" formaction="/api/posts/delete">Delete posts
|
||||
</button>
|
||||
<button class=" form-action" type="submit" formaction="/api/posts/unpublish">Unpublish Posts
|
||||
</button>
|
||||
<button id="date-update" class="form-action" type="submit" formaction="#update-date" formmethod="get">Edit
|
||||
Date
|
||||
</button>
|
||||
</div>
|
||||
<ul tabindex="-1">
|
||||
</ul>
|
||||
|
|
|
@ -3,3 +3,4 @@
|
|||
<admin-widget type="drafts"></admin-widget>
|
||||
<admin-widget type="posts"></admin-widget>
|
||||
</div>
|
||||
<script src="/assets/js/admin.js"></script>
|
||||
|
|
Loading…
Reference in New Issue