Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Adding some page updates after sorting: #553

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion client/src/components/ElementEditor/ElementEditor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global window */

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { inject } from 'lib/Injector';
Expand Down Expand Up @@ -57,7 +59,10 @@ class ElementEditor extends PureComponent {
handleDragEnd(sourceId, afterId) {
const { actions: { handleSortBlock }, areaId } = this.props;

handleSortBlock(sourceId, afterId, areaId);
handleSortBlock(sourceId, afterId, areaId).then(() => {
const preview = window.jQuery('.cms-preview');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought - this doesn't have any handling for where this selector doesn't match. I suppose that means you won't be able to use Elemental outside of the CMS (e.g. in a ModelAdmin). We should be careful to handle edge cases like this I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's reasonable and I agree. While I was doing this I was thinking it would be nice to consolidate a lot of this "entwine hack" code into some lib js file. Preferably available in admin or something. There's a bunch of places we use this code now.

preview.entwine('ss.preview')._loadUrl(preview.find('iframe').attr('src'));
});

this.setState({
dragTargetElementId: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('ElementActions', () => {
{ title: 'Settings', name: 'Settings' },
{ title: 'History', name: 'History' }
];
const editTabsClick = () => {};

describe('renderEditTabs()', () => {
it('should map input tabs into an array of buttons', () => {
Expand All @@ -24,6 +25,7 @@ describe('ElementActions', () => {
areaId={1}
editTabs={testTabs}
ActionMenuComponent={ActionMenuComponent}
handleEditTabsClick={editTabsClick}
/>
);

Expand All @@ -42,6 +44,7 @@ describe('ElementActions', () => {
areaId={1}
editTabs={testTabs}
ActionMenuComponent={ActionMenuComponent}
handleEditTabsClick={editTabsClick}
/>
);

Expand All @@ -60,6 +63,7 @@ describe('ElementActions', () => {
areaId={1}
editTabs={testTabs}
ActionMenuComponent={ActionMenuComponent}
handleEditTabsClick={editTabsClick}
>
<AbstractAction title="some button" />
</ElementActions>
Expand Down
18 changes: 15 additions & 3 deletions client/src/state/editor/sortBlockMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mutation SortBlockMutation($blockId:ID!, $afterBlockId:ID!) {
AfterBlockID: $afterBlockId
) {
ID
IsLiveVersion
IsPublished
}
}
`;
Expand All @@ -24,22 +26,32 @@ const config = {
optimisticResponse: {
sortBlock: {
ID: blockId,
IsLiveVersion: false,
__typename: 'Block',
},
},
update: store => {
update: (store, { data: { sortBlock: updatedElementData } }) => {
const variables = readBlocksConfig.options({ areaId }).variables;
const data = store.readQuery({ query: readBlocksQuery, variables });
const cachedData = store.readQuery({ query: readBlocksQuery, variables });

// Query returns a deeply nested object. Explicit reconstruction via spreads is too verbose.
// This is an alternative, relatively efficient way to deep clone
const newData = JSON.parse(JSON.stringify(data));
const newData = JSON.parse(JSON.stringify(cachedData));
let { edges } = newData.readOneElementalArea.Elements;

// Find the block we reordered
const movedBlockIndex = edges.findIndex(edge => edge.node.ID === blockId);
// Keep it
const movedBlock = edges[movedBlockIndex];
// Update the moved block with the new details returned in the GraphQL response
Object.entries(updatedElementData).forEach(([key, value]) => {
// Skip the type name as this is always returned but should never change
if (key === '__typename') {
return;
}

movedBlock[key] = value;
});
// Remove the moved block
edges.splice(movedBlockIndex, 1);
// If the target is 0, it's added to the start
Expand Down