diff --git a/api/preference.go b/api/preference.go index d9ddb1a216..240ead5712 100644 --- a/api/preference.go +++ b/api/preference.go @@ -16,6 +16,7 @@ func InitPreference() { BaseRoutes.Preferences.Handle("/", ApiUserRequired(getAllPreferences)).Methods("GET") BaseRoutes.Preferences.Handle("/save", ApiUserRequired(savePreferences)).Methods("POST") + BaseRoutes.Preferences.Handle("/delete", ApiUserRequired(deletePreferences)).Methods("POST") BaseRoutes.Preferences.Handle("/{category:[A-Za-z0-9_]+}", ApiUserRequired(getPreferenceCategory)).Methods("GET") BaseRoutes.Preferences.Handle("/{category:[A-Za-z0-9_]+}/{name:[A-Za-z0-9_]+}", ApiUserRequired(getPreference)).Methods("GET") } @@ -81,3 +82,30 @@ func getPreference(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(data.ToJson())) } } + +func deletePreferences(c *Context, w http.ResponseWriter, r *http.Request) { + preferences, err := model.PreferencesFromJson(r.Body) + if err != nil { + c.Err = model.NewLocAppError("savePreferences", "api.preference.delete_preferences.decode.app_error", nil, err.Error()) + c.Err.StatusCode = http.StatusBadRequest + return + } + + for _, preference := range preferences { + if c.Session.UserId != preference.UserId { + c.Err = model.NewLocAppError("deletePreferences", "api.preference.delete_preferences.user_id.app_error", + nil, "session.user_id="+c.Session.UserId+",preference.user_id="+preference.UserId) + c.Err.StatusCode = http.StatusForbidden + return + } + } + + for _, preference := range preferences { + if result := <-Srv.Store.Preference().Delete(c.Session.UserId, preference.Category, preference.Name); result.Err != nil { + c.Err = result.Err + return + } + } + + ReturnStatusOK(w) +} diff --git a/api/preference_test.go b/api/preference_test.go index 082f025271..3e41c884f1 100644 --- a/api/preference_test.go +++ b/api/preference_test.go @@ -161,3 +161,49 @@ func TestGetPreference(t *testing.T) { t.Fatal("preference updated incorrectly") } } + +func TestDeletePreferences(t *testing.T) { + th := Setup().InitBasic() + Client := th.BasicClient + user1 := th.BasicUser + + var originalCount int + if result, err := Client.GetAllPreferences(); err != nil { + t.Fatal(err) + } else { + originalCount = len(result.Data.(model.Preferences)) + } + + // save 10 preferences + var preferences model.Preferences + for i := 0; i < 10; i++ { + preference := model.Preference{ + UserId: user1.Id, + Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, + Name: model.NewId(), + } + preferences = append(preferences, preference) + } + + if _, err := Client.SetPreferences(&preferences); err != nil { + t.Fatal(err) + } + + // delete 10 preferences + th.LoginBasic2() + + if _, err := Client.DeletePreferences(&preferences); err == nil { + t.Fatal("shouldn't have been able to delete another user's preferences") + } + + th.LoginBasic() + if _, err := Client.DeletePreferences(&preferences); err != nil { + t.Fatal(err) + } + + if result, err := Client.GetAllPreferences(); err != nil { + t.Fatal(err) + } else if data := result.Data.(model.Preferences); len(data) != originalCount { + t.Fatal("should've deleted preferences") + } +} diff --git a/i18n/en.json b/i18n/en.json index 961ddc50c8..ab6dd4f87f 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -1139,6 +1139,14 @@ "id": "api.post_get_post_by_id.get.app_error", "translation": "Unable to get post" }, + { + "id": "api.preference.delete_preferences.decode.app_error", + "translation": "Unable to decode preferences from request" + }, + { + "id": "api.preference.delete_preferences.user_id.app_error", + "translation": "Unable to delete preferences for other user" + }, { "id": "api.preference.init.debug", "translation": "Initializing preference api routes" @@ -3011,6 +3019,10 @@ "id": "model.preference.is_valid.name.app_error", "translation": "Invalid name" }, + { + "id": "model.preference.is_valid.theme.app_error", + "translation": "Invalid theme" + }, { "id": "model.preference.is_valid.value.app_error", "translation": "Value is too long" @@ -3175,10 +3187,6 @@ "id": "model.user.is_valid.team_id.app_error", "translation": "Invalid team id" }, - { - "id": "model.user.is_valid.theme.app_error", - "translation": "Invalid theme" - }, { "id": "model.user.is_valid.update_at.app_error", "translation": "Update at must be a valid time" @@ -3775,6 +3783,10 @@ "id": "store.sql_post.update.app_error", "translation": "We couldn't update the Post" }, + { + "id": "store.sql_preference.delete.app_error", + "translation": "We encountered an error while deleting preferences" + }, { "id": "store.sql_preference.delete_unused_features.debug", "translation": "Deleting any unused pre-release features" @@ -4055,6 +4067,10 @@ "id": "store.sql_user.get_unread_count.app_error", "translation": "We could not get the unread message count for the user" }, + { + "id": "store.sql_user.migrate_theme.critical", + "translation": "Failed to migrate User.ThemeProps to Preferences table %v" + }, { "id": "store.sql_user.missing_account.const", "translation": "We couldn't find an existing account matching your email address for this team. This team may require an invite from the team owner to join." diff --git a/model/client.go b/model/client.go index 5ccf2c63c1..e12cd595dc 100644 --- a/model/client.go +++ b/model/client.go @@ -1556,6 +1556,16 @@ func (c *Client) GetPreferenceCategory(category string) (*Result, *AppError) { } } +// DeletePreferences deletes a list of preferences owned by the current user. If successful, +// it will return status=ok. Otherwise, an error will be returned. +func (c *Client) DeletePreferences(preferences *Preferences) (bool, *AppError) { + if r, err := c.DoApiPost("/preferences/delete", preferences.ToJson()); err != nil { + return false, err + } else { + return c.CheckStatusOK(r), nil + } +} + func (c *Client) CreateOutgoingWebhook(hook *OutgoingWebhook) (*Result, *AppError) { if r, err := c.DoApiPost(c.GetTeamRoute()+"/hooks/outgoing/create", hook.ToJson()); err != nil { return nil, err diff --git a/model/preference.go b/model/preference.go index 22858e043f..779c41e501 100644 --- a/model/preference.go +++ b/model/preference.go @@ -6,6 +6,8 @@ package model import ( "encoding/json" "io" + "regexp" + "strings" "unicode/utf8" ) @@ -17,6 +19,9 @@ const ( PREFERENCE_CATEGORY_DISPLAY_SETTINGS = "display_settings" PREFERENCE_NAME_COLLAPSE_SETTING = "collapse_previews" + PREFERENCE_CATEGORY_THEME = "theme" + // the name for theme props is the team id + PREFERENCE_CATEGORY_LAST = "last" PREFERENCE_NAME_LAST_CHANNEL = "channel" ) @@ -57,13 +62,48 @@ func (o *Preference) IsValid() *AppError { return NewLocAppError("Preference.IsValid", "model.preference.is_valid.category.app_error", nil, "category="+o.Category) } - if len(o.Name) == 0 || len(o.Name) > 32 { + if len(o.Name) > 32 { return NewLocAppError("Preference.IsValid", "model.preference.is_valid.name.app_error", nil, "name="+o.Name) } - if utf8.RuneCountInString(o.Value) > 128 { + if utf8.RuneCountInString(o.Value) > 2000 { return NewLocAppError("Preference.IsValid", "model.preference.is_valid.value.app_error", nil, "value="+o.Value) } + if o.Category == PREFERENCE_CATEGORY_THEME { + var unused map[string]string + if err := json.NewDecoder(strings.NewReader(o.Value)).Decode(&unused); err != nil { + return NewLocAppError("Preference.IsValid", "model.preference.is_valid.theme.app_error", nil, "value="+o.Value) + } + } + return nil } + +func (o *Preference) PreUpdate() { + if o.Category == PREFERENCE_CATEGORY_THEME { + // decode the value of theme (a map of strings to string) and eliminate any invalid values + var props map[string]string + if err := json.NewDecoder(strings.NewReader(o.Value)).Decode(&props); err != nil { + // just continue, the invalid preference value should get caught by IsValid before saving + return + } + + colorPattern := regexp.MustCompile(`^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$`) + + // blank out any invalid theme values + for name, value := range props { + if name == "image" || name == "type" || name == "codeTheme" { + continue + } + + if !colorPattern.MatchString(value) { + props[name] = "#ffffff" + } + } + + if b, err := json.Marshal(props); err == nil { + o.Value = string(b) + } + } +} diff --git a/model/preference_test.go b/model/preference_test.go index e29250bbaa..df7fe612da 100644 --- a/model/preference_test.go +++ b/model/preference_test.go @@ -4,6 +4,7 @@ package model import ( + "encoding/json" "strings" "testing" ) @@ -31,7 +32,7 @@ func TestPreferenceIsValid(t *testing.T) { preference.Category = PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW if err := preference.IsValid(); err != nil { - t.Fatal() + t.Fatal(err) } preference.Name = strings.Repeat("01234567890", 20) @@ -41,16 +42,48 @@ func TestPreferenceIsValid(t *testing.T) { preference.Name = NewId() if err := preference.IsValid(); err != nil { - t.Fatal() + t.Fatal(err) } - preference.Value = strings.Repeat("01234567890", 20) + preference.Value = strings.Repeat("01234567890", 201) if err := preference.IsValid(); err == nil { t.Fatal() } preference.Value = "1234garbage" if err := preference.IsValid(); err != nil { + t.Fatal(err) + } + + preference.Category = PREFERENCE_CATEGORY_THEME + if err := preference.IsValid(); err == nil { t.Fatal() } + + preference.Value = `{"color": "#ff0000", "color2": "#faf"}` + if err := preference.IsValid(); err != nil { + t.Fatal(err) + } +} + +func TestPreferencePreUpdate(t *testing.T) { + preference := Preference{ + Category: PREFERENCE_CATEGORY_THEME, + Value: `{"color": "#ff0000", "color2": "#faf", "codeTheme": "github", "invalid": "invalid"}`, + } + + preference.PreUpdate() + + var props map[string]string + if err := json.NewDecoder(strings.NewReader(preference.Value)).Decode(&props); err != nil { + t.Fatal(err) + } + + if props["color"] != "#ff0000" || props["color2"] != "#faf" || props["codeTheme"] != "github" { + t.Fatal("shouldn't have changed valid props") + } + + if props["invalid"] == "invalid" { + t.Fatal("should have changed invalid prop") + } } diff --git a/model/user.go b/model/user.go index c792f80d1e..4444352d32 100644 --- a/model/user.go +++ b/model/user.go @@ -49,7 +49,6 @@ type User struct { AllowMarketing bool `json:"allow_marketing,omitempty"` Props StringMap `json:"props,omitempty"` NotifyProps StringMap `json:"notify_props,omitempty"` - ThemeProps StringMap `json:"theme_props,omitempty"` LastPasswordUpdate int64 `json:"last_password_update,omitempty"` LastPictureUpdate int64 `json:"last_picture_update,omitempty"` FailedAttempts int `json:"failed_attempts,omitempty"` @@ -106,10 +105,6 @@ func (u *User) IsValid() *AppError { return NewLocAppError("User.IsValid", "model.user.is_valid.auth_data_pwd.app_error", nil, "user_id="+u.Id) } - if len(u.ThemeProps) > 2000 { - return NewLocAppError("User.IsValid", "model.user.is_valid.theme.app_error", nil, "user_id="+u.Id) - } - return nil } @@ -179,21 +174,6 @@ func (u *User) PreUpdate() { } u.NotifyProps["mention_keys"] = strings.Join(goodKeys, ",") } - - if u.ThemeProps != nil { - colorPattern := regexp.MustCompile(`^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$`) - - // blank out any invalid theme values - for name, value := range u.ThemeProps { - if name == "image" || name == "type" || name == "codeTheme" { - continue - } - - if !colorPattern.MatchString(value) { - u.ThemeProps[name] = "#ffffff" - } - } - } } func (u *User) SetDefaultNotifications() { @@ -282,7 +262,6 @@ func (u *User) ClearNonProfileFields() { u.AllowMarketing = false u.Props = StringMap{} u.NotifyProps = StringMap{} - u.ThemeProps = StringMap{} u.LastPasswordUpdate = 0 u.LastPictureUpdate = 0 u.FailedAttempts = 0 diff --git a/model/user_test.go b/model/user_test.go index 899542a05b..16ac2583b7 100644 --- a/model/user_test.go +++ b/model/user_test.go @@ -39,19 +39,6 @@ func TestUserPreSave(t *testing.T) { func TestUserPreUpdate(t *testing.T) { user := User{Password: "test"} user.PreUpdate() - - user.ThemeProps = StringMap{ - "codeTheme": "github", - "awayIndicator": "#cdbd4e", - "buttonColor": "invalid", - } - user.PreUpdate() - - if user.ThemeProps["codeTheme"] != "github" || user.ThemeProps["awayIndicator"] != "#cdbd4e" { - t.Fatal("shouldn't have changed valid theme props") - } else if user.ThemeProps["buttonColor"] != "#ffffff" { - t.Fatal("should've changed invalid theme prop") - } } func TestUserUpdateMentionKeysFromUsername(t *testing.T) { diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go index 83bf92ead3..a701c3cb8a 100644 --- a/store/sql_preference_store.go +++ b/store/sql_preference_store.go @@ -26,7 +26,7 @@ func NewSqlPreferenceStore(sqlStore *SqlStore) PreferenceStore { table.ColMap("UserId").SetMaxSize(26) table.ColMap("Category").SetMaxSize(32) table.ColMap("Name").SetMaxSize(32) - table.ColMap("Value").SetMaxSize(128) + table.ColMap("Value").SetMaxSize(2000) } return s @@ -100,6 +100,8 @@ func (s SqlPreferenceStore) Save(preferences *model.Preferences) StoreChannel { func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *model.Preference) StoreResult { result := StoreResult{} + preference.PreUpdate() + if result.Err = preference.IsValid(); result.Err != nil { return result } @@ -304,3 +306,26 @@ func (s SqlPreferenceStore) IsFeatureEnabled(feature, userId string) StoreChanne return storeChannel } + +func (s SqlPreferenceStore) Delete(userId, category, name string) StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + if _, err := s.GetMaster().Exec( + `DELETE FROM + Preferences + WHERE + UserId = :UserId + AND Category = :Category + AND Name = :Name`, map[string]interface{}{"UserId": userId, "Category": category, "Name": name}); err != nil { + result.Err = model.NewLocAppError("SqlPreferenceStore.Delete", "store.sql_preference.delete.app_error", nil, err.Error()) + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} diff --git a/store/sql_preference_store_test.go b/store/sql_preference_store_test.go index ec9d1df6ce..8c6a2b6afb 100644 --- a/store/sql_preference_store_test.go +++ b/store/sql_preference_store_test.go @@ -193,7 +193,7 @@ func TestPreferenceGetAll(t *testing.T) { } } -func TestPreferenceDelete(t *testing.T) { +func TestPreferenceDeleteByUser(t *testing.T) { Setup() userId := model.NewId() @@ -367,3 +367,28 @@ func TestDeleteUnusedFeatures(t *testing.T) { t.Fatalf("Found %d features with value 'true', expected to find at least %d features", val, 2) } } + +func TestPreferenceDelete(t *testing.T) { + Setup() + + preference := model.Preference{ + UserId: model.NewId(), + Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, + Name: model.NewId(), + Value: "value1a", + } + + Must(store.Preference().Save(&model.Preferences{preference})) + + if prefs := Must(store.Preference().GetAll(preference.UserId)).(model.Preferences); len([]model.Preference(prefs)) != 1 { + t.Fatal("should've returned 1 preference") + } + + if result := <-store.Preference().Delete(preference.UserId, preference.Category, preference.Name); result.Err != nil { + t.Fatal(result.Err) + } + + if prefs := Must(store.Preference().GetAll(preference.UserId)).(model.Preferences); len([]model.Preference(prefs)) != 0 { + t.Fatal("should've returned no preferences") + } +} diff --git a/store/sql_user_store.go b/store/sql_user_store.go index d4b65d04dd..867445aac2 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -9,7 +9,9 @@ import ( "fmt" "strconv" "strings" + "time" + l4g "github.com/alecthomas/log4go" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" ) @@ -40,7 +42,6 @@ func NewSqlUserStore(sqlStore *SqlStore) UserStore { table.ColMap("Roles").SetMaxSize(64) table.ColMap("Props").SetMaxSize(4000) table.ColMap("NotifyProps").SetMaxSize(2000) - table.ColMap("ThemeProps").SetMaxSize(2000) table.ColMap("Locale").SetMaxSize(5) table.ColMap("MfaSecret").SetMaxSize(128) } @@ -53,27 +54,66 @@ func (us SqlUserStore) UpgradeSchemaIfNeeded() { us.CreateColumnIfNotExists("Users", "Locale", "varchar(5)", "character varying(5)", model.DEFAULT_LOCALE) // ADDED for 3.2 REMOVE for 3.6 - var data []*model.User - if _, err := us.GetReplica().Select(&data, "SELECT * FROM Users WHERE ThemeProps LIKE '%solarized%'"); err == nil { - for _, user := range data { - shouldUpdate := false - if user.ThemeProps["codeTheme"] == "solarized_dark" { - user.ThemeProps["codeTheme"] = "solarized-dark" - shouldUpdate = true - } else if user.ThemeProps["codeTheme"] == "solarized_light" { - user.ThemeProps["codeTheme"] = "solarized-light" - shouldUpdate = true - } + if us.DoesColumnExist("Users", "ThemeProps") { + params := map[string]interface{}{ + "Category": model.PREFERENCE_CATEGORY_THEME, + "Name": "", + } - if shouldUpdate { - if result := <-us.Update(user, true); result.Err != nil { - return - } + transaction, err := us.GetMaster().Begin() + if err != nil { + themeMigrationFailed(err) + } + + // increase size of Value column of Preferences table to match the size of the ThemeProps column + if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { + if _, err := transaction.Exec("ALTER TABLE Preferences ALTER COLUMN Value TYPE varchar(2000)"); err != nil { + themeMigrationFailed(err) + } + } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL { + if _, err := transaction.Exec("ALTER TABLE Preferences MODIFY Value text"); err != nil { + themeMigrationFailed(err) } } + + // copy data across + if _, err := transaction.Exec( + `INSERT INTO + Preferences(UserId, Category, Name, Value) + SELECT + Id, '`+model.PREFERENCE_CATEGORY_THEME+`', '', ThemeProps + FROM + Users`, params); err != nil { + themeMigrationFailed(err) + } + + // delete old data + if _, err := transaction.Exec("ALTER TABLE Users DROP COLUMN ThemeProps"); err != nil { + themeMigrationFailed(err) + } + + if err := transaction.Commit(); err != nil { + themeMigrationFailed(err) + } + + // rename solarized_* code themes to solarized-* to match client changes in 3.0 + var data model.Preferences + if _, err := us.GetReplica().Select(&data, "SELECT * FROM Preferences WHERE Category = '"+model.PREFERENCE_CATEGORY_THEME+"' AND Value LIKE '%solarized_%'"); err == nil { + for i := range data { + data[i].Value = strings.Replace(data[i].Value, "solarized_", "solarized-", -1) + } + + us.Preference().Save(&data) + } } } +func themeMigrationFailed(err error) { + l4g.Critical(utils.T("store.sql_user.migrate_theme.critical"), err) + time.Sleep(time.Second) + panic(fmt.Sprintf(utils.T("store.sql_user.migrate_theme.critical"), err.Error())) +} + func (us SqlUserStore) CreateIndexesIfNotExists() { us.CreateIndexIfNotExists("idx_users_email", "Users", "Email") } diff --git a/store/store.go b/store/store.go index 445de440a5..0c19fd5b6e 100644 --- a/store/store.go +++ b/store/store.go @@ -243,6 +243,7 @@ type PreferenceStore interface { Get(userId string, category string, name string) StoreChannel GetCategory(userId string, category string) StoreChannel GetAll(userId string) StoreChannel + Delete(userId, category, name string) StoreChannel PermanentDeleteByUser(userId string) StoreChannel IsFeatureEnabled(feature, userId string) StoreChannel } diff --git a/webapp/actions/user_actions.jsx b/webapp/actions/user_actions.jsx index 2f6eb9942a..6d14e9fba3 100644 --- a/webapp/actions/user_actions.jsx +++ b/webapp/actions/user_actions.jsx @@ -1,10 +1,15 @@ // Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. -import Client from 'utils/web_client.jsx'; +import AppDispatcher from 'dispatcher/app_dispatcher.jsx'; import * as AsyncClient from 'utils/async_client.jsx'; +import Client from 'utils/web_client.jsx'; +import PreferenceStore from 'stores/preference_store.jsx'; import TeamStore from 'stores/team_store.jsx'; +import UserStore from 'stores/user_store.jsx'; + +import {ActionTypes, Preferences} from 'utils/constants.jsx'; export function switchFromLdapToEmail(email, password, ldapPassword, onSuccess, onError) { Client.ldapToEmail( @@ -28,3 +33,52 @@ export function getMoreDmList() { AsyncClient.getProfilesForDirectMessageList(); AsyncClient.getTeamMembers(TeamStore.getCurrentId()); } + +export function saveTheme(teamId, theme, onSuccess, onError) { + AsyncClient.savePreference( + Preferences.CATEGORY_THEME, + teamId, + JSON.stringify(theme), + () => { + onThemeSaved(teamId, theme, onSuccess); + }, + (err) => { + onError(err); + } + ); +} + +function onThemeSaved(teamId, theme, onSuccess) { + const themePreferences = PreferenceStore.getCategory(Preferences.CATEGORY_THEME); + + if (teamId !== '' && themePreferences.size > 1) { + // no extra handling to be done to delete team-specific themes + onSuccess(); + return; + } + + const toDelete = []; + + for (const [name] of themePreferences) { + if (name === '') { + continue; + } + + toDelete.push({ + user_id: UserStore.getCurrentId(), + category: Preferences.CATEGORY_THEME, + name + }); + } + + // we're saving a new global theme so delete any team-specific ones + AsyncClient.deletePreferences(toDelete); + + // delete them locally before we hear from the server so that the UI flow is smoother + AppDispatcher.handleServerAction({ + type: ActionTypes.DELETED_PREFERENCES, + preferences: toDelete + }); + + onSuccess(); +} \ No newline at end of file diff --git a/webapp/components/logged_in.jsx b/webapp/components/logged_in.jsx index f637e9dc53..2ac858dfb2 100644 --- a/webapp/components/logged_in.jsx +++ b/webapp/components/logged_in.jsx @@ -92,15 +92,6 @@ export default class LoggedIn extends React.Component { id: user.id }); } - - // Update CSS classes to match user theme - if (user) { - if ($.isPlainObject(user.theme_props) && !$.isEmptyObject(user.theme_props)) { - Utils.applyTheme(user.theme_props); - } else { - Utils.applyTheme(Constants.THEMES.default); - } - } } onUserChanged() { diff --git a/webapp/components/needs_team.jsx b/webapp/components/needs_team.jsx index 07b90636d8..a8c7b35082 100644 --- a/webapp/components/needs_team.jsx +++ b/webapp/components/needs_team.jsx @@ -41,19 +41,34 @@ export default class NeedsTeam extends React.Component { constructor(params) { super(params); - this.onChanged = this.onChanged.bind(this); + this.onTeamChanged = this.onTeamChanged.bind(this); + this.onPreferencesChanged = this.onPreferencesChanged.bind(this); + + const team = TeamStore.getCurrent(); this.state = { - team: TeamStore.getCurrent() + team, + theme: PreferenceStore.getTheme(team.id) }; } - onChanged() { + onTeamChanged() { + const team = TeamStore.getCurrent(); + this.setState({ - team: TeamStore.getCurrent() + team, + theme: PreferenceStore.getTheme(team.id) }); } + onPreferencesChanged(category) { + if (!category || category === Preferences.CATEGORY_THEME) { + this.setState({ + theme: PreferenceStore.getTheme(this.state.team.id) + }); + } + } + componentWillMount() { // Go to tutorial if we are first arriving const tutorialStep = PreferenceStore.getInt(Preferences.TUTORIAL_STEP, UserStore.getCurrentId(), 999); @@ -63,7 +78,8 @@ export default class NeedsTeam extends React.Component { } componentDidMount() { - TeamStore.addChangeListener(this.onChanged); + TeamStore.addChangeListener(this.onTeamChanged); + PreferenceStore.addChangeListener(this.onPreferencesChanged); // Emit view action GlobalActions.viewLoggedIn(); @@ -80,10 +96,19 @@ export default class NeedsTeam extends React.Component { $(window).on('blur', () => { window.isActive = false; }); + + Utils.applyTheme(this.state.theme); + } + + componentDidUpdate(prevProps, prevState) { + if (!Utils.areObjectsEqual(prevState.theme, this.state.theme)) { + Utils.applyTheme(this.state.theme); + } } componentWillUnmount() { - TeamStore.removeChangeListener(this.onChanged); + TeamStore.removeChangeListener(this.onTeamChanged); + PreferenceStore.removeChangeListener(this.onPreferencesChanged); $(window).off('focus'); $(window).off('blur'); } diff --git a/webapp/components/setting_item_max.jsx b/webapp/components/setting_item_max.jsx index ec496a765d..ad765a7d69 100644 --- a/webapp/components/setting_item_max.jsx +++ b/webapp/components/setting_item_max.jsx @@ -84,6 +84,7 @@ export default class SettingItemMax extends React.Component {