Modify with Feed Form

This commit is contained in:
2025-01-29 22:03:57 +01:00
parent 53e7d79f7c
commit 03510fd68a
4 changed files with 88 additions and 4 deletions

View File

@@ -91,7 +91,8 @@ async fn main() -> Result<()> {
.nest_service("/assets", ServeDir::new("assets"))
.route("/", get(index))
.route("/feed/", get(get_list_feed).post(post_feed))
.route("/feed/:id", get(get_feed).delete(delete_feed))
.route("/feed/:id", get(get_feed).delete(delete_feed).put(put_feed))
.route("/feedform/:id", get(get_feed_form))
.layer(middlewares)
.with_state(context.clone());
// Run our app with hyper
@@ -136,7 +137,42 @@ async fn get_feed(
}
}
#[derive(Deserialize)]
#[derive(Template)]
#[template(path="feed_form.html")]
struct FeedFormTemplate {
feed: RssFeed
}
async fn get_feed_form(
State(ctx): State<AppContext>,
Path(feed_id): Path<i32>
) -> impl IntoResponse {
let conn = ctx.pool.get().await.unwrap();
use self::schema::rss_feeds::dsl::*;
let result = conn.interact(move |conn| {
rss_feeds
.find(feed_id)
.select(RssFeed::as_select())
.first(conn)
.optional()
.expect("Error loading feeds")
}).await.unwrap();
if let Some(feed) = result {
FeedFormTemplate{ feed }
} else {
FeedFormTemplate{
feed: RssFeed{
id: -1,
name: "ERROR".to_string(),
feed_url: "ERROR".to_string(),
last_pub_date: Some("ERROR".to_string())
}
}
}
}
#[derive(Deserialize, AsChangeset)]
#[diesel(table_name = crate::schema::rss_feeds)]
struct PostForm{
name: String,
feed_url: String,
@@ -162,6 +198,23 @@ async fn post_feed(
res
}
async fn put_feed(
State(ctx): State<AppContext>,
Path(feed_id): Path<i32>,
Form(post): Form<PostForm>
) -> impl IntoResponse {
let conn = ctx.pool.get().await.unwrap();
use self::schema::rss_feeds::dsl::*;
let result = conn.interact(move |conn| {
diesel::update(rss_feeds.find(feed_id))
.set(post)
.returning(RssFeed::as_select())
.get_result(conn)
.expect("Error updating feed")
}).await.unwrap();
FeedTemplate{ feed: result }
}
#[derive(Template)]
#[template(source="\

View File

@@ -2,7 +2,6 @@
<html lang="en">
<head>
<link rel="stylesheet" href="https://matcha.mizu.sh/matcha.css">
<!-- <script src="https://cdn.twind.style" crossorigin></script>-->
<title>{% block title %}{{ title }}{% endblock %}</title>
<script src="https://unpkg.com/htmx.org@2.0.4"></script>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.14.8/dist/cdn.min.js"></script>

View File

@@ -6,7 +6,12 @@
<button hx-get="/feed/{{feed.id}}"
hx-target="#feed_{{feed.id}}"
hx-swap="outerHTML">
Refresh
Edit Inline
</button>
<button hx-get="/feedform/{{feed.id}}"
hx-target="body"
hx-swap="beforeend">
Edit Dialog
</button>
<button class="danger"
hx-confirm="Are you sure?"

27
templates/feed_form.html Normal file
View File

@@ -0,0 +1,27 @@
<div id="modal">
<div class="overlay" style="z-index: 30"></div>
<dialog open style="z-index: 31">
<header>
<h2>Modify RSS Feed subscription</h2>
</header>
<form id="modify_form" hx-put="/feed/{{feed.id}}" hx-target="#feed_{{feed.id}}" hx-swap="outerHTML" hx-indicator="#mod_indicator" @htmx:after-on-load="document.getElementById('modal').remove()">
<label>
Name
<input type="text" name="name" value="{{feed.name}}">
</label>
<label>
Feed URL
<input type="url" name="feed_url" value="{{feed.feed_url}}">
</label>
</form>
<footer>
<button form="modify_form" type="reset" @click="document.getElementById('modal').remove()">Close
</button>
<button form="modify_form" type="submit">
Submit
<img id="mod_indicator" class="htmx-indicator" src="/assets/oval.svg"
style="height: 1rem; margin-left: 2px" alt="...">
</button>
</footer>
</dialog>
</div>