From f99623d1cb85f7f3d604a07a7349f136f99336ac Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sun, 29 May 2022 22:44:51 -0400 Subject: [PATCH] Add / update / delete custom feed complete --- src/MyWebLog.Domain/ViewModels.fs | 38 ++++- src/MyWebLog/Handlers/Feed.fs | 26 ++- .../themes/admin/custom-feed-edit.liquid | 150 ++++++++++-------- src/MyWebLog/themes/admin/rss-settings.liquid | 4 +- 4 files changed, 147 insertions(+), 71 deletions(-) diff --git a/src/MyWebLog.Domain/ViewModels.fs b/src/MyWebLog.Domain/ViewModels.fs index 0cde291..d72d807 100644 --- a/src/MyWebLog.Domain/ViewModels.fs +++ b/src/MyWebLog.Domain/ViewModels.fs @@ -3,6 +3,15 @@ open System open MyWebLog +/// Helper functions for view models +[] +module private Helpers = + + /// Create a string option if a string is blank + let noneIfBlank (it : string) = + match it.Trim () with "" -> None | trimmed -> Some trimmed + + /// Details about a category, used to display category lists [] type DisplayCategory = @@ -259,6 +268,7 @@ type EditCustomFeedModel = summary = p.summary displayedAuthor = p.displayedAuthor email = p.email + imageUrl = Permalink.toString p.imageUrl itunesCategory = p.iTunesCategory itunesSubcategory = defaultArg p.iTunesSubcategory "" explicit = ExplicitRating.toString p.explicit @@ -266,7 +276,31 @@ type EditCustomFeedModel = mediaBaseUrl = defaultArg p.mediaBaseUrl "" } | None -> rss - + + /// Update a feed with values from this model + member this.updateFeed (feed : CustomFeed) = + { feed with + source = if this.sourceType = "tag" then Tag this.sourceValue else Category (CategoryId this.sourceValue) + path = Permalink this.path + podcast = + if this.isPodcast then + Some { + title = this.title + subtitle = noneIfBlank this.subtitle + itemsInFeed = this.itemsInFeed + summary = this.summary + displayedAuthor = this.displayedAuthor + email = this.email + imageUrl = Permalink this.imageUrl + iTunesCategory = this.itunesCategory + iTunesSubcategory = noneIfBlank this.itunesSubcategory + explicit = ExplicitRating.parse this.explicit + defaultMediaType = noneIfBlank this.defaultMediaType + mediaBaseUrl = noneIfBlank this.mediaBaseUrl + } + else + None + } /// View model to edit a page [] @@ -427,7 +461,7 @@ type EditRssModel = itemsInFeed = if this.itemsInFeed = 0 then None else Some this.itemsInFeed categoryEnabled = this.categoryEnabled tagEnabled = this.tagEnabled - copyright = if this.copyright.Trim () = "" then None else Some (this.copyright.Trim ()) + copyright = noneIfBlank this.copyright } diff --git a/src/MyWebLog/Handlers/Feed.fs b/src/MyWebLog/Handlers/Feed.fs index 03a87e6..dc3df97 100644 --- a/src/MyWebLog/Handlers/Feed.fs +++ b/src/MyWebLog/Handlers/Feed.fs @@ -316,7 +316,7 @@ let saveSettings : HttpHandler = fun next ctx -> task { let editCustomFeed feedId : HttpHandler = fun next ctx -> task { let customFeed = match feedId with - | "new" -> Some CustomFeed.empty + | "new" -> Some { CustomFeed.empty with id = CustomFeedId "new" } | _ -> ctx.WebLog.rss.customFeeds |> List.tryFind (fun f -> f.id = CustomFeedId feedId) match customFeed with | Some f -> @@ -332,8 +332,28 @@ let editCustomFeed feedId : HttpHandler = fun next ctx -> task { // POST: /admin/rss/save let saveCustomFeed : HttpHandler = fun next ctx -> task { - // TODO: stub - return! Error.notFound next ctx + let conn = ctx.Conn + match! Data.WebLog.findById ctx.WebLog.id conn with + | Some webLog -> + let! model = ctx.BindFormAsync () + let theFeed = + match model.id with + | "new" -> Some { CustomFeed.empty with id = CustomFeedId.create () } + | _ -> webLog.rss.customFeeds |> List.tryFind (fun it -> CustomFeedId.toString it.id = model.id) + match theFeed with + | Some feed -> + let feeds = model.updateFeed feed :: (webLog.rss.customFeeds |> List.filter (fun it -> it.id <> feed.id)) + let webLog = { webLog with rss = { webLog.rss with customFeeds = feeds } } + do! Data.WebLog.updateRssOptions webLog conn + WebLogCache.set webLog + do! addMessage ctx { + UserMessage.success with + message = $"""Successfully {if model.id = "new" then "add" else "sav"}ed custom feed""" + } + let nextUrl = $"admin/settings/rss/{CustomFeedId.toString feed.id}/edit" + return! redirectToGet (WebLog.relativeUrl webLog (Permalink nextUrl)) next ctx + | None -> return! Error.notFound next ctx + | None -> return! Error.notFound next ctx } // POST /admin/rss/{id}/delete diff --git a/src/MyWebLog/themes/admin/custom-feed-edit.liquid b/src/MyWebLog/themes/admin/custom-feed-edit.liquid index e74794a..c20c29f 100644 --- a/src/MyWebLog/themes/admin/custom-feed-edit.liquid +++ b/src/MyWebLog/themes/admin/custom-feed-edit.liquid @@ -2,28 +2,59 @@
- {%- assign is_cat = model.source_type == "category" -%} + + {%- assign typ = model.source_type -%}
- +
+
+
+ Identification +
+
+
+ + + Appended to {{ web_log.url_base }}/ +
+
+
+
+
+
+ + +
+
+
+
+
+
Feed Source
+ {%- unless typ == "tag" %} checked="checked" {% endunless -%} + onclick="Admin.customFeedBy('category')">
-
+
+ {%- if typ == "tag" %} checked="checked"{% endif %} onclick="Admin.customFeedBy('tag')">
-
+
+ {%- unless typ == "tag" %} disabled="disabled"{% endunless %} required + {%- if typ == "tag" %} value="{{ model.source_value }}"{% endif %}>
@@ -50,70 +81,26 @@
-
-
-
- - - Appended to {{ web_log.url_base }}/ -
-
-
-
- - -
-
-
Podcast Settings
-
+
-
+
-
-
-
-
- - - Displayed in podcast directories -
-
-
-
-
-
- - -
-
-
-
- - - For iTunes, must match registered e-mail -
-
-
+
@@ -122,7 +109,7 @@
-
+
@@ -135,16 +122,14 @@
-
+
-
-
-
+
+ +
+
+
+
+ + + For iTunes, must match registered e-mail +
+
+
@@ -164,7 +166,27 @@ Optional; blank for no default
-
+
+
+ + + Relative URL will be appended to {{ web_log.url_base }}/ +
+
+
+
+
+
+ + + Displayed in podcast directories +
+
+
+
+
diff --git a/src/MyWebLog/themes/admin/rss-settings.liquid b/src/MyWebLog/themes/admin/rss-settings.liquid index 41b2ec4..6bb7a99 100644 --- a/src/MyWebLog/themes/admin/rss-settings.liquid +++ b/src/MyWebLog/themes/admin/rss-settings.liquid @@ -85,10 +85,10 @@ View Feed - {%- capture feed_edit %}admin/rss/{{ feed.id }}/edit{% endcapture -%} + {%- capture feed_edit %}admin/settings/rss/{{ feed.id }}/edit{% endcapture -%} Edit - {%- capture feed_del %}admin/rss/{{ feed.id }}/delete{% endcapture -%} + {%- capture feed_del %}admin/settings/rss/{{ feed.id }}/delete{% endcapture -%} {%- capture feed_del_link %}{{ feed_del | relative_link }}{% endcapture -%}