From ddf3ceffe7b48c60c1e1668c38b2d8393e411289 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 1 Jan 2024 14:18:13 +1300 Subject: [PATCH] . --- client/dist/js/bundle.js | 17 ++++++ .../ElementEditor/AddElementPopover.js | 2 +- .../components/ElementEditor/ElementEditor.js | 16 ++++++ src/Controllers/ElementalAreaController.php | 55 ++++++++++++++++++- 4 files changed, 88 insertions(+), 2 deletions(-) diff --git a/client/dist/js/bundle.js b/client/dist/js/bundle.js index 14770334..1fb4dccd 100644 --- a/client/dist/js/bundle.js +++ b/client/dist/js/bundle.js @@ -2114,6 +2114,10 @@ var _withDragDropContext = __webpack_require__(21); var _withDragDropContext2 = _interopRequireDefault(_withDragDropContext); +var _Backend = __webpack_require__(12); + +var _Backend2 = _interopRequireDefault(_Backend); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } @@ -2176,6 +2180,13 @@ var ElementEditor = function (_PureComponent) { dragSpot: null }); } + }, { + key: 'fetchBlocks', + value: function fetchBlocks() { + _Backend2.default.get('/admin/elemental-area/readBlocks/' + this.props.areaId).then(function (response) { + console.log('readBlocks', response); + }); + } }, { key: 'render', value: function render() { @@ -2193,6 +2204,12 @@ var ElementEditor = function (_PureComponent) { dragTargetElementId = _state.dragTargetElementId, dragSpot = _state.dragSpot; + + var globalUseGraphqQL = true; + if (globalUseGraphqQL) { + this.fetchBlocks(); + } + var allowedElementTypes = allowedElements.map(function (className) { return elementTypes.find(function (type) { return type.class === className; diff --git a/client/src/components/ElementEditor/AddElementPopover.js b/client/src/components/ElementEditor/AddElementPopover.js index 72121353..a3bbf0c5 100644 --- a/client/src/components/ElementEditor/AddElementPopover.js +++ b/client/src/components/ElementEditor/AddElementPopover.js @@ -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, diff --git a/client/src/components/ElementEditor/ElementEditor.js b/client/src/components/ElementEditor/ElementEditor.js index f9c9284f..fc944ca3 100644 --- a/client/src/components/ElementEditor/ElementEditor.js +++ b/client/src/components/ElementEditor/ElementEditor.js @@ -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 @@ -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, @@ -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) diff --git a/src/Controllers/ElementalAreaController.php b/src/Controllers/ElementalAreaController.php index 1e350970..2d771c4a 100644 --- a/src/Controllers/ElementalAreaController.php +++ b/src/Controllers/ElementalAreaController.php @@ -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 @@ -36,6 +36,7 @@ class ElementalAreaController extends CMSMain 'POST api/saveForm/$ID' => 'apiSaveForm', '$FormName/field/$FieldName' => 'formAction', // + 'GET readBlocks/$elementalAreaID!' => 'readBlocks', 'POST add' => 'add', ]; @@ -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() {