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 7643e54 commit ddf3cef
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
17 changes: 17 additions & 0 deletions client/dist/js/bundle.js

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

2 changes: 1 addition & 1 deletion client/src/components/ElementEditor/AddElementPopover.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AddElementPopover extends Component {
getElementButtonClickHandler(elementType) {
return (event) => {
event.preventDefault();
backend.post(`/admin/elemental-area/add/`,{
backend.post(`/admin/elemental-area/add/`, {
elementClass: elementType.class,
elementalAreaID: this.props.areaId,
insertAfterElementID: this.props.insertAfterElement,
Expand Down
16 changes: 16 additions & 0 deletions client/src/components/ElementEditor/ElementEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { DropTarget } from 'react-dnd';
import sortBlockMutation from 'state/editor/sortBlockMutation';
import ElementDragPreview from 'components/ElementEditor/ElementDragPreview';
import withDragDropContext from 'lib/withDragDropContext';
import backend from 'lib/Backend';

/**
* The ElementEditor is used in the CMS to manage a list or nested lists of
Expand Down Expand Up @@ -79,6 +80,16 @@ class ElementEditor extends PureComponent {
});
}

fetchBlocks() {
// # rpc
// todo
// make a call to readAll elements endpoint (areaID)
backend.get(`/admin/elemental-area/readBlocks/${this.props.areaId}`)
.then((response) => {
console.log('readBlocks', response);
});
}

render() {
const {
fieldName,
Expand All @@ -93,6 +104,11 @@ class ElementEditor extends PureComponent {
} = this.props;
const { dragTargetElementId, dragSpot } = this.state;

const globalUseGraphqQL = true;
if (globalUseGraphqQL) {
this.fetchBlocks();
}

// Map the allowed elements because we want to retain the sort order provided by that array.
const allowedElementTypes = allowedElements.map(className =>
elementTypes.find(type => type.class === className)
Expand Down
55 changes: 54 additions & 1 deletion src/Controllers/ElementalAreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use SilverStripe\Security\SecurityToken;
use InvalidArgumentException;
use DNADesign\Elemental\Models\ElementalArea;
use SilverStripe\GraphQL\QueryHandler\UserContextProvider;
use DNADesign\Elemental\Services\ReorderElements;
use SilverStripe\Control\Controller;

/**
* Controller for "ElementalArea" - handles loading and saving of in-line edit forms in an elemental area in admin
Expand All @@ -36,6 +36,7 @@ class ElementalAreaController extends CMSMain
'POST api/saveForm/$ID' => 'apiSaveForm',
'$FormName/field/$FieldName' => 'formAction',
//
'GET readBlocks/$elementalAreaID!' => 'readBlocks',
'POST add' => 'add',
];

Expand All @@ -45,11 +46,63 @@ class ElementalAreaController extends CMSMain
'apiSaveForm',
'formAction',
//a
'readBlocks',
'add',
];

// ===

public function readBlocks()
{
$request = $this->getRequest();
$elementalAreaID = $request->param('elementalAreaID');

// validate post vars
$elementalArea = ElementalArea::get()->byID($elementalAreaID);
if (!$elementalArea) {
throw new InvalidArgumentException("Invalid ElementalAreaID: $elementalAreaID");
}
// permission checks
if (!$elementalArea->canView()) {
throw new InvalidArgumentException("The current user has insufficient permission to view ElementalArea");
}
$json = [];
foreach ($elementalArea->Elements() as $element) {
if (!$element->canView()) {
continue;
}
$typeName = str_replace('//', '_', get_class($element)); // todo obsolete class name
// should probably be able to just red rid of this
$blockSchema = [
'typeName' => $typeName,
'actions' => [
'edit' => Controller::join_links(BASE_URL, "/admin/pages/edit/show/4") // todo pageID
],
'content' => '',
];
$json[] = [
'id' => $element->ID,
'title' => $element->Title,
'type' => $element->Type,
'blockSchema' => $blockSchema,
'obsoleteClassName' => null, // todo
'version' => 1, // todo
'isPublished' => false, // todo
'isLiveVersion' => false, // todo
// 'canEdit' => $element->canEdit(), // not in graphql response
'canDelete' => $element->canDelete(),
'canPublish' => $element->canPublish(),
'canUnpublish' => $element->canUnpublish(),
'canCreate' => $element->canCreate(),
];
}
$response = $this->getResponse();
$response->setStatusCode(200);
$response->addHeader('Content-Type', 'application/json');
$response->setBody(json_encode($json));
return $response;
}

// Resolver.php resolveAddElementToArea()
public function add()
{
Expand Down

0 comments on commit ddf3cef

Please sign in to comment.