Skip to content

Instantly share code, notes, and snippets.

@troglotit
Last active July 4, 2018 07:47
Show Gist options
  • Save troglotit/fa52d56bf6add4dab400c251a056c1b4 to your computer and use it in GitHub Desktop.
Save troglotit/fa52d56bf6add4dab400c251a056c1b4 to your computer and use it in GitHub Desktop.
(defn request-flow
[{:keys [method
route
params
allow-empty-string
hide-auth-header
on-success
on-failure
url-host]}]
(let [request-id (keyword (str "request-flow/" (gensym "id-")))
success-pred (fn [[event-type cb-id]]
(and (= event-type :request-success)
(= request-id cb-id)))
failure-pred (fn [[event-type cb-id]]
(and (= event-type :request-failure)
(= request-id cb-id)))
dispatch-on-success (fn [[e _ payload]] [(conj on-success payload)])
dispatch-on-failure (fn [[e _ payload]] [(conj on-failure payload)])
opts {:method method
:route route
:params params
:request-id request-id
:allow-empty-string allow-empty-string
:hide-auth-header hide-auth-header}
opts (if (nil? url-host)
opts
(merge opts {:url-host url-host}))]
{:first-dispatch
[:request opts]
:rules
[{:when :seen?
:events success-pred
:dispatch-fn dispatch-on-success
:halt? true}
{:when :seen?
:events failure-pred
:dispatch-fn dispatch-on-failure
:halt? true}]}))
(rf/reg-event-fx
:request
(fn [{:keys [db]}
[_ {:keys [method
route
params
request-id
allow-empty-string
hide-auth-header
url-host]
:or {allow-empty-string false
hide-auth-header false
url-host equipage-rest-api-url}
:as request-params}]]
(let [method (case method
:get ajax/GET
:post ajax/POST
:put ajax/PUT
:patch ajax/PATCH
:delete ajax/DELETE)
token (:token db)
params (if allow-empty-string
params
(remove-empty-string params))
params (transform-keys ->snake_case params)
handler (comp #(rf/dispatch [:request-success request-id %])
(partial transform-keys ->kebab-case))
error-handler (comp #(rf/dispatch [:check-bad-auth request-id request-params %])
(partial transform-keys ->kebab-case))
uri (str url-host route)
ajax-params {:params params
:format :json
:response-format :json
:keywords? true
:handler handler
:error-handler error-handler}
ajax-params (if (and token (not hide-auth-header))
(merge
ajax-params
{:headers {"Authorization" (str "Bearer " (:token token))}})
ajax-params)]
{:http {:method method
:uri uri
:ajax-params ajax-params}})))
(rf/reg-event-fx
:request-success
(fn []))
(rf/reg-event-fx
:check-bad-auth
(fn [{:keys [db]} [_ request-id request-params {{[error]
:errors} :response
:keys [status]
:as payload}]]
(if (= status 401)
(if (= error {:type "bad_auth"
:message "expired_token"})
{:async-flow
{:first-dispatch
[:refresh-company]
:rules
[{:when :seen?
:events [:refresh-company-success]
:dispatch [:request request-params]
:halt? true}
{:when :seen?
:events [:refresh-company-failure]
:dispatch [:logout]
:halt? true}]}}
{:dispatch [:logout]})
{:dispatch [:request-failure request-id payload]})))
(rf/reg-event-fx
:request-failure
(fn []))
(rf/reg-event-fx
:get-user-info
(fn [{:keys [db]} [_]]
{:async-flow
(request-flow {:method :get
:route "/users/me"
:on-success [:get-user-info-success]
:on-failure [:get-user-info-failure]})}))
(rf/reg-event-db
:get-user-info-success
(fn [db [_ {:keys [user]}]]
(assoc db :user user)))
(rf/reg-event-fx
:get-user-info-failure
(fn []))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment