 
 (defn hyphenation-pattern-item-ui [{:keys [hyphenation] :as pattern}]
   (let [editing (reagent/atom false)
-        new-hyphenation (reagent/atom hyphenation)]
+        new-hyphenation (reagent/atom hyphenation)
+        new-suggestion (reagent/atom "")]
     (fn [{:keys [word hyphenation] :as pattern}]
       (let [stop #(do (reset! new-hyphenation hyphenation)
                       (reset! editing false))
-            start #(reset! editing true)
+            start #(do (reset! editing true)
+                       (lookup-hyphenation-pattern!
+                        @spelling word
+                        (fn [hyphenation] (reset! new-suggestion hyphenation))))
             save #(do (when (hyphenation-valid? @new-hyphenation word)
                         (add-hyphenation-pattern! (assoc pattern :hyphenation @new-hyphenation)))
                       (reset! editing false))
             remove #(remove-hyphenation-pattern! pattern)]
         (if-not @editing
           [hyphenation-pattern-readonly-ui word hyphenation start remove]
-          [hyphenation-pattern-edit-ui word new-hyphenation stop save])))))
+          [hyphenation-pattern-edit-ui word new-hyphenation new-suggestion stop save])))))
 
 (defn word-ui []
   (let [label "Word"

      [:span.glyphicon.glyphicon-trash {:aria-hidden true}] " Delete"]]]])

(defn- hyphenation-pattern-edit-ui
  [word new-hyphenation stop save]
  (let [valid? (hyphenation-valid? @new-hyphenation word)
        klass (when-not valid? "has-error")
        help-text (when-not valid? "The hyphenation is not valid")]
    [:tr
     [:td word]
     [:td

   :format :json))

(defn lookup-hyphenation-pattern!
  "Fetch the hyphenation pattern from the server for given `spelling`
  and `word`. If a `handler` is given invoke it on success. Otherwise
  use a default handler which updates the `app-state` in
  `:suggested-hyphenation` and `:hyphenation`."
  ([spelling word]
   (let [handler (fn [hyphenated]
                   (do
                     (swap! app-state assoc :suggested-hyphenation hyphenated)
                     (swap! app-state assoc :hyphenation hyphenated)))]
     (lookup-hyphenation-pattern! spelling word handler)))
  ([spelling word handler]
   (ajax/GET "/api/hyphenate"
             :params {:word word :spelling spelling}
             :handler handler
             :error-handler (fn [details]
                              (.warn js/console
                                     (str "Failed to lookup hyphenation patterns for word: " details))))))

(defn hyphenation-pattern-ui [{:keys [word hyphenation]}]
  [:tr
