Skip to content

Commit

Permalink
allow restoring unsaved profile modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
ginesdt committed Dec 1, 2023
1 parent 1f85c85 commit dcb9a98
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
24 changes: 23 additions & 1 deletion resources/scss/components/popUpGrant.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#popUpGrant,
#popUpRestoreSettings,
#popUpAddContent,
#popUpSafeLink,
#popUpWarn {
Expand Down Expand Up @@ -82,7 +83,25 @@
display: none;
}
}
.popUpContent, .popUpGrant, .popUpSafeLink, .popUpWarn {
.popUpRestoreSettings {
.content {
padding: 20px 20px 2px;
}
.restore {
margin-bottom: 5px;
}
.discard {
color: black;
background-color: lightgrey;
&:hover {
background-color: $yellow;
}
}
.btBasic {
font-size: 16px;
}
}
.popUpContent, .popUpGrant, .popUpSafeLink, .popUpWarn, .popUpRestoreSettings {
width: calc(100% - 42px);
max-width: 490px;
margin: 0 auto;
Expand Down Expand Up @@ -268,6 +287,9 @@
}
}
}
.btBasic {
font-size: 16px;
}
}
@include media-breakpoint-up(lg) {
.popUpContent, .popUpGrant {
Expand Down
3 changes: 2 additions & 1 deletion src/streamtide/ui/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
(assoc :cart (:cart store))
(assoc :trust-domains (:trust-domains store))
(assoc :multipliers (:multipliers store))
(assoc :donations (:donations store)))))
(assoc :donations (:donations store))
(assoc :my-settings (:my-settings store)))))

(re-frame/reg-event-fx
::init
Expand Down
29 changes: 29 additions & 0 deletions src/streamtide/ui/my_settings/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
[district.ui.graphql.events :as gql-events]
[district.ui.logging.events :as logging]
[district.ui.notification.events :as notification-events]
[district.ui.web3-accounts.queries :as account-queries]
[re-frame.core :as re-frame]
[streamtide.shared.utils :as shared-utils]
[streamtide.ui.components.error-notification :as error-notification]
[streamtide.ui.components.verifiers :as verifiers]))

Expand Down Expand Up @@ -160,3 +162,30 @@
"Failed to verify social"
{:error error
:form-data form-data} ::verify-social]]}))

(re-frame/reg-event-fx
::store-settings-local
[(re-frame/inject-cofx :store)]
(fn [{:keys [db store]} [_ form-data]]
(let [active-account (account-queries/active-account db)
state {:form form-data
:touched? (-> form-data meta :touched?)
:timestamp (shared-utils/now-secs)}]
{:store (assoc-in store [:my-settings active-account] state)
:db (assoc-in db [:my-settings active-account] state)})))

(re-frame/reg-event-fx
::discard-stored-form
[(re-frame/inject-cofx :store)]
(fn [{:keys [db store]} _]
(let [active-account (account-queries/active-account db)]
{:store (update store :my-settings dissoc active-account)
:db (update db :my-settings dissoc active-account)})))

(re-frame/reg-event-fx
::restore-stored-form
(fn [{:keys [db]} [_ form-data-atom]]
(let [active-account (account-queries/active-account db)
state (get-in db [:my-settings active-account])]
(reset! form-data-atom (with-meta (:form state) {:touched? (:touched? state)}))
{})))
42 changes: 39 additions & 3 deletions src/streamtide/ui/my_settings/page.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[re-frame.core :refer [dispatch subscribe]]
[reagent.core :as r]
[reagent.ratom :refer [reaction]]
[streamtide.shared.utils :refer [valid-url? valid-email? expected-root-domain? social-domains deep-merge from-wei]]
[streamtide.shared.utils :refer [valid-url? valid-email? expected-root-domain? social-domains deep-merge from-wei now-secs]]
[streamtide.ui.components.app-layout :refer [app-layout]]
[streamtide.ui.components.error-notification :as error-notification]
[streamtide.ui.components.general :refer [no-items-found support-seal discord-invite-link]]
Expand Down Expand Up @@ -250,6 +250,24 @@
:on-success
(fn [] (close-popup nil))}])}]]]]]))))

