This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/controllers/preferences/profile.js
Martin Brennan b8b29e79ad
FIX: Improve user timezone saving (#9230)
Based on issues identified in https://meta.discourse.org/t/improved-bookmarks-with-reminders/144542/20

* Implement the resolvedTimezone() function on the user model where we return the user's timezone if it has been set, or we guess it using moment and save it to the user using an update call if it has not yet been set. This covers the cases of users who do not log out/in often who will not get their timezone set via login. This also makes sure the guess + save is done in a non-obtrusive way not on every page -- only when it is needed.

* Before if a user's timezone was blank when they visited their profile page we were autofilling the dropdown with the guessed timezone from moment. However this was confusing as it would appear you have that timezone saved in the DB when you really didn't. Now we do not autofill the dropdown and added a button to automatically guess the current timezone to make everything more explicit.
2020-03-24 11:39:09 +10:00

116 lines
3.2 KiB
JavaScript

import { isEmpty } from "@ember/utils";
import EmberObject from "@ember/object";
import Controller from "@ember/controller";
import discourseComputed from "discourse-common/utils/decorators";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { cookAsync } from "discourse/lib/text";
import { ajax } from "discourse/lib/ajax";
import showModal from "discourse/lib/show-modal";
export default Controller.extend({
init() {
this._super(...arguments);
this.saveAttrNames = [
"bio_raw",
"website",
"location",
"custom_fields",
"user_fields",
"profile_background_upload_url",
"card_background_upload_url",
"date_of_birth",
"timezone"
];
},
@discourseComputed("model.user_fields.@each.value")
userFields() {
let siteUserFields = this.site.get("user_fields");
if (!isEmpty(siteUserFields)) {
const userFields = this.get("model.user_fields");
// Staff can edit fields that are not `editable`
if (!this.get("currentUser.staff")) {
siteUserFields = siteUserFields.filterBy("editable", true);
}
return siteUserFields.sortBy("position").map(function(field) {
const value = userFields
? userFields[field.get("id").toString()]
: null;
return EmberObject.create({ value, field });
});
}
},
@discourseComputed("model.can_change_bio")
canChangeBio(canChangeBio) {
return canChangeBio;
},
actions: {
showFeaturedTopicModal() {
showModal("feature-topic-on-profile", {
model: this.model,
title: "user.feature_topic_on_profile.title"
});
},
clearFeaturedTopicFromProfile() {
bootbox.confirm(
I18n.t("user.feature_topic_on_profile.clear.warning"),
result => {
if (result) {
ajax(`/u/${this.model.username}/clear-featured-topic`, {
type: "PUT"
})
.then(() => {
this.model.set("featured_topic", null);
})
.catch(popupAjaxError);
}
}
);
},
useCurrentTimezone() {
this.model.set("user_option.timezone", moment.tz.guess());
},
save() {
this.set("saved", false);
const model = this.model,
userFields = this.userFields;
// Update the user fields
if (!isEmpty(userFields)) {
const modelFields = model.get("user_fields");
if (!isEmpty(modelFields)) {
userFields.forEach(function(uf) {
modelFields[uf.get("field.id").toString()] = uf.get("value");
});
}
}
return model
.save(this.saveAttrNames)
.then(() => {
// update the timezone in memory so we can use the new
// one if we change routes without reloading the user
if (this.currentUser.id === this.model.id) {
this.currentUser.changeTimezone(this.model.user_option.timezone);
}
cookAsync(model.get("bio_raw"))
.then(() => {
model.set("bio_cooked");
this.set("saved", true);
})
.catch(popupAjaxError);
})
.catch(popupAjaxError);
}
}
});