Skip to content

Commit

Permalink
Toggle button disabled when Tx in progress. Fixes #415
Browse files Browse the repository at this point in the history
  • Loading branch information
madis committed May 14, 2024
1 parent 4b72b98 commit 6db6992
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
15 changes: 11 additions & 4 deletions ui/src/ethlance/ui/page/job_contract.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,16 @@
(let [text (re/subscribe [:page.job-contract/accept-proposal-message-text])
proposal-data (assoc (select-keys message-params [:job/id :job-story/id :candidate :employer])
:text @text)
can-accept? (= :proposal (:job-story/status message-params))]
can-accept? (= :proposal (:job-story/status message-params))
button-disabled? (re/subscribe [:page.job-contract/buttons-disabled?])]
(if can-accept?
[:div.message-input-container
[:div.label "Message"]
[c-textarea-input {:placeholder ""
:value @text
:on-change #(re/dispatch [:page.job-contract/set-accept-proposal-message-text %])}]
[c-button {:color :primary
:disabled? @button-disabled?
:on-click #(re/dispatch [:page.job-contract/accept-proposal proposal-data])}
[c-button-label "Accept Proposal"]]]

Expand Down Expand Up @@ -451,7 +453,8 @@
(not has-arbiter?) "This job doesn't yet have an arbiter so disputes can't be created."
has-invoice? "You have already raised a dispute on your latest invoice. One invoice can only be disputed once."
:else "Raising dispute becomes available after creating an invoice.")
dispute-text (re/subscribe [:page.job-contract/dispute-text])]
dispute-text (re/subscribe [:page.job-contract/dispute-text])
button-disabled? (re/subscribe [:page.job-contract/buttons-disabled?])]
[c-tabular-layout
{:key "candidate-tabular-layout"
:default-tab 0}
Expand Down Expand Up @@ -484,6 +487,7 @@
:value @dispute-text
:on-change #(re/dispatch [:page.job-contract/set-dispute-text %])}]
[c-button {:color :primary
:disabled? @button-disabled?
:on-click #(re/dispatch [:page.job-contract/raise-dispute
{:job/id job-id
:job-story/id job-story-id
Expand Down Expand Up @@ -559,7 +563,8 @@
resolution-contract-amount (* candidate-invoiced-amount resolution-percentage)
feedback-available-for-arbiter? (or
(not-empty (get-in @invoice-result [:job-story :job-story/employer-feedback]))
(not-empty (get-in @invoice-result [:job-story :job-story/candidate-feedback])))]
(not-empty (get-in @invoice-result [:job-story :job-story/candidate-feedback])))
button-disabled? (re/subscribe [:page.job-contract/buttons-disabled?])]
[c-tabular-layout
{:key "arbiter-tabular-layout"
:default-tab 0}
Expand Down Expand Up @@ -598,7 +603,9 @@
[c-textarea-input {:placeholder "Please explain the reasoning behind the resolution"
:value @dispute-text
:on-change #(re/dispatch [:page.job-contract/set-dispute-text %])}]
[c-button {:color :primary :on-click #(re/dispatch [:page.job-contract/resolve-dispute
[c-button {:color :primary
:disabled? @button-disabled?
:on-click #(re/dispatch [:page.job-contract/resolve-dispute
{:job/id job-id
:job-story/id job-story-id
:invoice/id invoice-id
Expand Down
65 changes: 44 additions & 21 deletions ui/src/ethlance/ui/page/job_contract/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@
(re/reg-event-fx :page.job-contract/set-feedback-rating (create-assoc-handler :feedback-rating))
(re/reg-event-fx :page.job-contract/set-feedback-text (create-assoc-handler :feedback-text))

(re/reg-event-db :page.job-contract/dispute-to-ipfs-failure (create-logging-handler))
(re/reg-event-fx :page.job-contract/tx-hash (create-logging-handler))
(re/reg-event-db ::dispute-tx-error (create-logging-handler))


(defn clear-forms
Expand Down Expand Up @@ -128,10 +126,11 @@
:job/id job-id
:job-story/id job-story-id
:invoice/id invoice-id}]
{:ipfs/call {:func "add"
{:fx [[:dispatch [::set-buttons-disabled true]]]
:ipfs/call {:func "add"
:args [(js/Blob. [ipfs-dispute])]
:on-success [:page.job-contract/raise-dispute-to-ipfs-success ipfs-dispute]
:on-error [:page.job-contract/dispute-to-ipfs-failure invoice-id]}}))
:on-error [::dispute-to-ipfs-failure invoice-id]}}))


(re/reg-event-fx
Expand All @@ -142,16 +141,22 @@
invoice-id (:invoice/id dispute-details)
job-contract-address (:job/id dispute-details)
tx-opts {:from creator}]
{:dispatch [::web3-events/send-tx
{:instance (contract-queries/instance db :job job-contract-address)
:fn :raiseDispute
:args [invoice-id ipfs-hash]
:tx-opts tx-opts
:tx-hash [::tx-hash]
:on-tx-hash-error [::tx-hash-error]
:on-tx-success [::dispute-tx-success "Transaction to raise dispute processed successfully"]
:on-tx-error [::dispute-tx-error]}]})))

{:fx [[:dispatch [::set-buttons-disabled true]]
[:dispatch [::web3-events/send-tx
{:instance (contract-queries/instance db :job job-contract-address)
:fn :raiseDispute
:args [invoice-id ipfs-hash]
:tx-opts tx-opts
:tx-hash [::tx-hash]
:on-tx-hash-error [::tx-hash-error]
:on-tx-success [::dispute-tx-success "Transaction to raise dispute processed successfully"]
:on-tx-error [::dispute-tx-error]}]]]})))