(defn popup-restore-settings [_ restore-settings-loaded? form-data]
(fn [restore-settings-popup-open? _]
[:div {:id "popUpRestoreSettings" :style {"display" (if @restore-settings-popup-open? "flex" "none")}}
[:div.bgPopUp]
[:div.popUpRestoreSettings
[:div.content
[:h3 "Restore settings"]
[:p "You made some modifications to your profile you did not save. Do you want to restore them?"]
[:input.btBasic.btBasic-light.restore {:type "submit"
:value "Restore unsaved modifications"
:on-click (fn []
(dispatch [::ms-events/restore-stored-form form-data])
(reset! restore-settings-loaded? true))}]
[:input.btBasic.btBasic-light.discard {:type "submit"
:value "Discard modifications"
:on-click (fn []
(dispatch [::ms-events/discard-stored-form])
(reset! restore-settings-loaded? true))}]]]]))

(defn social-links []
(let [active-account (subscribe [::accounts-subs/active-account])]
Expand Down Expand Up @@ -479,12 +497,23 @@
(and (not-empty email)
(not (valid-email? email))))

(defn- restore-settings? [active-account restore-settings-loaded?]
(when @active-account
(let [stored-settings @(subscribe [::ms-subs/stored-settings @active-account])
require-restore? (and (not @restore-settings-loaded?)
stored-settings
(:touched? stored-settings)
(> (+ 864000 (:timestamp stored-settings)) (now-secs)))]
(when-not require-restore? (reset! restore-settings-loaded? true))
require-restore?)))

(defmethod page :route.my-settings/index []
(let [active-account (subscribe [::accounts-subs/active-account])
grant-popup-open? (r/atom false)
show-grant-popup-fn (fn [e show]
(when e (.stopPropagation e))
(switch-popup grant-popup-open? show))
restore-settings-loaded? (r/atom false)
form-data (r/atom {})
errors (reaction {:local (cond-> {}
(and (:min-donation @form-data)
Expand Down Expand Up @@ -512,9 +541,14 @@
(assoc :photo (-> @form-data :photo :error))
(-> @form-data :bg-photo :error)
(assoc :bg-photo (-> @form-data :bg-photo :error)))})]
(add-watch form-data :store-settings-local
(fn [_ _ _ new-state]
(when (-> new-state meta :touched?)
(dispatch [::ms-events/store-settings-local new-state]))))
(fn []
(check-session)
(let [user-settings (when @active-account (subscribe [::gql/query {:queries [(build-user-settings-query {:user/address @active-account})]}]))
(let [restore-settings-popup-open? (r/atom (restore-settings? active-account restore-settings-loaded?))
user-settings (when @active-account (subscribe [::gql/query {:queries [(build-user-settings-query {:user/address @active-account})]}]))
grant-status-query (when @active-account (subscribe [::gql/query {:queries [(build-grant-status-query {:user/address @active-account})]}
{:refetch-on [::ms-events/request-grant-success]}]))
grant-status (when grant-status-query (-> @grant-status-query :grant :grant/status gql-utils/gql-name->kw))
Expand Down Expand Up @@ -626,7 +660,9 @@
{:form-data (clean-form-data form-data form-values initial-values)
:on-success (fn []
(reset! form-data (with-meta @form-data {:touched? false}))
(dispatch [::ms-events/discard-stored-form @active-account])
(dispatch [::router-events/navigate :route.profile/index
{:address @active-account}]))}])}
"SAVE CHANGES"]]]]
[popup-request-grant grant-popup-open? show-grant-popup-fn #(clean-form-data form-data form-values initial-values)]]))))
[popup-request-grant grant-popup-open? show-grant-popup-fn #(clean-form-data form-data form-values initial-values)]
[popup-restore-settings restore-settings-popup-open? restore-settings-loaded? form-data]]))))
5 changes: 5 additions & 0 deletions src/streamtide/ui/my_settings/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@
::verifying-social?
(fn [db [_ network]]
(get-in db [:verifying-social? network])))

(re-frame/reg-sub
::stored-settings
(fn [db [_ address]]
(get-in db [:my-settings address])))

0 comments on commit dcb9a98

Please sign in to comment.