Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 1, 2024
1 parent b55ae34 commit 91db11b
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 44 deletions.
64 changes: 45 additions & 19 deletions client/dist/js/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 36 additions & 14 deletions client/src/components/ElementEditor/ElementEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ElementEditor extends PureComponent {
dragTargetElementId: null,
dragSpot: null,
contentBlocks: null,
isLoading: false,
isLoading: true,
};

this.handleDragOver = this.handleDragOver.bind(this);
Expand Down Expand Up @@ -61,11 +61,10 @@ class ElementEditor extends PureComponent {
* @param afterId
*/
handleDragEnd(sourceId, afterId) {
const { actions: { handleSortBlock }, areaId } = this.props;

const globalUseGraphQL = true;
const globalUseGraphQL = false;
if (globalUseGraphQL) {
// see sortBlockMutation.js for reference
const { actions: { handleSortBlock }, areaId } = this.props;
handleSortBlock(sourceId, afterId, areaId).then(() => {
const preview = window.jQuery('.cms-preview');
preview.entwine('ss.preview')._loadUrl(preview.find('iframe').attr('src'));
Expand All @@ -77,6 +76,19 @@ class ElementEditor extends PureComponent {
// call to read the element that was moved
// (strange code for sorting is in this component and not ElementList, however do not refator it)
// update the preview via jquery/entwine (see graphql code above)
backend.post(`/admin/elemental-area/sort`, {
ID: sourceId,
afterBlockID: afterId,
})
.then(response => response.json())
.then(responseJson => {
console.log(responseJson);
// this.setState({
// ...this.state,
// contentBlocks: responseJson,
// isLoading: false,
// })
});
}

this.setState({
Expand All @@ -89,11 +101,13 @@ class ElementEditor extends PureComponent {
* # rpc
* make a call to readAll elements endpoint (areaID)
*/
fetchBlocks() {
this.setState({
...this.state,
isLoading: true
});
fetchBlocks(doSetLoadingState = true) {
if (doSetLoadingState) {
this.setState({
...this.state,
isLoading: true,
});
}
backend.get(`/admin/elemental-area/readBlocks/${this.props.areaId}`)
.then(response => response.json())
.then(responseJson => {
Expand All @@ -116,12 +130,13 @@ class ElementEditor extends PureComponent {
isDraggingOver,
connectDropTarget,
allowedElements,
isLoading,
} = this.props;
const { dragTargetElementId, dragSpot, contentBlocks } = this.state;

const globalUseGraphqQL = false;
if (!globalUseGraphqQL && contentBlocks === null) {
this.fetchBlocks();
this.fetchBlocks(false);
}

// Map the allowed elements because we want to retain the sort order provided by that array.
Expand All @@ -148,6 +163,7 @@ class ElementEditor extends PureComponent {
isDraggingOver={isDraggingOver}
dragTargetElementId={dragTargetElementId}
contentBlocks={contentBlocks}
isLoading={isLoading}
/>
<ElementDragPreview elementTypes={elementTypes} />
<input
Expand Down Expand Up @@ -191,7 +207,8 @@ function mapStateToProps(state) {
}

export { ElementEditor as Component };
export default compose(

const params = [
withDragDropContext,
DropTarget('element', {}, (connector, monitor) => ({
connectDropTarget: connector.dropTarget(),
Expand All @@ -205,7 +222,12 @@ export default compose(
ListComponent,
}),
() => 'ElementEditor'
),
sortBlockMutation
)(ElementEditor);
)
];
const globalUseGraphQL = false;
if (globalUseGraphQL) {
params.push(sortBlockMutation);
}

export default compose(...params)(ElementEditor);

9 changes: 6 additions & 3 deletions client/src/components/ElementEditor/ElementList.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,23 @@ export { ElementList as Component };

const elementListTarget = {
drop(props, monitor) {
const { blocks } = props;
const { blocks, contentBlocks } = props;
const globalUseGraphQL = false;
const elements = globalUseGraphQL ? blocks : contentBlocks;

const elementTargetDropResult = monitor.getDropResult();

if (!elementTargetDropResult) {
return {};
}

const dropIndex = getDragIndicatorIndex(
blocks.map(element => element.id),
elements.map(element => element.id),
elementTargetDropResult.target,
monitor.getItem(),
elementTargetDropResult.dropSpot,
);
const dropAfterID = blocks[dropIndex - 1] ? blocks[dropIndex - 1].id : '0';
const dropAfterID = elements[dropIndex - 1] ? elements[dropIndex - 1].id : '0';

return {
...elementTargetDropResult,
Expand Down
42 changes: 34 additions & 8 deletions src/Controllers/ElementalAreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ElementalAreaController extends CMSMain
//
'GET readBlocks/$elementalAreaID!' => 'readBlocks',
'POST add' => 'add',
'POST sort' => 'sort',
];

private static $allowed_actions = [
Expand All @@ -49,10 +50,40 @@ class ElementalAreaController extends CMSMain
//a
'readBlocks',
'add',
'sort'
];

private function jsonResponse(?array $data = null)
{
$response = $this->getResponse();
$response->setStatusCode(200);
$response->addHeader('Content-Type', 'application/json');
$body = $data ? json_encode($data) : '';
$response->setBody($body);
return $response;
}

// ===

public function sort()
{
$request = $this->getRequest();
$postVars = json_decode($request->getBody(), true);
$id = $postVars['ID'];
$afterBlockID = $postVars['afterBlockID'];
$element = BaseElement::get()->byID($id);
if (!$element) {
throw new InvalidArgumentException(sprintf('%s#%s not found', BaseElement::class, $id));
}
if (!$element->canEdit()) {
$message = 'Changing the sort order of blocks is not allowed for the current user';
throw new InvalidArgumentException($message);
}
$reorderingService = Injector::inst()->create(ReorderElements::class, $element);
$reorderingService->reorder($afterBlockID);
return $this->jsonResponse(null);
}

public function readBlocks()
{
$request = $this->getRequest();
Expand All @@ -67,7 +98,7 @@ public function readBlocks()
if (!$elementalArea->canView()) {
throw new InvalidArgumentException("The current user has insufficient permission to view ElementalArea");
}
$json = [];
$data = [];
foreach ($elementalArea->Elements() as $element) {
if (!$element->canView()) {
continue;
Expand All @@ -84,7 +115,7 @@ public function readBlocks()
],
'content' => '',
];
$json[] = [
$data[] = [
'id' => (string) $element->ID,
'title' => $element->Title,
'__typename' => 'Block', // todo
Expand All @@ -100,12 +131,7 @@ public function readBlocks()
'canCreate' => $element->canCreate(),
];
}
// $json['__typename'] = 'ElementalArea'; // this should get removed
$response = $this->getResponse();
$response->setStatusCode(200);
$response->addHeader('Content-Type', 'application/json');
$response->setBody(json_encode($json));
return $response;
return $this->jsonResponse($data);
}

// Resolver.php resolveAddElementToArea()
Expand Down

0 comments on commit 91db11b

Please sign in to comment.