Skip to content

Commit

Permalink
Do not clear form when submitting new meme
Browse files Browse the repository at this point in the history
  • Loading branch information
ginesdt authored and madvas committed Apr 25, 2022
1 parent 66bd97a commit 34b7a06
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
25 changes: 16 additions & 9 deletions src/memefactory/ui/contract/meme_factory.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,40 @@

(re-frame/reg-event-fx
::approve-and-create-meme
(fn [{:keys [db]} [_ data deposit ipfs-response]]
(fn [{:keys [db]} [_ data ipfs-response]]
(log/info "Meme meta uploaded" {:ipfs-response ipfs-response} ::approve-and-create-meme)
(try-catch
(let [[{:keys [Name Hash Size] :as meme-meta}] (utils/parse-ipfs-response ipfs-response)
tx-id (str (random-uuid))
tx-name (gstring/format "%s submitted" (:title data))
tx-id (:send-tx/id data)
form-data (:form-data data)
deposit (:deposit data)
tx-name (gstring/format "%s submitted" (:title form-data))
active-account (account-queries/active-account db)
extra-data (web3-eth/contract-get-data (contract-queries/instance db :meme-factory)
:create-meme
active-account
Hash
(bn/number (:issuance data)))]
(bn/number (:issuance form-data)))]
{:dispatch [::tx-events/send-tx {:instance (contract-queries/instance db :DANK)
:fn :approve-and-call
:args [(contract-queries/contract-address db :meme-factory)
deposit
extra-data]
:tx-opts {:from active-account}
:tx-id {:meme/create-meme tx-id}
:tx-id {::approve-and-create-meme tx-id}
:tx-log {:name tx-name
:related-href {:name :route.memefolio/index
:query {:tab "created" :term (:title data)}}}
:query {:tab "created" :term (:title form-data)}}}
:on-tx-success-n [[::logging/info (str tx-name " tx success") meme-meta ::create-meme]
[::notification-events/show (gstring/format "%s was successfully submitted" (:title data))]]
[::notification-events/show (gstring/format "%s was successfully submitted" (:title form-data))]
[::creation-success]]
:on-tx-error [::logging/error
(str tx-name " tx error")
{:user {:id active-account}
:deposit deposit
:data data
:data form-data
:meme-meta meme-meta} ::create-meme]}]}))))


(re-frame/reg-event-db
::creation-success
(fn [db _] db))
20 changes: 9 additions & 11 deletions src/memefactory/ui/dank_registry/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,29 @@

(re-frame/reg-event-fx
::upload-meme
(fn [_ [_ {:keys [file-info] :as data} deposit]]
(log/info "Uploading meme image" {:file file-info} ::upload-meme)
(fn [_ [_ {:keys [:form-data :deposit] :as data}]]
(log/info "Uploading meme image" {:file (:file-info form-data)} ::upload-meme)
{:ipfs/call {:func "add"
:args [(:file file-info)]
:args [(:file (:file-info form-data))]
:opts {:wrap-with-directory true}
:on-success [::upload-meme-meta data deposit]
:on-error [::logging/error "upload-meme ipfs call error" {:data data
:deposit deposit}
:on-success [::upload-meme-meta data]
:on-error [::logging/error "upload-meme ipfs call error" {:data form-data}
::upload-meme]}}))

(re-frame/reg-event-fx
::upload-meme-meta
(fn [{:keys [db]} [_ data deposit ipfs-response]]
(fn [{:keys [db]} [_ data ipfs-response]]
(log/info "Meme image uploaded" {:ipfs-response ipfs-response} ::upload-meme-meta)
(try-catch
(let [resp (utils/parse-ipfs-response ipfs-response)
image-hash (str (-> resp last :Hash) "/" (-> resp first :Name))
meme-meta (build-meme-meta-string data image-hash)
meme-meta (build-meme-meta-string (:form-data data) image-hash)
buffer-data (js/buffer.Buffer.from meme-meta)]
(log/info "Uploading meme meta" {:meme-meta meme-meta} ::upload-meme-meta)
{:ipfs/call {:func "add"
:args [buffer-data]
:on-success [::meme-factory/approve-and-create-meme data deposit]
:on-success [::meme-factory/approve-and-create-meme data]
:on-error [::logging/error "upload-meme-meta ipfs call error"
{:data data
:deposit deposit
{:data (:form-data data)
:meme-meta meme-meta}
::upload-meme-meta]}}))))
25 changes: 18 additions & 7 deletions src/memefactory/ui/dank_registry/submit_page.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
[district.ui.graphql.subs :as gql]
[district.ui.web3-account-balances.subs :as balance-subs]
[district.ui.web3-accounts.subs :as accounts-subs]
[district.ui.web3-tx-id.subs :as tx-id-subs]
[goog.string :as gstring]
[memefactory.ui.components.app-layout :refer [app-layout]]
[memefactory.ui.components.buttons :refer [chain-check-pending-button]]
[memefactory.ui.components.general :refer [nav-anchor dank-with-logo]]
[memefactory.ui.components.tiles :refer [meme-image]]
[memefactory.ui.contract.meme-factory :as meme-factory]
[memefactory.ui.dank-registry.events :as dr-events]
[re-frame.core :refer [subscribe dispatch]]
[reagent.core :as r]
Expand Down Expand Up @@ -81,7 +84,10 @@
(assoc-in [:search-tags] (:error search-tags))))})
critical-errors (reaction (index-by-type @errors :error))
account-balance (subscribe [::balance-subs/active-account-balance :DANK])
active-account @(subscribe [::accounts-subs/active-account])]
active-account @(subscribe [::accounts-subs/active-account])
tx-id (str active-account "upload-meme" (random-uuid))
tx-pending? (subscribe [::tx-id-subs/tx-pending? {::meme-factory/approve-and-create-meme tx-id}])
tx-success? (subscribe [::tx-id-subs/tx-success? {::meme-factory/approve-and-create-meme tx-id}])]
(fn []
[app-layout
{:meta {:title "MemeFactory - Submit to Dank Registry"
Expand Down Expand Up @@ -149,12 +155,17 @@
:maxLength 500}]]
#_[:span.max-issuance (str "Max " max-meme-issuance)] ;; we are showing it on input focus
[:div.submit
[:button {:on-click (fn []
(dispatch [::dr-events/upload-meme @form-data deposit-value])
(reset! form-data {}))
:disabled (or (not (empty? @critical-errors))
(< @account-balance deposit-value))}
"Submit"]
[chain-check-pending-button {:pending? @tx-pending?
:disabled (or @tx-pending? @tx-success? (not (empty? @critical-errors))
(< @account-balance deposit-value) (not active-account))
:pending-text "Submitting ..."
:on-click #(dispatch [::dr-events/upload-meme
{:send-tx/id tx-id
:form-data @form-data
:deposit deposit-value}])}
(if @tx-success?
"Submitted"
"Submit")]
[dank-with-logo (web3/from-wei deposit-value :ether)]]
(when (< @account-balance deposit-value)
[:div.not-enough-dank "You don't have enough DANK tokens to submit a meme"])]]]])))
Expand Down

0 comments on commit 34b7a06

Please sign in to comment.