Skip to content

Commit

Permalink
ENH Refactor Element
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Apr 23, 2024
1 parent e5cc486 commit c825bb3
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 39 deletions.
18 changes: 9 additions & 9 deletions client/dist/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/src/components/ElementActions/PublishAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { compose } from 'redux';
import AbstractAction from 'components/ElementActions/AbstractAction';
import publishBlockMutation from 'state/editor/publishBlockMutation';
import i18n from 'i18n';
import { ElementContext } from 'components/ElementEditor/Element';
import { ElementContext } from 'components/ElementEditor/ElementContext';

/**
* Adds the elemental menu action to publish a draft/modified block
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/ElementActions/SaveAction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useContext, useEffect } from 'react';
import AbstractAction from 'components/ElementActions/AbstractAction';
import i18n from 'i18n';
import { ElementContext } from 'components/ElementEditor/Element';
import { ElementContext } from 'components/ElementEditor/ElementContext';

const SaveAction = (MenuComponent) => (props) => {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';
import { fireEvent, render } from '@testing-library/react';
import { Component as PublishAction } from '../PublishAction';
import { ElementContext } from '../../ElementEditor/Element';
import { ElementContext } from '../../ElementEditor/ElementContext';

window.jQuery = {
noticeAdd: () => null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import React from 'react';
import { fireEvent, render } from '@testing-library/react';
import { Component as SaveAction } from '../SaveAction';
import { ElementContext } from '../../ElementEditor/Element';
import { ElementContext } from '../../ElementEditor/ElementContext';

function makeProps(obj = {}) {
return {
Expand Down
16 changes: 2 additions & 14 deletions client/src/components/ElementEditor/Content.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { inject } from 'lib/Injector';
import { compose } from 'redux';
import { connect } from 'react-redux';
import { loadElementFormStateName } from 'state/editor/loadElementFormStateName';
import { isDirty } from 'redux-form';
import getFormState from 'lib/getFormState';

class Content extends PureComponent {
render() {
Expand Down Expand Up @@ -87,18 +83,11 @@ Content.propTypes = {
onFormInit: PropTypes.func,
ensureFormRendered: PropTypes.bool,
formHasRendered: PropTypes.bool,
formDirty: PropTypes.object,
};

Content.defaultProps = {};

function mapStateToProps(state, ownProps) {
const formName = loadElementFormStateName(ownProps.id);

return {
formDirty: isDirty(`element.${formName}`, getFormState)(state),
};
}

export { Content as Component };

export default compose(
Expand All @@ -108,6 +97,5 @@ export default compose(
SummaryComponent, InlineEditFormComponent,
}),
() => 'ElementEditor.ElementList.Element'
),
connect(mapStateToProps)
)
)(Content);
27 changes: 15 additions & 12 deletions client/src/components/ElementEditor/Element.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global window */

import React, { createContext, useState, useEffect } from 'react';
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { elementType } from 'types/elementType';
import { elementTypeType } from 'types/elementTypeType';
Expand All @@ -12,14 +12,14 @@ import { connect } from 'react-redux';
import { submit } from 'redux-form';
import { loadElementFormStateName } from 'state/editor/loadElementFormStateName';
import { loadElementSchemaValue } from 'state/editor/loadElementSchemaValue';
import { query as readBlocksForAreaQuery } from 'state/editor/readBlocksForAreaQuery';
import * as TabsActions from 'state/tabs/TabsActions';
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';

export const ElementContext = createContext(null);
import { ElementContext } from 'components/ElementEditor/ElementContext';

/**
* The Element component used in the context of an ElementEditor shows the summary
Expand Down Expand Up @@ -188,11 +188,14 @@ const Element = (props) => {
}
};

const refetchElementalArea = () => {
// This will trigger a graphql readOneElementalArea request that will cause this
// element to re-render including any updated title and versioned badge
window.ss.apolloClient.queryManager.reFetchObservableQueries();
};
// This will triggre a graphql request that will cause this
// element to re-render including any updated title and versioned badge
const refetchElementalArea = () => window.ss.apolloClient.queryManager.refetchQueries({
include: [{
query: readBlocksForAreaQuery,
variables: { id: props.areaId }
}]
});

/**
* Update the active tab on tab actions menu button click event. Is passed down to InlineEditForm.
Expand Down Expand Up @@ -271,10 +274,10 @@ const Element = (props) => {
// time under certain conditions, specifically during a behat test when trying to publish a closed
// block when presumably the apollo cache is empty (or something like that). This happens late and
// there are no hooks/callbacks available after this happens the input onchange handlers are fired
setTimeout(() => {
props.dispatchRemoveFormChanged();
}, 500);
refetchElementalArea();
Promise.all(refetchElementalArea())
.then(() => {
setTimeout(() => props.dispatchRemoveFormChanged(), 500);
});
};

const handleFormInit = (activeTab) => {
Expand Down
3 changes: 3 additions & 0 deletions client/src/components/ElementEditor/ElementContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createContext } from 'react';

export const ElementContext = createContext(null);
1 change: 1 addition & 0 deletions tests/Behat/features/publish-block-element.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Feature: Publish elements in the CMS
And I should see "Block B"

@modal
@sboyd
Scenario: I can publish a block
Given I see a list of blocks
When I press the "View actions" button
Expand Down

0 comments on commit c825bb3

Please sign in to comment.