generated from PEZ/rn-rf-shadow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint_total.cljs
49 lines (44 loc) · 1.76 KB
/
point_total.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(ns gurps.pages.character.widgets.point-total
(:require ["twrnc" :refer [style] :rename {style tw}]
[re-frame.core :as rf]
[gurps.utils.helpers :refer [->int]]
[gurps.pages.character.widgets.summary-field :refer [summary-field]]))
(defn point-total-field
[]
[summary-field {:key :point-total,
:text-align "right",
:editable? false,
:on-change-text ->int,
:input-mode "numeric",
:max-length 4,
:input-style (tw "w-16")}])
(rf/reg-sub
:profile/point-total
(fn [db]
(let [costs (+ (->> db :attribute-costs vals (reduce +))
(->> db :skills (map :cost) (reduce +))
(->> db :spell-costs vals (map :cost) (reduce +))
(->> db :advantages vals (map :cost) (reduce +))
(->> db :disadvantages vals (map :cost) (reduce +))
(->> db :languages (filter #(not (:native? %))) (map :cost) (reduce +)))]
(or costs 0))))
(defn unspent-points-field
[{:keys [style]}]
[summary-field {:key :unspent-points,
:on-change-text ->int,
:style style,
:input-style (tw "w-16 bg-slate-100"),
:input-mode "numeric",
:max-length 4,
:text-align "right"}])
(rf/reg-sub
:profile/unspent-points
(fn [db]
(get-in db [:profile :unspent-points] 0)))
(rf/reg-event-fx
:profile.update/unspent-points
(fn [{:keys [db]} [_ diff]]
(let [new-db (update-in db [:profile :unspent-points] #(- % diff))]
{:db new-db
:effects.async-storage/set {:k :profile/unspent-points
:value (get-in new-db [:profile :unspent-points])}})))