|
1 | 1 | (ns asva.assessments |
2 | 2 | (:require |
3 | | - [asva.components :refer [highlight progress-indicator]] |
| 3 | + [asva.components :refer [highlight progress-indicator recurrence-input]] |
4 | 4 | [asva.i18n :refer [t]] |
5 | 5 | [asva.icons :as icons] |
6 | | - [asva.utils :refer [<> <sub dispatch>e e> parse-int slug slug-key]] |
| 6 | + [asva.utils :refer [<> <sub dispatch>e e> parse-int slug slug-key now]] |
7 | 7 | [clojure.edn :as edn] |
8 | 8 | [clojure.string :as str] |
9 | 9 | [clojure.walk :refer [postwalk]] |
|
75 | 75 | (re-frame/reg-event-fx |
76 | 76 | ::notes |
77 | 77 | (fn [{:keys [db]} [_ shortcode notes]] |
78 | | - (let [updated-db (assoc-in db [::assessments (slug-key shortcode) :notes] notes)] |
| 78 | + (let [last-modified (now) |
| 79 | + updated-db (update-in db [::assessments (slug-key shortcode)] |
| 80 | + (fn [assessment] |
| 81 | + (merge assessment |
| 82 | + {:asvs-version (-> db ::asvs :version) |
| 83 | + :notes notes |
| 84 | + :last-modified last-modified})))] |
79 | 85 | {:db updated-db |
80 | 86 | :local-storage [::assessments (get updated-db ::assessments)]}))) |
81 | 87 |
|
82 | 88 | (re-frame/reg-event-fx |
83 | 89 | ::progress |
84 | 90 | (fn [{:keys [db]} [_ shortcode progress]] |
85 | 91 | (let [progress (max 0 (min 100 progress)) |
86 | | - updated-db (assoc-in db [::assessments (slug-key shortcode) :progress] progress)] |
| 92 | + last-modified (now) |
| 93 | + updated-db (update-in db [::assessments (slug-key shortcode)] |
| 94 | + (fn [assessment] |
| 95 | + (merge assessment |
| 96 | + {:asvs-version (-> db ::asvs :version) |
| 97 | + :progress progress |
| 98 | + :last-modified last-modified})))] |
87 | 99 | {:db updated-db |
88 | 100 | :local-storage [::assessments (get updated-db ::assessments)]}))) |
89 | 101 |
|
90 | 102 | (re-frame/reg-event-fx |
91 | 103 | ::not-applicable |
92 | 104 | (fn [{:keys [db]} [_ shortcode not-applicable]] |
93 | | - (let [updated-db (assoc-in db [::assessments (slug-key shortcode) :not-applicable] not-applicable)] |
| 105 | + (let [last-modified (now) |
| 106 | + updated-db (update-in db [::assessments (slug-key shortcode)] |
| 107 | + (fn [assessment] |
| 108 | + (merge assessment |
| 109 | + {:asvs-version (-> db ::asvs :version) |
| 110 | + :not-applicable not-applicable |
| 111 | + :last-modified last-modified})))] |
| 112 | + {:db updated-db |
| 113 | + :local-storage [::assessments (get updated-db ::assessments)]}))) |
| 114 | + |
| 115 | +(re-frame/reg-event-fx |
| 116 | + ::recurrence |
| 117 | + (fn [{:keys [db]} [_ shortcode recurrence]] |
| 118 | + (let [updated-db (update-in db [::assessments (slug-key shortcode)] |
| 119 | + (fn [assessment] |
| 120 | + (merge assessment |
| 121 | + {:asvs-version (-> db ::asvs :version) |
| 122 | + :last-modified (now) |
| 123 | + :recurring recurrence})))] |
94 | 124 | {:db updated-db |
95 | 125 | :local-storage [::assessments (get updated-db ::assessments)]}))) |
96 | 126 |
|
|
184 | 214 | (group-by :chapter))] |
185 | 215 | (sort-by key items)))) |
186 | 216 |
|
187 | | -(defn assessment [{:keys [shortcode description progress not-applicable notes cwe nist last-updated]}] |
| 217 | +(defn assessment [{:keys [shortcode description progress not-applicable notes cwe nist last-modified asvs-version]}] |
188 | 218 | (let [query (re-frame/subscribe [::query]) |
| 219 | + recurrence-input? (reagent/atom false) |
189 | 220 | editing? (reagent/atom false) |
190 | 221 | note (reagent/atom notes)] |
191 | | - (fn [{:keys [shortcode description progress not-applicable notes cwe nist last-updated] :as tmp}] |
| 222 | + (fn [{:keys [shortcode description progress not-applicable notes cwe nist last-modified asvs-version]}] |
192 | 223 | (let [not-applicable (boolean not-applicable) |
193 | 224 | url (str (-> js/location .-host) "/#" shortcode) |
194 | 225 | cwe-url (fn [n] (str "https://cwe.mitre.org/data/definitions/" n ".html")) |
|
209 | 240 | :title (t :not-applicable) |
210 | 241 | :on-change (dispatch>e [::not-applicable shortcode (not not-applicable)])}] |
211 | 242 | [:label {:for (str shortcode "-na")} "n/a"]]] |
212 | | - (when last-updated [:small.last-updated.badge (t :last-updated last-updated)]) |
| 243 | + (when last-modified |
| 244 | + [:small.last-modified.badge {:title (t :registered-version asvs-version (js/Date. last-modified))} |
| 245 | + (t :last-modified (js/Date. last-modified))]) |
213 | 246 | [:p.description (highlight description @query)] |
214 | 247 | [:div.horizontal |
215 | 248 | (when (some? cwe) [:a.badge.warning {:target :_blank :title :cwe :rel :noopener :href (cwe-url cwe)} cwe]) |
216 | 249 | (when (some? nist) [:a.badge {:target :_blank :title :nist :rel :noopener :href (nist-url nist)} nist])] |
| 250 | + [:menu |
| 251 | + (when-not @editing? |
| 252 | + [:button {:title (t :assessment-notes) :on-click (e> (reset! editing? true))} [icons/note]]) |
| 253 | + [:div |
| 254 | + [:button {:title (t :recurring) :on-click #(swap! recurrence-input? not)} [icons/recurring]]] |
| 255 | + [recurrence-input {:class [(when-not @recurrence-input? :hide)] :on-change #(re-frame/dispatch [::recurrence shortcode %])}]] |
217 | 256 | [:div.Markdown |
218 | 257 | (if @editing? |
219 | 258 | [:<> |
220 | 259 | [:textarea {:auto-focus true :placeholder (t :assessment-notes) :value @note :on-change (e> (reset! note value))}] |
221 | 260 | [:a {:href "https://www.markdownguide.org/basic-syntax/" :target :_blank :rel :noopener} |
222 | | - [icons/markdown {:title (t :markdown-support)}]]] |
| 261 | + [icons/markdown {:title (t :markdown-support)}]] |
| 262 | + [:button.primary {:on-click (dispatch>e (do (reset! editing? false) [::notes shortcode @note]))} "Save"]] |
223 | 263 | [markdown->hiccup notes])] |
224 | | - (if @editing? |
225 | | - [:button.primary {:on-click (dispatch>e (do (reset! editing? false) [::notes shortcode @note]))} "Save"] |
226 | | - [:button {:title (t :assessment-notes) :on-click (e> (reset! editing? true))} [icons/note]]) |
227 | 264 | [progress-indicator {:width 40 |
228 | 265 | :on-progress (fn [prog] (re-frame/dispatch [::progress shortcode prog]))} progress]])))) |
229 | 266 |
|
|
0 commit comments