Skip to content

Commit

Permalink
FIX: Allow reverting theme/text settings from cookie back to default
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaylorhq committed Feb 8, 2019
1 parent 62043e6 commit 95eb4c6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,12 @@ export default Ember.Controller.extend(PreferencesTabController, {
},

preferencesController: Ember.inject.controller("preferences"),
makeThemeDefault: true,
makeTextSizeDefault: true,

@computed()
availableLocales() {
return JSON.parse(this.siteSettings.available_locales);
},

@computed()
themeId() {
return currentThemeId();
},

@computed
textSizes() {
return TEXT_SIZES.map(value => {
Expand All @@ -81,6 +74,16 @@ export default Ember.Controller.extend(PreferencesTabController, {
previewTheme([id]);
},

@computed("model.user_option.theme_ids", "themeId")
showThemeSetDefault(userOptionThemes, selectedTheme) {
return userOptionThemes[0] !== selectedTheme;
},

@computed("model.user_option.text_size", "textSize")
showTextSetDefault(userOptionTextSize, selectedTextSize) {
return userOptionTextSize !== selectedTextSize;
},

homeChanged() {
const siteHome = this.siteSettings.top_menu.split("|")[0].split(",")[0];
const userHome = USER_HOMES[this.get("model.user_option.homepage_id")];
Expand Down Expand Up @@ -120,13 +123,17 @@ export default Ember.Controller.extend(PreferencesTabController, {
.then(() => {
this.set("saved", true);

if (!makeThemeDefault) {
if (makeThemeDefault) {
setLocalTheme([]);
} else {
setLocalTheme(
[this.get("themeId")],
this.get("model.user_option.theme_key_seq")
);
}
if (!makeTextSizeDefault) {
if (makeTextSizeDefault) {
this.get("model").updateTextSizeCookie(null);
} else {
this.get("model").updateTextSizeCookie(this.get("textSize"));
}

Expand Down
14 changes: 9 additions & 5 deletions app/assets/javascripts/discourse/models/user.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -719,11 +719,15 @@ const User = RestModel.extend({
},

updateTextSizeCookie(newSize) {
const seq = this.get("user_option.text_size_seq");
$.cookie("text_size", `${newSize}|${seq}`, {
path: "/",
expires: 9999
});
if (newSize) {
const seq = this.get("user_option.text_size_seq");
$.cookie("text_size", `${newSize}|${seq}`, {
path: "/",
expires: 9999
});
} else {
$.removeCookie("text_size", { path: "/", expires: 1 });
}
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import RestrictedUserRoute from "discourse/routes/restricted-user";
import { currentThemeId } from "discourse/lib/theme-selector";

export default RestrictedUserRoute.extend({
showFooter: true,

setupController(controller, user) {
controller.setProperties({
model: user,
textSize: user.get("currentTextSize")
textSize: user.get("currentTextSize"),
themeId: currentThemeId(),
makeThemeDefault:
currentThemeId() === user.get("user_option.theme_ids")[0],
makeTextSizeDefault:
user.get("currentTextSize") === user.get("user_option.text_size")
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
<div class="controls">
{{combo-box content=userSelectableThemes value=themeId}}
</div>
<div class="controls">
{{preference-checkbox labelKey="user.theme_default_on_all_devices" checked=makeThemeDefault}}
</div>
{{#if showThemeSetDefault}}
<div class="controls">
{{preference-checkbox labelKey="user.theme_default_on_all_devices" checked=makeThemeDefault}}
</div>
{{/if}}
</div>
{{/if}}

Expand All @@ -15,9 +17,11 @@
<div class="controls">
{{combo-box valueAttribute="value" content=textSizes value=textSize onSelect=(action "selectTextSize")}}
</div>
<div class="controls">
{{preference-checkbox labelKey="user.text_size_default_on_all_devices" checked=makeTextSizeDefault}}
</div>
{{#if showTextSetDefault}}
<div class="controls">
{{preference-checkbox labelKey="user.text_size_default_on_all_devices" checked=makeTextSizeDefault}}
</div>
{{/if}}
</div>

{{#if siteSettings.allow_user_locale}}
Expand Down
2 changes: 1 addition & 1 deletion test/javascripts/acceptance/preferences-test.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ QUnit.test("font size change", async assert => {
await selectKitSelectRowByValue("largest", ".text-size .combobox");

await savePreferences();
assert.equal($.cookie("text_size"), "larger|1", "cookie remains the same");
assert.equal($.cookie("text_size"), null, "cookie is removed");

$.removeCookie("text_size");
});
Expand Down

0 comments on commit 95eb4c6

Please sign in to comment.