Skip to content

Commit

Permalink
FIX Make change tracking work again for inline editable blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Feb 19, 2025
1 parent 4c38f21 commit 39b931d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
20 changes: 10 additions & 10 deletions client/dist/js/bundle.js

Large diffs are not rendered by default.

51 changes: 29 additions & 22 deletions client/src/components/ElementEditor/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { inject } from 'lib/Injector';
import i18n from 'i18n';
import classNames from 'classnames';
import { connect } from 'react-redux';
import { submit } from 'redux-form';
import { submit, isDirty } from 'redux-form';
import { loadElementFormStateName } from 'state/editor/loadElementFormStateName';
import { loadElementSchemaValue } from 'state/editor/loadElementSchemaValue';
import { publishBlockMutation } from 'state/editor/publishBlockMutation';
Expand All @@ -20,7 +20,8 @@ import { DragSource, DropTarget } from 'react-dnd';
import { getEmptyImage } from 'react-dnd-html5-backend';
import { elementDragSource, isOverTop } from 'lib/dragHelpers';
import * as toastsActions from 'state/toasts/ToastsActions';
import { addFormChanged, removeFormChanged } from 'state/unsavedForms/UnsavedFormsActions';
// import { addFormChanged, removeFormChanged } from 'state/unsavedForms/UnsavedFormsActions';
import getFormState from 'lib/getFormState';

export const ElementContext = createContext(null);

Expand All @@ -39,7 +40,7 @@ const Element = (props) => {
const [doPublishElementAfterSave, setDoPublishElementAfterSave] = useState(false);
const [ensureFormRendered, setEnsureFormRendered] = useState(false);
const [formHasRendered, setFormHasRendered] = useState(false);
const [doDispatchAddFormChanged, setDoDispatchAddFormChanged] = useState(false);
// const [doDispatchAddFormChanged, setDoDispatchAddFormChanged] = useState(false);
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
const [publishBlock] = useMutation(publishBlockMutation);

Expand Down Expand Up @@ -92,12 +93,12 @@ const Element = (props) => {
}
}, [justClickedPublishButton, formHasRendered]);

useEffect(() => {
if (doDispatchAddFormChanged) {
setDoDispatchAddFormChanged(false);
props.dispatchAddFormChanged();
}
}, [doDispatchAddFormChanged]);
// useEffect(() => {
// if (doDispatchAddFormChanged) {
// setDoDispatchAddFormChanged(false);
// props.dispatchAddFormChanged();
// }
// }, [doDispatchAddFormChanged]);

const getNoTitle = () => i18n.inject(
i18n._t('ElementHeader.NOTITLE', 'Untitled {type} block'),
Expand Down Expand Up @@ -153,7 +154,7 @@ const Element = (props) => {
// there are no hooks/callbacks available after this happens the input onchange handlers are fired
Promise.all(refetchElementalArea())
.then(() => {
setTimeout(() => props.dispatchRemoveFormChanged(), 250);
// setTimeout(() => props.dispatchRemoveFormChanged(), 250);
});
};

Expand Down Expand Up @@ -340,7 +341,7 @@ const Element = (props) => {
// Ensure that formDirty remains truthy
// Note we need to call props.dispatchAddFormChanged() on the next render rather than straight away
// or it will get unset by code somewhere else, probably redux-form
setDoDispatchAddFormChanged(true);
// setDoDispatchAddFormChanged(true);
// Don't accidentally auto publish the element once validation errors are fixed
if (doPublishElementAfterSave) {
setDoPublishElementAfterSave(false);
Expand Down Expand Up @@ -435,6 +436,7 @@ const Element = (props) => {
broken={type.broken}
onFormSchemaSubmitResponse={handleFormSchemaSubmitResponse}
onFormInit={() => handleFormInit(activeTab)}
formDirty={formDirty}
/>
</ElementContext.Provider>
</div>);
Expand Down Expand Up @@ -462,7 +464,10 @@ function mapStateToProps(state, ownProps) {

const tabSetName = tabSet && tabSet.id;
const uniqueFieldId = `element.${elementName}__${tabSetName}`;
const formDirty = state.unsavedForms.find((unsaved) => unsaved.name === `element.${elementName}`);
// const formDirty = state.unsavedForms.find((unsaved) => unsaved.name === `element.${elementName}`);

const formName = loadElementFormStateName(ownProps.element.id);
const formDirty = isDirty(`element.${formName}`, getFormState)(state);

// Find name of the active tab in the tab set
// Only defined once an element form is expanded for the first time
Expand Down Expand Up @@ -490,16 +495,18 @@ function mapDispatchToProps(dispatch, ownProps) {
// Perform a redux-form remote-submit
dispatch(submit(`element.${elementName}`));
},
dispatchAddFormChanged() {
// Ensures the form identifier is in unsavedForms in the global redux state
// This is used to derive the formDirty prop in mapStateToProps
dispatch(addFormChanged(`element.${elementName}`));
},
dispatchRemoveFormChanged() {
// Removes the form identifier from unsavedForms in the global redux store
// Opposite of beheaviour of dispatchAddFormChanged()
dispatch(removeFormChanged(`element.${elementName}`));
},
// dispatchAddFormChanged() {
// console.log('changing');
// // Ensures the form identifier is in unsavedForms in the global redux state
// // This is used to derive the formDirty prop in mapStateToProps
// // dispatch(addFormChanged(`element.${elementName}`));
// },
// dispatchRemoveFormChanged() {
// console.log('unchanging');
// // Removes the form identifier from unsavedForms in the global redux store
// // Opposite of beheaviour of dispatchAddFormChanged()
// // dispatch(removeFormChanged(`element.${elementName}`));
// },
actions: {
toasts: bindActionCreators(toastsActions, dispatch),
},
Expand Down
8 changes: 8 additions & 0 deletions client/src/components/ElementEditor/ElementEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class ElementEditor extends PureComponent {

render() {
const {
fieldName,
formState,
ToolbarComponent,
ListComponent,
areaId,
Expand Down Expand Up @@ -104,6 +106,12 @@ class ElementEditor extends PureComponent {
sharedObject={sharedObject}
/>
<ElementDragPreview elementTypes={elementTypes} />
<input
name={fieldName}
type="hidden"
value={JSON.stringify(formState) || ''}
className="no-change-track"
/>
</div>
);
}
Expand Down

0 comments on commit 39b931d

Please sign in to comment.