|
2 | 2 | (:require [reagent.core :as r] |
3 | 3 | [villagebookUI.helpers :as helpers] |
4 | 4 | [villagebookUI.components.utils :as utils] |
| 5 | + [villagebookUI.fetchers :as fetchers] |
5 | 6 | [villagebookUI.api.category :as api] |
6 | 7 | [villagebookUI.store.organisations :as org-store])) |
7 | 8 |
|
8 | 9 | (defn submit-create-category |
9 | 10 | [name fields] |
10 | | - (api/create {:name name |
11 | | - :fields (map #(second %) fields)} |
12 | | - (:id (org-store/get-selected)) |
13 | | - #(helpers/show-alert-bottom! :success (str "Category " name " created")) |
14 | | - #(helpers/show-alert-bottom! :error (:response %)))) |
| 11 | + (let [org-id (:id (org-store/get-selected))] |
| 12 | + (api/create {:name name |
| 13 | + :fields (map #(second %) fields)} |
| 14 | + org-id |
| 15 | + #(do |
| 16 | + (fetchers/fetch-categories! org-id last) |
| 17 | + (helpers/show-alert-bottom! :success (str "Category " name " created"))) |
| 18 | + #(helpers/show-alert-bottom! :error (:response %))))) |
15 | 19 |
|
16 | 20 | (defn new-field-key |
17 | 21 | [fields] |
18 | 22 | (-> fields last first inc)) |
19 | 23 |
|
20 | 24 | (def new-field |
21 | | - {:name "Name"}) |
| 25 | + {:name "New column"}) |
22 | 26 |
|
23 | 27 | (defn create-category-form [] |
24 | 28 | (let [name (r/atom "") |
25 | | - fields (r/atom (array-map |
26 | | - 1 {:name "Name"} |
27 | | - 2 {:name "Cost"}))] |
| 29 | + fields (r/atom (array-map 1 new-field))] |
28 | 30 | (fn [] |
29 | 31 | [:div |
30 | | - [utils/input {:type "text" |
31 | | - :class [:create-category-name] |
32 | | - :placeholder "Category name" |
33 | | - :on-change #(reset! name %)}] |
34 | 32 | [:form.form-group |
35 | 33 | {:on-submit #(do |
36 | 34 | (.preventDefault %) |
37 | 35 | (submit-create-category @name @fields))} |
| 36 | + [utils/input {:type "text" |
| 37 | + :class [:create-category-name] |
| 38 | + :placeholder "Category name" |
| 39 | + :on-change #(reset! name %)}] |
38 | 40 | [:div.input-group.mb-3 |
39 | 41 | [:div.input-group-prepend |
40 | 42 | [:span.input-group-text "Properties"]] |
|
48 | 50 | :value (:name field) |
49 | 51 | :on-change #(swap! fields assoc k {:name %})}])) |
50 | 52 | [:div.input-group-append |
51 | | - [:button.btn.btn-outline-secondary |
| 53 | + [:a.btn.btn-outline-secondary |
52 | 54 | {:on-click #(swap! fields assoc (new-field-key @fields) new-field)} |
53 | 55 | "+"]]] |
54 | 56 | [:button {:type "submit" :class [:btn :btn-outline-primary]} "Create"]]]))) |
0 commit comments