generated from PEZ/rn-rf-shadow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreified_secondary_attribute.cljs
34 lines (31 loc) · 1.39 KB
/
reified_secondary_attribute.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
(ns gurps.pages.character.widgets.reified-secondary-attribute
(:require ["twrnc" :refer [style] :rename {style tw}]
[re-frame.core :as rf]
[gurps.utils.helpers :refer [default-to]]
[gurps.pages.character.widgets.helpers :refer [update-attribute]]
[gurps.pages.character.widgets.attribute :refer [attribute-input]]))
(def point-per-cost
{:hp 2
:will 5
:per 5
:fp 3})
(defn- calc-attr-val
[attr cost based-on-val]
(js/Math.floor
(+ (/ (default-to cost 0) (attr point-per-cost))
based-on-val)))
(defn reified-secondary-attribute
[^js {:keys [attr based-on has-current?]
:or {has-current? false}}]
(let [cost (some-> (rf/subscribe [(keyword :attribute-costs attr)]) deref)
based-on-val (or (some-> (rf/subscribe [(keyword :attributes based-on)]) deref js/parseInt) 10)
val (calc-attr-val attr cost based-on-val)
current (when has-current? (or (some-> (rf/subscribe [(keyword :attribute-current attr)]) deref) val))
on-change-text (partial update-attribute (keyword :attribute-costs attr))]
[attribute-input {:attr attr
:cost cost
:val val
:secondary? true
:on-change-text on-change-text
:current current
:style (tw "justify-end")}]))