diff --git a/web/modals/threads/thread-settings-modal.react.js b/web/modals/threads/thread-settings-modal.react.js --- a/web/modals/threads/thread-settings-modal.react.js +++ b/web/modals/threads/thread-settings-modal.react.js @@ -92,19 +92,15 @@ +setAccountPassword: SetState, +currentTabType: TabType, +setCurrentTabType: SetState, -}; -type State = { +queuedChanges: ThreadChanges, + +setQueuedChanges: SetState, }; -class ThreadSettingsModal extends React.PureComponent { +class ThreadSettingsModal extends React.PureComponent { nameInput: ?HTMLInputElement; accountPasswordInput: ?HTMLInputElement; constructor(props: Props) { super(props); - this.state = { - queuedChanges: Object.freeze({}), - }; } componentDidMount() { @@ -150,10 +146,10 @@ possiblyChangedValue(key: string) { const valueChanged = - this.state.queuedChanges[key] !== null && - this.state.queuedChanges[key] !== undefined; + this.props.queuedChanges[key] !== null && + this.props.queuedChanges[key] !== undefined; return valueChanged - ? this.state.queuedChanges[key] + ? this.props.queuedChanges[key] : this.props.threadInfo[key]; } @@ -172,7 +168,7 @@ value => value !== null && value !== undefined, // the lodash/fp libdef coerces the returned object's properties to the // same type, which means it only works for object-as-maps $FlowFixMe - )(this.state.queuedChanges), + )(this.props.queuedChanges), ).length > 0 ); } @@ -320,13 +316,10 @@ const target = event.currentTarget; const newValue = target.value !== this.props.threadInfo.name ? target.value : undefined; - this.setState((prevState: State) => ({ - ...prevState, - queuedChanges: { - ...prevState.queuedChanges, - name: firstLine(newValue), - }, - })); + this.props.setQueuedChanges({ + ...this.props.queuedChanges, + name: firstLine(newValue), + }); }; onChangeDescription = (event: SyntheticEvent) => { @@ -335,37 +328,28 @@ target.value !== this.props.threadInfo.description ? target.value : undefined; - this.setState((prevState: State) => ({ - ...prevState, - queuedChanges: { - ...prevState.queuedChanges, - description: newValue, - }, - })); + this.props.setQueuedChanges({ + ...this.props.queuedChanges, + description: newValue, + }); }; onChangeColor = (color: string) => { const newValue = color !== this.props.threadInfo.color ? color : undefined; - this.setState((prevState: State) => ({ - ...prevState, - queuedChanges: { - ...prevState.queuedChanges, - color: newValue, - }, - })); + this.props.setQueuedChanges({ + ...this.props.queuedChanges, + color: newValue, + }); }; onChangeThreadType = (event: SyntheticEvent) => { const uiValue = assertThreadType(parseInt(event.currentTarget.value, 10)); const newValue = uiValue !== this.props.threadInfo.type ? uiValue : undefined; - this.setState((prevState: State) => ({ - ...prevState, - queuedChanges: { - ...prevState.queuedChanges, - type: newValue, - }, - })); + this.props.setQueuedChanges({ + ...this.props.queuedChanges, + type: newValue, + }); }; onChangeAccountPassword = (event: SyntheticEvent) => { @@ -385,7 +369,7 @@ try { const response = await this.props.changeThreadSettings({ threadID: this.props.threadInfo.id, - changes: this.state.queuedChanges, + changes: this.props.queuedChanges, }); this.props.onClose(); return response; @@ -393,16 +377,9 @@ this.props.setErrorMessage('unknown error'); this.props.setAccountPassword(''); this.props.setCurrentTabType('general'); - this.setState( - prevState => ({ - ...prevState, - queuedChanges: Object.freeze({}), - }), - () => { - invariant(this.nameInput, 'nameInput ref unset'); - this.nameInput.focus(); - }, - ); + this.props.setQueuedChanges(Object.freeze({})); + invariant(this.nameInput, 'nameInput ref unset'); + this.nameInput.focus(); throw e; } } @@ -467,6 +444,9 @@ const [currentTabType, setCurrentTabType] = React.useState( 'general', ); + const [queuedChanges, setQueuedChanges] = React.useState( + Object.freeze({}), + ); if (!threadInfo) { return ( @@ -495,6 +475,8 @@ setAccountPassword={setAccountPassword} currentTabType={currentTabType} setCurrentTabType={setCurrentTabType} + queuedChanges={queuedChanges} + setQueuedChanges={setQueuedChanges} /> ); },