Skip to content

Commit

Permalink
Merge pull request #544 from instedd/template-selector
Browse files Browse the repository at this point in the history
Template selector
  • Loading branch information
Hugo David Farji authored Jun 10, 2019
2 parents aef047a + d58fe86 commit a8d74da
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 15 deletions.
4 changes: 2 additions & 2 deletions resources/planwise/sql/projects2.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- :name db-create-project! :<! :1
INSERT INTO projects2
("owner-id", name, config, "provider-set-id", state)
VALUES (:owner-id, :name, NULL, NULL, :state)
("owner-id", name, config, "region-id", "provider-set-id", "source-set-id", state)
VALUES (:owner-id, :name, :config, :region-id, :provider-set-id, :source-set-id, :state)
RETURNING id;

-- :name db-update-project :!
Expand Down
55 changes: 55 additions & 0 deletions resources/sass/site2.scss
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,61 @@ form.vertical, .fields-vertical {
padding: 10px;
}

.template-container {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
border-radius: 4px;
background-color: #FFFFFF;
box-shadow: 0 1px 2px 0 rgba(0,0,0,0.2), 0 1px 3px 0 rgba(0,0,0,0.1);
flex-direction: column;
padding: 60px;

h2 {
color: #999999;
font-size: 1rem;
text-transform: uppercase;
margin-bottom: 45px;
}

i {
font-size: 48px;
color: #FF5722;
display: block;
padding: 15px;
border: 1px solid rgba(0,0,0,0.1);
border-radius: 48px;
height: 48px;
width: 48px
}

.action {
display: flex;
flex-direction: column;
align-items: center;
width: 20%;
cursor: pointer;

div {
text-align: center;
margin: 1rem 0;
}
}

.row {
display: flex;
justify-content: space-around;
width: 100%;
}

hr {
border: 1px solid rgba(0,0,0,0.1);
width: calc(100% + 118px);
margin: 60px 0;
}
}

.loader {
position: absolute;
left: 50%;
Expand Down
9 changes: 8 additions & 1 deletion src/planwise/client/projects2/api.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
;; API methods

(defn- create-project!
[]
[defaults]
{:method :post
:section :index
:params {:project defaults}
:uri "/api/projects2"})

(defn- list-projects
Expand All @@ -15,6 +16,12 @@
:section :index
:uri (str "/api/projects2")})

(defn- list-templates
[]
{:method :get
:section :index
:uri (str "/api/projects2/templates")})

