From e78d8d59b62e4631aab7cfb446e6161482f56aae Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 7 Jun 2020 16:40:56 -0500 Subject: [PATCH] Compose: Fix content retention on close, fixes #139 --- app/soapbox/components/modal_root.js | 20 +++++++++++++++----- app/soapbox/reducers/compose.js | 15 +++------------ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/soapbox/components/modal_root.js b/app/soapbox/components/modal_root.js index 1f7f2f1d1..17cdaa64a 100644 --- a/app/soapbox/components/modal_root.js +++ b/app/soapbox/components/modal_root.js @@ -9,8 +9,18 @@ const messages = defineMessages({ confirm: { id: 'confirmations.delete.confirm', defaultMessage: 'Delete' }, }); +const checkComposeContent = compose => { + return [ + compose.get('text').length > 0, + compose.get('spoiler_text').length > 0, + compose.get('media_attachments').size > 0, + compose.get('in_reply_to') !== null, + compose.get('poll') !== null, + ].some(check => check === true); +}; + const mapStateToProps = state => ({ - composeText: state.getIn(['compose', 'text']), + hasComposeContent: checkComposeContent(state.get('compose')), }); const mapDispatchToProps = (dispatch) => ({ @@ -30,7 +40,7 @@ class ModalRoot extends React.PureComponent { onOpenModal: PropTypes.func.isRequired, onCancelReplyCompose: PropTypes.func.isRequired, intl: PropTypes.object.isRequired, - composeText: PropTypes.string, + hasComposeContent: PropTypes.bool, type: PropTypes.string, }; @@ -48,16 +58,16 @@ class ModalRoot extends React.PureComponent { } handleOnClose = () => { - const { onOpenModal, composeText, intl, type, onCancelReplyCompose } = this.props; + const { onOpenModal, hasComposeContent, intl, type, onCancelReplyCompose } = this.props; - if (composeText && type === 'COMPOSE') { + if (hasComposeContent && type === 'COMPOSE') { onOpenModal('CONFIRM', { message: , confirm: intl.formatMessage(messages.confirm), onConfirm: () => onCancelReplyCompose(), onCancel: () => onOpenModal('COMPOSE'), }); - } else if (composeText && type === 'CONFIRM') { + } else if (hasComposeContent && type === 'CONFIRM') { onOpenModal('COMPOSE'); } else { this.props.onClose(); diff --git a/app/soapbox/reducers/compose.js b/app/soapbox/reducers/compose.js index e1fbc2a35..d2d1f5961 100644 --- a/app/soapbox/reducers/compose.js +++ b/app/soapbox/reducers/compose.js @@ -99,7 +99,7 @@ function clearAll(state) { map.set('in_reply_to', null); map.set('privacy', state.get('default_privacy')); map.set('sensitive', false); - map.update('media_attachments', list => list.clear()); + map.set('media_attachments', ImmutableList()); map.set('poll', null); map.set('idempotencyKey', uuid()); }); @@ -257,21 +257,12 @@ export default function compose(state = initialState, action) { map.set('spoiler_text', ''); } }); - case COMPOSE_REPLY_CANCEL: - case COMPOSE_RESET: - return state.withMutations(map => { - map.set('in_reply_to', null); - map.set('text', ''); - map.set('spoiler', false); - map.set('spoiler_text', ''); - map.set('privacy', state.get('default_privacy')); - map.set('poll', null); - map.set('idempotencyKey', uuid()); - }); case COMPOSE_SUBMIT_REQUEST: return state.set('is_submitting', true); case COMPOSE_UPLOAD_CHANGE_REQUEST: return state.set('is_changing_upload', true); + case COMPOSE_REPLY_CANCEL: + case COMPOSE_RESET: case COMPOSE_SUBMIT_SUCCESS: return clearAll(state); case COMPOSE_SUBMIT_FAIL: