generated from PEZ/rn-rf-shadow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.cljs
29 lines (26 loc) · 1.39 KB
/
notes.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(ns gurps.pages.character.info.widgets.notes
(:require [gurps.widgets.base :refer [view text]]
[gurps.widgets.underlined-input :refer [multiline-underlined-input]]
[gurps.utils.debounce :refer [debounce-and-dispatch]]
["twrnc" :refer [style] :rename {style tw}]
[re-frame.core :as rf]
[clojure.string :as str]))
(def ^:private n-lines 6)
(defn notes-section []
(let [notes (some-> (rf/subscribe [:profile/notes]) deref)]
[:> view {:style (tw "flex flex-col gap-1")}
[:> text {:style (tw "uppercase font-bold")} "NOTES"] ;; TODO: replace by heading widget ?
[multiline-underlined-input {:val notes
:on-change-text #(debounce-and-dispatch [:profile.notes/update, %1, %2] 500)
:n-lines n-lines}]]))
(rf/reg-event-fx
:profile.notes/update
(fn [{:keys [db]} [_ i v]]
(let [new-db (update-in db
[:profile :notes]
#(let [lines (str/split-lines %)
filled-lines (vec (concat lines (repeat (- n-lines (count lines)) "\n")))]
(str/join "\n" (assoc-in filled-lines [i] v))))]
{:db new-db
:effects.async-storage/set {:k :profile/notes
:value (get-in new-db [:profile :notes])}})))