(defn- update-project
[project-id project]
{:method :put
Expand Down
45 changes: 45 additions & 0 deletions src/planwise/client/projects2/components/create.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(ns planwise.client.projects2.components.create
(:require [re-frame.core :refer [subscribe dispatch] :as rf]
[planwise.client.asdf :as asdf]
[reagent.core :as r]
[re-com.core :as rc]
[planwise.client.components.common2 :as common2]
[planwise.client.routes :as routes]
[planwise.client.utils :as utils]
[planwise.client.ui.common :as ui]
[planwise.client.ui.rmwc :as m]
[planwise.client.mapping :refer [static-image fullmap-region-geo]]
[planwise.client.components.common :as common]))
(def project-templates
[{:description "Plan facilities based on ground access"
:icon "directions_walk"
:key "plan"
:defaults {:name "ground"}}
{:description "Plan diagonostic devices & sample referrals"
:icon "call_split"
:key "diagnosis"
:defaults {:name "sample"}}])

(defn project-section-template-selector
[]
[ui/fixed-width (common2/nav-params)
(let [templates (subscribe [:projects2/templates])
scratch-template (first (filter #(not (contains? % :description)) @templates))
sample-templates (filter #(contains? % :description) @templates)]
(dispatch [:projects2/get-templates-list])
[:div.template-container
(if (some? sample-templates)
[:h2 "Start from a template"])
(if (some? sample-templates)
[:div.row
(map (fn [template]
[:a.action {:key (:key template) :onClick #(dispatch [:projects2/new-project (:defaults template)])}
[m/Icon {} (:icon template)]
[:div (:description template)]])
sample-templates)])
[:hr]
[:h2 "Start from scratch"]
[:div.row
[:a.action {:onClick #(dispatch [:projects2/new-project (:defaults scratch-template)])}
[m/Icon {} "folder_open"]
[:div "Follow a wizard through all available settings"]]]])])
2 changes: 1 addition & 1 deletion src/planwise/client/projects2/components/listings.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
[projects-list @projects])))

(defn project-section-index []
(let [create-project-button (ui/main-action {:icon "add" :on-click #(dispatch [:projects2/new-project])})]
(let [create-project-button (ui/main-action {:icon "add" :on-click #(dispatch [:projects2/template-project])})]
[ui/fixed-width (merge {:action create-project-button} (common2/nav-params))
[listing-component]]))
24 changes: 22 additions & 2 deletions src/planwise/client/projects2/handlers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,30 @@
(rf/reg-event-fx
:projects2/new-project
in-projects2
(fn [_ [_]]
{:api (assoc (api/create-project!)
(fn [_ [_ defaults]]
{:api (assoc (api/create-project! defaults)
:on-success [:projects2/project-created])}))

(rf/reg-event-fx
:projects2/template-project
in-projects2
(fn [_ [_]]
{:navigate (routes/projects2-new {})}))

(rf/reg-event-fx
:projects2/get-templates-list
in-projects2
(fn [_ [_]]
{:api (assoc (api/list-templates) :on-success [:projects2/templates-fetched])}))
; {:api (assoc (api/create-project!)
; :on-success [:projects2/project-created])}))
(rf/reg-event-fx
:projects2/templates-fetched
in-projects2
(fn [{:keys [db]} [_ templates]]
{:db (-> db
(assoc :templates templates))}))

(rf/reg-event-fx
:projects2/project-created
in-projects2
Expand Down
6 changes: 5 additions & 1 deletion src/planwise/client/projects2/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
(fn [db _]
(get-in db [:projects2 :current-project])))

(rf/reg-sub
:projects2/templates
(fn [db _]
(get-in db [:projects2 :templates])))

(rf/reg-sub
:projects2/list
(fn [db _]
Expand All @@ -29,4 +34,3 @@
:projects2/upgrade-actions :<- [:projects2/current-project]
(fn [current-project [_]]
(get-in current-project [:config :actions :upgrade])))

2 changes: 2 additions & 0 deletions src/planwise/client/projects2/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[planwise.client.projects2.components.dashboard :as dashboard]
[planwise.client.projects2.components.listings :as listings]
[planwise.client.projects2.components.settings :as settings]
[planwise.client.projects2.components.create :as create]
[planwise.client.routes :as routes]
[planwise.client.ui.common :as ui]))

Expand Down Expand Up @@ -37,6 +38,7 @@
(let [section (:section @page-params)]
(case section
:index [listings/project-section-index]
:new [create/project-section-template-selector]
:show [project-section-show :scenarios]
:project-scenarios [project-section-show :scenarios]
:project-settings [project-section-show :settings]
Expand Down
2 changes: 2 additions & 0 deletions src/planwise/client/routes.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
(dispatch [:navigate {:page :sources}]))
(defroute projects2 "/projects2" []
(dispatch [:navigate {:page :projects2, :section :index}]))
(defroute projects2-new "/projects2/new" []
(dispatch [:navigate {:page :projects2, :section :new}]))
(defroute projects2-show "/projects2/:id" [id]
(dispatch [:navigate {:page :projects2, :id (js/parseInt id), :section :show}]))
(defroute projects2-show-with-step "/projects2/:id/steps/:step" [id step]
Expand Down
8 changes: 4 additions & 4 deletions src/planwise/component/projects2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
;; Service definition

(defn create-project
[store owner-id]
(db-create-project! (get-db store) {:owner-id owner-id
:name ""
:state "draft"}))
[store params]
(db-create-project! (get-db store) (-> (merge params {:state "draft" :config {}})
(assoc :config (pr-str (:config params)))
(assoc :providers (pr-str (:providers params))))))

(defn update-project
[store {:keys [config provider-set-id] :as project}]
Expand Down
48 changes: 48 additions & 0 deletions src/planwise/configuration/templates.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
(ns planwise.configuration.templates)

(defn templates-list
[]
[{:description "Plan facilities based on ground access"
:icon "directions_walk"
:key "plan"
:defaults {:name "ground"
:config {:coverage {:filter-options {:driving-time 90}}
:demographics {:target 12
:unit-name "humans"}
:actions {:budget 123123
:build [{:id "build-0"
:capacity 123
:investment 456}
{:id "build-1"
:capacity 333
:investment 444}]
:upgrade [{:id "upgrade-0"
:capacity 123
:investment 456}
{:id "upgrade-1"
:capacity 333
:investment 444}]}}

:source-set-id 2
:region-id 1
:provider-set-id 1}}
{:description "Plan diagonostic devices & sample referrals"
:icon "call_split"
:key "diagnosis"
:defaults {:name "sample"
:config {:coverage {}
:demographics {}
:actions {}}

:source-set-id nil
:region-id nil
:provider-set-id nil}}
{:key "empty"
:defaults {:name ""
:config {:coverage {}
:demographics {}
:actions {}}

:source-set-id nil
:region-id nil
:provider-set-id nil}}])
12 changes: 8 additions & 4 deletions src/planwise/endpoint/projects2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[planwise.model.projects2 :as model]
[planwise.boundary.providers-set :as providers-set]
[planwise.boundary.projects2 :as projects2]
[planwise.configuration.templates :as templates-config]
[planwise.boundary.scenarios :as scenarios]))

(timbre/refer-timbre)
Expand All @@ -26,11 +27,11 @@
[{service :projects2 service-scenarios :scenarios}]
(routes

(POST "/" request
(POST "/" [name project :as request]
(let [user-id (util/request-user-id request)
project-id (:id (projects2/create-project service user-id))
project (projects2/get-project service project-id)]
(response project)))
project-id (:id (projects2/create-project service (merge project {:owner-id user-id})))
project-db (projects2/get-project service project-id)]
(response project-db)))

(PUT "/:id" [id project :as request]
(let [user-id (util/request-user-id request)
Expand All @@ -44,6 +45,9 @@
(projects2/update-project service project)
(response (api-project (projects2/get-project service id)))))))

(GET "/templates" []
(response (templates-config/templates-list)))

(GET "/:id" [id :as request]
(let [user-id (util/request-user-id request)
project (filter-owned-by (projects2/get-project service (Integer. id)) user-id)]
Expand Down

0 comments on commit a8d74da

Please sign in to comment.