Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Nov 29, 2023
1 parent 9bc6b2e commit bab7651
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/controllers/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace markhuot\keystone\controllers;

use craft\web\Controller;
use markhuot\keystone\actions\AddComponent;
use markhuot\keystone\actions\DeleteComponent;
use markhuot\keystone\actions\EditComponentData;
Expand Down Expand Up @@ -44,7 +43,7 @@ public function actionStore()
{
$data = $this->request->getBodyParamObjectOrFail(StoreComponentRequest::class);

(new AddComponent)->handle(
$component = (new AddComponent)->handle(
elementId: $data->element->id,
fieldId: $data->field->id,
sortOrder: $data->sortOrder,
Expand All @@ -53,9 +52,7 @@ public function actionStore()
type: $data->type,
);

return $this->asSuccess('Component added', [
'fieldHtml' => $data->element->getFieldHtml($data->field),
]);
return $this->asFieldSuccess('Component added', $component);
}

public function actionEdit()
Expand Down Expand Up @@ -83,29 +80,23 @@ public function actionUpdate()

(new EditComponentData)->handle($component, $fields);

return $this->asSuccess('Component saved', [
'fieldHtml' => $component->getElement()->getFieldHtml($component->getField()),
]);
return $this->asFieldSuccess('Component saved', $component);
}

public function actionDelete()
{
$component = $this->request->getBodyParamObjectOrFail(Component::class);
(new DeleteComponent)->handle($component);

return $this->asSuccess('Component deleted', [
'fieldHtml' => $component->element->getFieldHtml($component->field),
]);
return $this->asFieldSuccess('Component deleted', $component);
}

public function actionMove()
{
$data = $this->request->getBodyParamObjectOrFail(MoveComponentRequest::class);
(new MoveComponent)->handle($data->source, $data->position, $data->target, $data->slot);

return $this->asSuccess('Component moved', [
'fieldHtml' => $data->getTargetElement()->getFieldHtml($data->getTargetField()),
]);
return $this->asFieldSuccess('Component moved', $data->source);
}

public function actionToggleDisclosure()
Expand Down
16 changes: 16 additions & 0 deletions src/controllers/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace markhuot\keystone\controllers;

use markhuot\keystone\models\Component;
use yii\web\Response;

class Controller extends \craft\web\Controller
{
public function asFieldSuccess(string $message, ?Component $component): Response
{
$html = $component->getElement()->getFieldHtml($component->getField());

return $this->asSuccess($message, ['fieldHtml' => $html]);
}
}

0 comments on commit bab7651

Please sign in to comment.