Possibly-working?

This commit is contained in:
Simon Brooke 2019-02-17 15:19:24 +00:00
parent 8771b905d4
commit 0d3efe1323
6 changed files with 99 additions and 15 deletions

4
.gitignore vendored
View file

@ -12,3 +12,7 @@ profiles.clj
/.env
.nrepl-port
/log
\.rebel_readline_history
[0-9a-f]*-init\.clj

View file

@ -4,6 +4,8 @@
:url "http://example.com/FIXME"
:dependencies [[baking-soda "0.2.0" :exclusions [cljsjs/react-bootstrap]]
[binaryage/devtools "0.9.10"]
[com.cemerick/url "0.1.1"]
[cheshire "5.8.1"]
[clj-commons/secretary "1.2.4"]
[clj-oauth "1.5.5"]

View file

@ -7,14 +7,14 @@
<link href="css/standard.css" rel="stylesheet" type="text/css">
</head>
<body>
<form action="http://loriner.journeyman.cc:8888/v1/tesseract/" method="post">
<form action="http://loriner.journeyman.cc:8888/v1/tesseract/" method="POST">
<p class="widget">
<label for="image-url" title="URL of the image you wish to transcribe">Image URL</label>
<input id="image-url" type="text"/>
<input id="image-url" type="text" size="80"/>
</p>
<p class="widget">
<label for="submit">{% i18n change-pass-prompt %}</label>
<input name="action" id="action" type="submit" class="action" value="{% i18n change-pass-link %}!"/>
<label for="submit">To transcribe the text of this image</label>
<input name="action" id="action" type="submit" class="action" value="Transcribe!"/>
</p>
</form>
</body>

View file

@ -8,6 +8,7 @@
[markdown.core :refer [md->html]]
[ireadit.ajax :as ajax]
[ireadit.events]
[ireadit.views.form :as form]
[secretary.core :as secretary])
(:import goog.History))
@ -34,7 +35,8 @@
[b/Collapse {:is-open @expanded? :navbar true}
[b/Nav {:class-name "mr-auto" :navbar true}
[nav-link "#/" "Home" :home]
[nav-link "#/about" "About" :about]]]]))
[nav-link "#/about" "About" :about]
[nav-link "#/transcribe" "Transcribe" :transcribe]]]]))
(defn about-page []
[:div.container
@ -51,7 +53,8 @@
(def pages
{:home #'home-page
:about #'about-page})
:about #'about-page
:transcribe #'form/form-page})
(defn page []
[:div
@ -69,6 +72,9 @@
(secretary/defroute "/about" []
(rf/dispatch [:navigate :about]))
(secretary/defroute "/transcribe" []
(rf/dispatch [:navigate :transcribe]))
;; -------------------------
;; History
;; must be called after routes have been defined

View file

@ -1,9 +1,15 @@
(ns ireadit.events
(:require [re-frame.core :as rf]
[ajax.core :as ajax]))
[ajax.core :as ajax]
[cemerick.url :refer (url-encode)]))
;;dispatchers
(rf/reg-event-db
:set-url
(fn [db [_ url]]
(assoc db :url url)))
(rf/reg-event-db
:navigate
(fn [db [_ page]]
@ -14,6 +20,11 @@
(fn [db [_ docs]]
(assoc db :docs docs)))
(rf/reg-event-db
:set-transcription
(fn [db [_ transcription]]
(assoc db :transcription transcription)))
(rf/reg-event-fx
:fetch-docs
(fn [_ _]
@ -22,6 +33,27 @@
:response-format (ajax/raw-response-format)
:on-success [:set-docs]}}))
(rf/reg-event-fx
:fetch-transcription
(fn [{db :db} _]
(let [uri (str "http://loriner.journeyman.cc:8888/v1/tesseract/" (url-encode (:url db)))]
(js/console.log
(str
"Fetching transcription data: " uri))
{:http-xhrio {:method :post
:uri uri
:format (ajax/json-request-format)
:response-format (ajax/json-response-format {:keywords? true})
:on-success [:set-transcription]
:on-failure [:bad-transcription]}})))
(rf/reg-event-fx
:bad-transcription
(fn
[{db :db} [_ response]]
;; TODO: signal something has failed? It doesn't matter very much, unless it keeps failing.
(js/console.log (str "Failed to fetch transcription data" response))))
(rf/reg-event-db
:common/set-error
(fn [db [_ error]]
@ -43,3 +75,14 @@
:common/error
(fn [db _]
(:common/error db)))
(rf/reg-sub
:url
(fn [db _]
(:url db)))
(rf/reg-sub
:transcription
(fn [db _]
(:transcription db)))

View file

@ -0,0 +1,29 @@
(ns ireadit.views.form
(:require [baking-soda.core :as b]
[day8.re-frame.http-fx]
[reagent.core :as r]
[re-frame.core :as rf]
[goog.events :as events]
[goog.history.EventType :as HistoryEventType]
[markdown.core :refer [md->html]]
[ireadit.ajax :as ajax]
[ireadit.events]
[secretary.core :as secretary])
(:import goog.History))
(defn form-page []
[:div
[:h1 "Transcribe the text of an image"]
[:div.container {:id "main-container"}
[:div
[:p.widget
[b/Label {:for "image-url" :title "URL of the image you wish to transcribe"}"Image URL"]
[b/Input {:id "image-url" :type "text" :size "80"
:on-change #(rf/dispatch [:set-url (.-value (.-target %))])}]]
[:p.widget
[b/Label {:for "send"} "To transcribe the image"]
[b/Button {:id "send" :on-click #(rf/dispatch [:fetch-transcription])} "Transcribe!"]]]
[:div.transcription @(rf/subscribe [:transcription])]
]])