0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-24 15:39:50 -05:00

Improve error handling on onboarding questions form

This commit is contained in:
Andrey Antukh 2022-09-21 09:29:03 +02:00
parent ec53288b66
commit 21683be07b
3 changed files with 47 additions and 14 deletions

View file

@ -21,7 +21,6 @@
(defmethod ptk/resolve :default
[type data]
(log/warn :hint "no implementation found for event" :event type)
(ptk/data-event type data))
(defonce state

View file

@ -7,34 +7,63 @@
(ns app.main.ui.onboarding.questions
"External form for onboarding questions."
(:require
[app.main.data.events :as ev]
[app.main.data.users :as du]
[app.main.store :as st]
[app.util.dom :as dom]
[goog.events :as ev]
[goog.events :as gev]
[potok.core :as ptk]
[promesa.core :as p]
[rumext.alpha :as mf]))
(defn load-arengu-sdk
[container-ref email form-id]
(letfn [(on-init []
(when-let [container (mf/ref-val container-ref)]
(letfn [(on-arengu-loaded [resolve reject]
(let [container (mf/ref-val container-ref)]
(-> (.embed js/ArenguForms form-id container)
(p/then (fn [form]
(.setHiddenField ^js form "email" email))))))
(.setHiddenField ^js form "email" email)
(st/emit! (ptk/event ::ev/event {::ev/name "arengu-form-load-success"
::ev/origin "onboarding-questions"
::ev/type "fact"}))
(on-submit-success [_event]
(st/emit! (du/mark-questions-as-answered)))]
(resolve)))
(p/catch reject))))
(let [script (dom/create-element "script")
head (unchecked-get js/document "head")
lkey1 (ev/listen js/document "af-submitForm-success" on-submit-success)]
(mark-as-answered []
(st/emit! (du/mark-questions-as-answered)))
(unchecked-set script "src" "https://sdk.arengu.com/forms.js")
(unchecked-set script "onload" on-init)
(dom/append-child! head script)
(initialize [cleaners resolve reject]
(let [script (dom/create-element "script")
head (unchecked-get js/document "head")
lkey1 (gev/listen js/document "af-submitForm-success" mark-as-answered)
lkey2 (gev/listen js/document "af-getForm-error" reject)]
(unchecked-set script "src" "https://sdk.arengu.com/forms.js")
(unchecked-set script "onload" (partial on-arengu-loaded resolve reject))
(dom/append-child! head script)
(swap! cleaners conj
#(do (gev/unlistenByKey lkey1)
(gev/unlistenByKey lkey2)))
(swap! cleaners conj
#(dom/remove-child! head script))))
(on-error [_]
(st/emit! (ptk/event ::ev/event {::ev/name "arengu-form-load-error"
::ev/origin "onboarding-questions"
::ev/type "fact"}))
(mark-as-answered))
]
(let [cleaners (atom #{})]
(-> (p/create (partial initialize cleaners))
(p/timeout 5000)
(p/catch on-error))
(fn []
(ev/unlistenByKey lkey1)))))
(run! (fn [clean-fn] (clean-fn)) @cleaners)))))
(mf/defc questions
[{:keys [profile form-id]}]

View file

@ -262,6 +262,11 @@
(when (some? el)
(.appendChild ^js el child)))
(defn remove-child!
[^js el child]
(when (some? el)
(.removeChild ^js el child)))
(defn get-first-child
[^js el]
(when (some? el)