(re/reg-event-db
::set-buttons-disabled
(fn [db [_ disabled?]]
(assoc-in db [state-key :buttons-disabled?] disabled?)))

(re/reg-event-fx
:page.job-contract/accept-proposal
Expand All @@ -163,7 +168,8 @@
:job/id (:job/id proposal-data)
:message/creator (:employer proposal-data)
:text (:text proposal-data)}]
{:ipfs/call {:func "add"
{:fx [[:dispatch [::set-buttons-disabled true]]]
:ipfs/call {:func "add"
:args [(js/Blob. [to-ipfs])]
:on-success [:accept-proposal-to-ipfs-success to-ipfs]
:on-error [::accept-proposal-to-ipfs-failure to-ipfs]}})))
Expand Down Expand Up @@ -191,22 +197,29 @@
(re/reg-event-fx
::accept-proposal-to-ipfs-failure
(fn [_cofx event]
(println ">>> :invitation-to-ipfs-failure" event)))
{:fx [[:dispatch [::set-buttons-disabled false]]]}))


(re/reg-event-fx
::accept-proposal-tx-success
(fn [{:keys [db]} event]
(println ">>> ::accept-proposal-tx-success" event)
{:db (clear-forms db)
:fx [[:dispatch [:page.job-contract/refetch-messages]]
:fx [[:dispatch [::set-buttons-disabled false]]
[:dispatch [:page.job-contract/refetch-messages]]
[:dispatch [::notification.events/show "Transaction to accept proposal processed successfully"]]]}))


(re/reg-event-fx
::tx-hash-error
(fn [_cofx _event]
{:fx [[:dispatch [::set-buttons-disabled false]]
[:dispatch [::notification.events/show "Error, the transaction was not sent"]]]}))

(re/reg-event-fx
::accept-proposal-tx-failure
(fn [_cofx _event]
{:dispatch [::notification.events/show "Error processing accept proposal transaction"]}))
{:fx [[:dispatch [::set-buttons-disabled false]]
[:dispatch [::notification.events/show "Error processing accept proposal transaction"]]]}))


(re/reg-event-fx
Expand All @@ -219,9 +232,19 @@
::dispute-tx-success
(fn [{:keys [db]} [_event-name message]]
{:db (clear-forms db)
:fx [[:dispatch [:page.job-contract/refetch-messages]]
:fx [[:dispatch [::set-buttons-disabled false]]
[:dispatch [:page.job-contract/refetch-messages]]
[:dispatch [::notification.events/show message]]]}))

(re/reg-event-fx
::dispute-to-ipfs-failure
(fn [_cofx _]
{:fx [[:dispatch [::set-buttons-disabled false]]]}))

(re/reg-event-db
::dispute-tx-error
(fn [_cofx _]
{:fx [[:dispatch [::set-buttons-disabled false]]]}))

(defn send-resolve-dispute-ipfs
[{:keys [db]} [_ {invoice-id :invoice/id
Expand All @@ -236,7 +259,7 @@
{:ipfs/call {:func "add"
:args [(js/Blob. [ipfs-dispute])]
:on-success [:page.job-contract/resolve-dispute-to-ipfs-success event]
:on-error [:page.job-contract/dispute-to-ipfs-failure event]}}))
:on-error [::dispute-to-ipfs-failure event]}}))


(defn send-resolve-dispute-tx
Expand Down
1 change: 1 addition & 0 deletions ui/src/ethlance/ui/page/job_contract/subscriptions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@

(re/reg-sub :page.job-contract/feedback-rating (create-get-handler :feedback-rating))
(re/reg-sub :page.job-contract/feedback-text (create-get-handler :feedback-text))
(re/reg-sub :page.job-contract/buttons-disabled? (create-get-handler :buttons-disabled?))

0 comments on commit 6db6992

Please sign in to comment.