Skip to content

Commit

Permalink
Refactor re-frame grant/revoke roles logic
Browse files Browse the repository at this point in the history
  • Loading branch information
skydread1 committed Aug 4, 2023
1 parent e4d21e1 commit 374625f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 61 deletions.
62 changes: 12 additions & 50 deletions client/common/src/flybot/client/common/db/event.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -77,42 +77,24 @@
:fx [[:fx.log/message ["User logged out."]]]}))

(rf/reg-event-fx
:fx.http/grant-role-success
(fn [_ [_ role-granted {:keys [users]}]]
(let [{:user/keys [name roles]} (-> users :new-role role-granted)]
:fx.http/update-role-success
(fn [_ [_ operation role-granted {:keys [users]}]]
(let [{:user/keys [name roles]} (-> users operation role-granted)
new-role? (= :new-role operation)]
{:fx [[:dispatch [:evt.form/clear :form.role/fields]]
[:fx.log/message ["User " name "'s roles are now " (map :role/name roles)]]
[:dispatch [:evt.notif/set-notif
:success
"New role granted"
(if new-role? "New role granted" "Role revoked")
(str name
" is now "
(if new-role? " is now " " is no longer ")
(case role-granted
:owner "an owner"
:admin "an administrator"
:editor "an editor"
"a user")
".")]]]})))

(rf/reg-event-fx
:fx.http/revoke-role-success
(fn [_ [_ role-revoked {:keys [users]}]]
(let [{:user/keys [name]} (-> users :revoked-role role-revoked)]
{:fx [[:dispatch [:evt.form/clear :form.role/fields]]
[:fx.log/message
["User " name " has had a role revoked: " role-revoked]]
[:dispatch [:evt.notif/set-notif
:success
"Role revoked"
(str name
" is no longer "
(case role-revoked
:owner "an owner"
:admin "an administrator"
:editor "an editor"
"a user")
".")]]]})))

;; ---------- User ----------

(rf/reg-event-db
Expand All @@ -137,45 +119,25 @@
:on-failure [:fx.http/failure]}}))

