Skip to content

Commit

Permalink
fix JS error when saving a custom theme vector (mattermost#6734)
Browse files Browse the repository at this point in the history
  • Loading branch information
saturninoabril authored and enahum committed Jun 23, 2017
1 parent 334f84d commit a092e45
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
17 changes: 7 additions & 10 deletions actions/user_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -383,21 +383,18 @@ export function loadProfilesForDM() {
}
}

export function saveTheme(teamId, theme, onSuccess, onError) {
export function saveTheme(teamId, theme, cb) {
const currentUserId = UserStore.getCurrentId();
savePreferences(currentUserId, [{
const preference = [{
user_id: currentUserId,
category: Preferences.CATEGORY_THEME,
name: teamId,
value: JSON.stringify(theme)
}])(dispatch, getState).then(
(data) => {
if (data && onSuccess) {
onThemeSaved(teamId, theme, onSuccess);
} else if (data == null && onError) {
const serverError = getState().requests.users.savePreferences.error;
onError({id: serverError.server_error_id, ...serverError});
}
}];

savePreferences(currentUserId, preference)(dispatch, getState).then(
() => {
onThemeSaved(teamId, theme, cb);
}
);
}
Expand Down
39 changes: 31 additions & 8 deletions components/user_settings/custom_theme_chooser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ const messages = defineMessages({
const HEX_CODE_LENGTH = 7;

class CustomThemeChooser extends React.Component {
constructor(props) {
super(props);
this.selectTheme = this.selectTheme.bind(this);

const copyTheme = Object.assign({}, this.props.theme);
delete copyTheme.type;
delete copyTheme.image;

this.state = {
copyTheme: JSON.stringify(copyTheme)
};
}

componentDidMount() {
$('.color-picker').colorpicker({
format: 'hex'
Expand Down Expand Up @@ -144,9 +157,11 @@ class CustomThemeChooser extends React.Component {
}

const theme = this.props.theme;
theme[e.target.id] = e.color.toHex();
theme.type = 'custom';
this.props.updateTheme(theme);
if (theme[e.target.id] !== e.color.toHex()) {
theme[e.target.id] = e.color.toHex();
theme.type = 'custom';
this.props.updateTheme(theme);
}
}

pasteBoxChange = (e) => {
Expand All @@ -169,6 +184,10 @@ class CustomThemeChooser extends React.Component {
return;
}

this.setState({
copyTheme: JSON.stringify(theme)
});

theme.type = 'custom';
this.props.updateTheme(theme);
}
Expand All @@ -177,6 +196,12 @@ class CustomThemeChooser extends React.Component {
e.stopPropagation();
}

selectTheme() {
const textarea = this.refs.textarea;
textarea.focus();
textarea.setSelectionRange(0, this.state.copyTheme.length);
}

toggleSidebarStyles = (e) => {
e.preventDefault();

Expand Down Expand Up @@ -346,10 +371,6 @@ class CustomThemeChooser extends React.Component {
}
});

const copyTheme = Object.assign({}, theme);
delete copyTheme.type;
delete copyTheme.image;

const pasteBox = (
<div className='col-sm-12'>
<label className='custom-label'>
Expand All @@ -359,10 +380,12 @@ class CustomThemeChooser extends React.Component {
/>
</label>
<textarea
ref='textarea'
className='form-control'
value={JSON.stringify(copyTheme)}
value={this.state.copyTheme}
onPaste={this.pasteBoxChange}
onChange={this.onChangeHandle}
onClick={this.selectTheme}
/>
</div>
);
Expand Down
5 changes: 0 additions & 5 deletions components/user_settings/user_settings_theme.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@ export default class ThemeSetting extends React.Component {
this.originalTheme = Object.assign({}, this.state.theme);
this.scrollToTop();
this.props.updateSection('');
},
(err) => {
var state = this.getStateFromStores();
state.serverError = err;
this.setState(state);
}
);
}
Expand Down

0 comments on commit a092e45

Please sign in to comment.