-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Delete organisation #15
Changes from 9 commits
b730b09
1fd71c6
a708212
7cfa606
0f5f1e3
2392f4e
b571384
917f1d6
e33a249
77a5140
a228ed3
98f9abc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
(ns villagebook.organisation.models | ||
(:require [villagebook.organisation.db :as db] | ||
[villagebook.factory :as factory])) | ||
[villagebook.factory :as factory] | ||
[clojure.java.jdbc :as jdbc] | ||
[villagebook.config :as config])) | ||
|
||
(defonce DEFAULT_PERMISSION :owner) | ||
(defonce OWNER_PERMISSION :owner) | ||
|
||
(defn create! | ||
[orgdata user-id] | ||
(let [{org-id :id :as org} (db/create! orgdata) | ||
permission (db/add-user-as! org-id user-id DEFAULT_PERMISSION)] | ||
permission (db/add-user-as! org-id user-id OWNER_PERMISSION)] | ||
{:success org})) | ||
|
||
(defn retrieve | ||
|
@@ -20,3 +22,18 @@ | |
(defn retrieve-by-user | ||
[user-id] | ||
{:success (db/retrieve-by-user user-id)}) | ||
|
||
(defn is-owner? | ||
([org-id user-id] | ||
(is-owner? (config/db-spec) org-id user-id)) | ||
([conn org-id user-id] | ||
(= (db/get-permission conn user-id org-id) (name OWNER_PERMISSION)))) | ||
|
||
(defn delete! | ||
[user-id id] | ||
"Check if user is owner of the organisation and delete it" | ||
(jdbc/with-db-transaction [trn (config/db-spec)] | ||
(if (is-owner? trn id user-id) | ||
(if-not (empty? (db/delete! trn id)) | ||
{:success "Organisation deleted"}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the else section is missing here, this returns |
||
{:error "Invalid permission"}))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,45 @@ | ||
(ns villagebookUI.components.main-content | ||
(:require [villagebookUI.store.organisations :as org-store])) | ||
(:require [villagebookUI.store.organisations :as org-store] | ||
[villagebookUI.api.organisation :as org-api] | ||
[villagebookUI.fetchers :as fetchers] | ||
[villagebookUI.components.utils :as utils] | ||
[villagebookUI.store.ui :as ui-store] | ||
[villagebookUI.helpers :as helpers])) | ||
|
||
(declare navbar content-box delete-org-btn delete-org) | ||
|
||
(defn main-content [] | ||
[:div.main-content | ||
[:div.navbar | ||
[:h5 (:name (org-store/get-selected))]]]) | ||
(let [org (org-store/get-selected)] | ||
[:div.main-content | ||
[navbar org] | ||
(if org | ||
[content-box] | ||
[:h5 "Oops, page not found"]) | ||
[utils/alert-bottom (ui-store/get-el-state :alert-bottom)]])) | ||
|
||
(defn- navbar | ||
[org] | ||
[:div.navbar | ||
[:h5 (:name org)] | ||
[delete-org-btn org]]) | ||
|
||
(defn content-box [] | ||
[:div]) | ||
|
||
(defn delete-org-btn | ||
[org] | ||
(if (= (:permission org) "owner") | ||
[:a {:href "#" | ||
:style {:height "30px"} | ||
:on-click #(delete-org org)} | ||
"Delete organisation"])) | ||
|
||
(defn delete-org | ||
[org] | ||
(if (js/confirm (str "Are you sure you want to delete " (:name org) "?")) | ||
(org-api/delete (:id org) | ||
(fn [res] | ||
(helpers/show-alert-bottom :success res) | ||
(fetchers/fetch-orgs! first)) | ||
(fn [res] | ||
(helpers/show-alert-bottom :error res))))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
(ns villagebookUI.helpers | ||
(:require [villagebookUI.store.session :as session])) | ||
|
||
(:require [villagebookUI.store.session :as session] | ||
[villagebookUI.store.ui :as ui-store])) | ||
|
||
(defn random-color [] | ||
(str "hsl(" (->> (.random js/Math) | ||
|
@@ -41,3 +41,10 @@ | |
(on-success)) | ||
(fn [res] | ||
(show-error (:response res)))))) | ||
|
||
(defn show-alert-bottom | ||
[status message & [time]] | ||
(let [time (if (number? time) (* time 1000) 4000)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking if time is a number and is not nil. |
||
(ui-store/set! :alert-bottom {:status status | ||
:message message}) | ||
(js/setTimeout #(ui-store/set! :alert-bottom nil) time))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
(ns villagebookUI.store.core | ||
(:require [villagebookUI.store.user :as user-store] | ||
[villagebookUI.store.session :as session] | ||
[villagebookUI.store.organisations :as org-store])) | ||
[villagebookUI.store.organisations :as org-store] | ||
[villagebookUI.store.ui :as ui])) | ||
|
||
(defn init! [] | ||
(session/init!) | ||
(ui/init!) | ||
(user-store/init!) | ||
(org-store/init!)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
(ns villagebookUI.store.ui | ||
(:require [reagent.core :as r] | ||
[villagebookUI.store.state :refer [state]])) | ||
|
||
(def ui-state | ||
(r/cursor state [:ui])) | ||
|
||
(defn get-el-state [element-key] | ||
(get @ui-state element-key)) | ||
|
||
(defn set! [element-key val] | ||
(swap! ui-state assoc element-key val)) | ||
|
||
(defn init! [] | ||
(reset! ui-state {})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jdbc/find-by-keys
?