(rf/reg-event-fx
:evt.user.form/grant-role
(fn [{:keys [db]} [_ role]]
(let [schema (valid/update-role-schema :new-role role)
:evt.user.form/update-role
(fn [{:keys [db]} [_ operation role]]
(let [schema (valid/update-role-schema operation role)
role-info (-> db :form.role/fields (valid/validate (valid/prepare-role schema)))
user-email (-> role-info :new-role role :user/email)]
user-email (-> role-info operation role :user/email)]
(if (:errors role-info)
{:fx [[:dispatch [:evt.notif/set-notif :error/form "Form Input Error" (valid/error-msg role-info)]]]}
{:http-xhrio {:method :post
:uri (base-uri "/pattern")
:headers {:cookie (:user/cookie db)}
:params {:users
{:new-role
{operation
{(list role :with [user-email])
{:user/name '?
:user/roles [{:role/name '?
:role/date-granted '?}]}}}}
:format (edn-request-format {:keywords? true})
:response-format (edn-response-format {:keywords? true})
:on-success [:fx.http/grant-role-success role]
:on-failure [:fx.http/failure]}}))))

(rf/reg-event-fx
:evt.user.form/revoke-role
(fn [{:keys [db]} [_ role]]
(let [schema (valid/update-role-schema :revoked-role role)
role-info (-> db :form.role/fields (valid/validate (valid/prepare-role schema)))
user-email (-> role-info :revoked-role role :user/email)]
(if (:errors role-info)
{:fx [[:dispatch [:evt.notif/set-notif :error/form "Form Input Error" (valid/error-msg role-info)]]]}
{:http-xhrio {:method :post
:uri (base-uri "/pattern")
:headers {:cookie (:user/cookie db)}
:params {:users
{:revoked-role
{(list role :with [user-email])
{:user/name '?}}}}
:format (edn-request-format {:keywords? true})
:response-format (edn-response-format {:keywords? true})
:on-success [:fx.http/revoke-role-success role]
:on-success [:fx.http/update-role-success operation role]
:on-failure [:fx.http/failure]}}))))

;; ---------- Post ----------
Expand Down
4 changes: 2 additions & 2 deletions client/web/src/flybot/client/web/core/dom/page/admin.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
[role]
[:button
{:type "button"
:on-click #(rf/dispatch [:evt.user.form/grant-role role])}
:on-click #(rf/dispatch [:evt.user.form/update-role :new-role role])}
svg/done-icon])

(defn revoke-role-button
[role]
[:button
{:type "button"
:on-click #(rf/dispatch [:evt.user.form/revoke-role role])}
:on-click #(rf/dispatch [:evt.user.form/update-role :revoked-role role])}
svg/trash-icon])

;;; --------- Forms ----------
Expand Down
18 changes: 10 additions & 8 deletions client/web/test/flybot/client/web/core/dom/page/admin_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
;; Initialize db
(rf/dispatch [:evt.app/initialize]))

(deftest grant-revoke-roles
(deftest update-roles
(with-redefs [utils/mk-date (constantly s/alice-date-granted)]
(rf-test/run-test-sync
(test-fixtures)
Expand All @@ -41,7 +41,7 @@
;; Fill new role form
(rf/dispatch [:evt.role.form/set-field :new-role :admin :user/email "email@wrong.com"])
;; Attempt to grant role
(rf/dispatch [:evt.user.form/grant-role :admin])
(rf/dispatch [:evt.user.form/update-role :new-role :admin])
(testing "Form not cleared, error notification added to DB."
(is @role-form)
(is (= "Form Input Error" (:notification/title @notification))))
Expand All @@ -50,7 +50,7 @@

(testing "Revoke role: Validation error:"
(rf/dispatch [:evt.role.form/set-field :revoked-role :admin :user/email "email@wrong.com"])
(rf/dispatch [:evt.user.form/revoke-role :admin])
(rf/dispatch [:evt.user.form/update-role :revoked-role :admin])
(testing "Form not cleared, error notification added to DB."
(is @role-form)
(is (= "Form Input Error" (:notification/title @notification))))
Expand All @@ -63,20 +63,21 @@
(rf/dispatch [:fx.http/failure {:status 479}])))
(rf/dispatch [:evt.role.form/set-field
:revoked-role :admin :user/email email])
(rf/dispatch [:evt.user.form/revoke-role :admin])
(rf/dispatch [:evt.user.form/update-role :revoked-role :admin])
(testing "Form not cleared, error notification added to DB."
(is @role-form)
(is (= "HTTP error" (:notification/title @notification)))))

(testing "Grant role: Success:"
(rf/reg-fx :http-xhrio
(fn [_] (rf/dispatch
[:fx.http/grant-role-success
[:fx.http/update-role-success
:new-role
:admin
{:users {:new-role {:admin admin-alice}}}])))
(rf/dispatch [:evt.role.form/set-field
:new-role :admin :user/email email])
(rf/dispatch [:evt.user.form/grant-role :admin])
(rf/dispatch [:evt.user.form/update-role :new-role :admin])
(testing "Form cleared."
(is (not @role-form)))
(testing "Notification sent."
Expand All @@ -88,12 +89,13 @@
(testing "Revoke role: Success:"
(rf/reg-fx :http-xhrio
(fn [_] (rf/dispatch
[:fx.http/revoke-role-success
[:fx.http/update-role-success
:revoked-role
:admin
{:users {:revoked-role {:admin admin-alice}}}])))
(rf/dispatch [:evt.role.form/set-field
:revoked-role :admin :user/email email])
(rf/dispatch [:evt.user.form/revoke-role :admin])
(rf/dispatch [:evt.user.form/update-role :revoked-role :admin])
(testing "Form cleared."
(is (not @role-form)))
(testing "Notification sent."
Expand Down
1 change: 0 additions & 1 deletion common/src/flybot/common/validation.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@

(defn error-msg
[errors]
(println errors)
(-> errors
(me/humanize
{:errors (-> me/default-errors
Expand Down

0 comments on commit 374625f

Please sign in to comment.