Skip to content

Commit

Permalink
Generate PTV summary/description from edit form state
Browse files Browse the repository at this point in the history
  • Loading branch information
Deraen committed Nov 12, 2024
1 parent 012a397 commit c69b400
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 22 deletions.
7 changes: 7 additions & 0 deletions webapp/src/clj/lipas/backend/ptv/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
:message
:content)))

(defn generate-ptv-descriptions-from-data
[doc]
(let [doc (core/enrich doc)]
(-> (ai/generate-ptv-descriptions doc)
:message
:content)))

(defn make-overview
[sites]
{:city-name (->> sites first :search-meta :location :city :name)
Expand Down
14 changes: 13 additions & 1 deletion webapp/src/clj/lipas/backend/ptv/handler.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns lipas.backend.ptv.handler
(:require [lipas.backend.ptv.core :as ptv-core]
[reitit.coercion.malli]))
[reitit.coercion.malli]
[reitit.coercion.spec]))

(defn localized-string-schema [string-props]
[:map
Expand Down Expand Up @@ -55,6 +56,17 @@
search
(-> req :parameters :body :lipas-id))})}}]

["/actions/generate-ptv-descriptions-from-data"
{:post
{:require-privilege :ptv/manage
:coercion reitit.coercion.spec/coercion
:parameters {:body :lipas/new-or-existing-sports-site}
:handler
(fn [req]
{:status 200
:body (ptv-core/generate-ptv-descriptions-from-data
(-> req :parameters :body))})}}]

["/actions/generate-ptv-service-descriptions"
{:post
{:require-privilege :ptv/manage
Expand Down
4 changes: 1 addition & 3 deletions webapp/src/clj/lipas/backend/ptv/integration.clj
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,7 @@
{:bool
{:must
(remove nil?
[;; Include all statuses - this is also used to remove the sites from PTV
; {:terms {:status.keyword ["active"
; "out-of-service-temporarily"]}}
[{:terms {:status.keyword ["active" "out-of-service-temporarily"]}}
(when city-codes
{:terms {:location.city.city-code city-codes}})
(when owners
Expand Down
30 changes: 30 additions & 0 deletions webapp/src/cljs/lipas/ui/ptv/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,36 @@
:fx (or extra-fx
[[:dispatch [:lipas.ui.events/set-active-notification notification]]])})))

(rf/reg-event-fx ::generate-descriptions-from-data
(fn [{:keys [db]} [_ lipas-id]]
(let [token (-> db :user :login :token)
edit-data (-> db :sports-sites (get lipas-id) :editing)]
{:db (assoc-in db [:ptv :loading-from-lipas :descriptions] true)
:fx [[:http-xhrio
{:method :post
:headers {:Authorization (str "Token " token)}
:uri (str (:backend-url db) "/actions/generate-ptv-descriptions-from-data")
:params (utils/make-saveable edit-data)
:format (ajax/transit-request-format)
:response-format (ajax/transit-response-format)
:on-success [::generate-descriptions-from-data-success lipas-id]
:on-failure [::generate-descriptions-from-data-failure lipas-id]}]]})))

(rf/reg-event-fx ::generate-descriptions-from-data-success
(fn [{:keys [db]} [_ lipas-id resp]]
{:db (-> db
(assoc-in [:ptv :loading-from-lipas :descriptions] false)
(update-in [:sports-sites lipas-id :editing :ptv] merge resp))}))

(rf/reg-event-fx ::generate-descriptions-from-data-failure
(fn [{:keys [db]} [_]]
(let [tr (:translator db)
notification {:message (tr :notifications/get-failed)
:success? false}]
{:db (-> db
(assoc-in [:ptv :loading-from-lipas :descriptions] false))
:fx [[:dispatch [:lipas.ui.events/set-active-notification notification]]]})))

(rf/reg-event-db ::toggle-sync-all
(fn [db [_ enabled]]
(let [org-id (get-in db [:ptv :selected-org :id])]
Expand Down
41 changes: 23 additions & 18 deletions webapp/src/cljs/lipas/ui/ptv/site_view.cljs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
(ns lipas.ui.ptv.site-view
(:require ["@mui/material/Alert$default" :as Alert]
["@mui/material/Button$default" :as Button]
["@mui/material/CircularProgress$default" :as CircularProgress]
["@mui/material/FormControl$default" :as FormControl]
["@mui/material/FormLabel$default" :as FormLabel]
["@mui/material/Stack$default" :as Stack]
["@mui/material/Typography$default" :as Typography]
[lipas.ui.components :as lui]
[lipas.ui.ptv.controls :as controls]
[lipas.ui.ptv.events :as events]
[lipas.ui.ptv.subs :as subs]
[lipas.ui.uix.hooks :refer [use-subscribe]]
[re-frame.core :as rf]
[reagent.core :as r]
Expand All @@ -20,11 +22,12 @@
editing? (and can-edit? editing*)
read-only? (not editing?)
site (use-subscribe [:lipas.ui.sports-sites.subs/latest-rev lipas-id])

;; default-settings {}
enabled (boolean (:ptv site))
descriptions-enabled (not= "manual" (:descriptions-integration (:ptv site)))

loading? false]
loading? (use-subscribe [::subs/generating-descriptions?])]
($ Stack
{:direction "column"
:sx #js {:gap 2}}
Expand Down Expand Up @@ -55,12 +58,24 @@
:tr tr})

(when (= "lipas-managed-ptv-fields" (:descriptions-integration (:ptv site)))
($ Button
{:disabled loading?
:variant "outlined"
:on-click (fn [_e]
(rf/dispatch [::events/generate-descriptions (:lipas-id site) [] []]))}
(tr :ptv.actions/generate-with-ai)))
($ Stack
{:sx #js {:position "relative"}}
($ Button
{:disabled (or loading?
read-only?)
:variant "outlined"
;; NOTE: Could use the lipas-id version when not editing? But then we don't have
;; place to store the results.
:on-click (fn [_e]
(rf/dispatch [::events/generate-descriptions-from-data lipas-id]))}
(tr :ptv.actions/generate-with-ai))
(when loading?
($ CircularProgress
{:size 24
:sx #js {:position "absolute"
:top "50%"
:left "50%"
:mt "-12px"}}))))

($ controls/lang-selector
{:value selected-tab
Expand Down Expand Up @@ -93,14 +108,4 @@
(rf/dispatch [:lipas.ui.sports-sites.events/edit-field lipas-id [:ptv :description selected-tab] v]))
:label "Kuvaus"
:value (or (get-in edit-data [:ptv :description selected-tab])
(get-in site [:ptv :description selected-tab]))}])

#_
($ Button
{:variant "contained"
:color "primary"
:disabled (or (not can-edit?)
loading?)
:on-click (fn [_e]
(rf/dispatch [::events/create-ptv-service-location* lipas-id [] []]))}
"Vie PTV"))))
(get-in site [:ptv :description selected-tab]))}]))))
2 changes: 2 additions & 0 deletions webapp/test/clj/lipas/backend/ptv_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
(let [updated-site (assoc site :ptv (merge default-settings
{:sync-enabled true
:org-id org-id
;; TODO: Need to setup the link to services
:service-ids []
:summary {:fi "foobar"
:se "foobar"
:en "foobar"}
Expand Down

0 comments on commit c69b400

Please sign in to comment.