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 c417ab4 commit 7643e54
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 15 deletions.
42 changes: 30 additions & 12 deletions client/dist/js/bundle.js

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

22 changes: 19 additions & 3 deletions client/src/components/ElementEditor/AddElementPopover.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import classNames from 'classnames';
import { inject } from 'lib/Injector';
import { elementTypeType } from 'types/elementTypeType';
import i18n from 'i18n';
import backend from 'lib/Backend';

/**
* The AddElementPopover component used in the context of an ElementEditor shows the
Expand All @@ -27,13 +28,13 @@ class AddElementPopover extends Component {
*/
getGraphQLElementButtonClickHandler(elementType) {
return (event) => {
// handleAddElementToArea comes from addElementMutation.js
const {
actions: { handleAddElementToArea },
insertAfterElement
} = this.props;

event.preventDefault();
// TODO This should probably use the GraphQL element type name (element.__typeName)
handleAddElementToArea(elementType.class, insertAfterElement).then(
() => {
const preview = window.jQuery('.cms-preview');
Expand All @@ -51,7 +52,22 @@ class AddElementPopover extends Component {
* - also then update the preview via jquery/entwine
*/
getElementButtonClickHandler(elementType) {
// todo
return (event) => {
event.preventDefault();
backend.post(`/admin/elemental-area/add/`,{
elementClass: elementType.class,
elementalAreaID: this.props.areaId,
insertAfterElementID: this.props.insertAfterElement,
})
.then(() => {
// todo call read blocks from area endpoint (areaID)
})
.then(() => {
const preview = window.jQuery('.cms-preview');
preview.entwine('ss.preview')._loadUrl(preview.find('iframe').attr('src'));
});
this.handleToggle();
};
}

/**
Expand All @@ -78,7 +94,7 @@ class AddElementPopover extends Component {
extraClass
);

const globalUseGraphQL = true;
const globalUseGraphQL = false;

const buttons = elementTypes.map((elementType) => ({
content: elementType.title,
Expand Down
61 changes: 61 additions & 0 deletions src/Controllers/ElementalAreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\Form;
use SilverStripe\Security\SecurityToken;
use InvalidArgumentException;
use DNADesign\Elemental\Models\ElementalArea;
use SilverStripe\GraphQL\QueryHandler\UserContextProvider;
use DNADesign\Elemental\Services\ReorderElements;

/**
* Controller for "ElementalArea" - handles loading and saving of in-line edit forms in an elemental area in admin
Expand All @@ -31,15 +35,72 @@ class ElementalAreaController extends CMSMain
// API access points with structured data
'POST api/saveForm/$ID' => 'apiSaveForm',
'$FormName/field/$FieldName' => 'formAction',
//
'POST add' => 'add',
];

private static $allowed_actions = [
'elementForm',
'schema',
'apiSaveForm',
'formAction',
//a
'add',
];

// ===

// Resolver.php resolveAddElementToArea()
public function add()
{
$request = $this->getRequest();
$postVars = json_decode($request->getBody(), true);
$elementClass = $postVars['elementClass'];
$elementalAreaID = $postVars['elementalAreaID'];
$afterElementID = $postVars['afterElementID'] ?? null;

// validate post vars
if (!is_subclass_of($elementClass, BaseElement::class)) {
throw new InvalidArgumentException("$elementClass is not a subclass of " . BaseElement::class);
}
$elementalArea = ElementalArea::get()->byID($elementalAreaID);
if (!$elementalArea) {
throw new InvalidArgumentException("Invalid ElementalAreaID: $elementalAreaID");
}

// permission checks
if (!$elementalArea->canEdit()) {
throw new InvalidArgumentException("The current user has insufficient permission to edit ElementalAreas");
}
/** @var BaseElement $newElement */
$newElement = Injector::inst()->create($elementClass);
if (!$newElement->canEdit()) {
throw new InvalidArgumentException(
'The current user has insufficient permission to edit Elements'
);
}

// Assign the parent ID directly rather than via HasManyList to prevent multiple writes.
// See BaseElement::$has_one for the "Parent" naming.
$newElement->ParentID = $elementalArea->ID;
// Ensure that a sort order is assigned - see BaseElement::onBeforeWrite()
$newElement->onBeforeWrite();

if ($afterElementID !== null) {
/** @var ReorderElements $reorderer */
$reorderer = Injector::inst()->create(ReorderElements::class, $newElement);
$reorderer->reorder($afterElementID); // also writes the element
} else {
$newElement->write();
}

$response = $this->getResponse();
$response->setStatusCode(201);
// return $newElement;
}

// ===

public function getClientConfig()
{
$clientConfig = parent::getClientConfig();
Expand Down

0 comments on commit 7643e54

Please sign in to comment.