Skip to content

Commit

Permalink
TASK: Use value object for sorting to simplify codeflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Jan 8, 2025
1 parent 65683d8 commit 6b046c2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 19 deletions.
17 changes: 11 additions & 6 deletions Neos.Workspace.Ui/Classes/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
use Neos\Workspace\Ui\ViewModel\DocumentItem;
use Neos\Workspace\Ui\ViewModel\EditWorkspaceFormData;
use Neos\Workspace\Ui\ViewModel\PendingChanges;
use Neos\Workspace\Ui\ViewModel\Sorting;
use Neos\Workspace\Ui\ViewModel\WorkspaceListItem;
use Neos\Workspace\Ui\ViewModel\WorkspaceListItems;

Expand Down Expand Up @@ -134,8 +135,13 @@ class WorkspaceController extends AbstractModuleController
/**
* Display a list of unpublished content
*/
public function indexAction(string $sortBy = 'title', bool $sortAscending = true): void
public function indexAction(Sorting|null $sorting = null): void
{
$sorting ??= new Sorting(
sortBy: 'title',
sortAscending: true
);

$currentUser = $this->userService->getCurrentUser();
if ($currentUser === null) {
throw new \RuntimeException('No user authenticated', 1718308216);
Expand All @@ -145,15 +151,14 @@ public function indexAction(string $sortBy = 'title', bool $sortAscending = true
$contentRepository = $this->contentRepositoryRegistry->get($contentRepositoryId);

$workspaceListItems = $this->getWorkspaceListItems($contentRepository);
if ($sortBy === 'title') {
$workspaceListItems = $workspaceListItems->sortByTitle($sortAscending);
}
$workspaceListItems = match($sorting->sortBy) {
'title' => $workspaceListItems->sortByTitle($sorting->sortAscending),
};

$this->view->assignMultiple([
'workspaceListItems' => $workspaceListItems,
'flashMessages' => $this->controllerContext->getFlashMessageContainer()->getMessagesAndFlush(),
'sortAscending' => $sortAscending,
'sortBy' => $sortBy,
'sorting' => $sorting,
]);
}

Expand Down
47 changes: 47 additions & 0 deletions Neos.Workspace.Ui/Classes/ViewModel/Sorting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Neos\Workspace\Ui\ViewModel;

use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;

#[Flow\Proxy(false)]
final readonly class Sorting implements \JsonSerializable, ProtectedContextAwareInterface
{
public function __construct(
public string $sortBy,
public bool $sortAscending
) {
if (!in_array($sortBy, ['title'], true)) {
throw new \RuntimeException(sprintf('Invalid sortBy %s specified', $sortBy), 1736344550);
}
}

public static function fromArray(array $array): self
{
return new self(
sortBy: $array['sortBy'],
sortAscending: (bool)$array['sortAscending'],
);
}

public function withInvertedSorting(): self
{
return new self(
sortBy: $this->sortBy,
sortAscending: !$this->sortAscending
);
}

public function jsonSerialize(): mixed
{
return get_object_vars($this);
}

public function allowsCallOfMethod($methodName)
{
return in_array($methodName, ['withInvertedSorting', 'jsonSerialize'], true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ Neos.Workspace.Ui.WorkspaceController.index = Neos.Fusion:Component {
workspaceListItems = ${workspaceListItems}
/// array
flashMessages = ${flashMessages}
/// string
sortBy = ${sortBy}
/// bool
sortAscending = ${sortAscending}
/// Neos\Workspace\Ui\ViewModel\Sorting
sorting = ${sorting}

newAction = Neos.Fusion:UriBuilder {
action = 'new'
Expand All @@ -25,8 +23,7 @@ Neos.Workspace.Ui.WorkspaceController.index = Neos.Fusion:Component {
<div class="neos-row-fluid">
<Neos.Workspace.Ui:Component.WorkspaceTable
workspaceListItems={props.workspaceListItems}
sortBy={props.sortBy}
sortAscending={props.sortAscending}
sorting={props.sorting}
/>
</div>
</main>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
#
prototype(Neos.Workspace.Ui:Component.WorkspaceTable) < prototype(Neos.Fusion:Component) {
/// \Neos\Workspace\Ui\ViewModel\WorkspaceListItems
workspaceListItems = ${{}}
/// string
sortBy = 'title'
/// bool
sortAscending = true
workspaceListItems = null
/// Neos\Workspace\Ui\ViewModel\Sorting
sorting = null

@private {
i18n = ${I18n.id('').source('Main').package('Neos.Workspace.Ui')}
workspacesUri = Neos.Fusion:ActionUri {
action = 'index'
format = 'html'
arguments {
sortAscending = ${!props.sortAscending}
// Todo hack convert to array manually, flows routing chokes on value objects: Tried to convert an object of type "Neos\Workspace\Ui\ViewModel\Sorting" to an identity array, but it is unknown to the Persistence Manager.
sorting = ${sorting.withInvertedSorting().jsonSerialize()}
}
}
}
Expand All @@ -35,7 +34,7 @@ prototype(Neos.Workspace.Ui:Component.WorkspaceTable) < prototype(Neos.Fusion:Co
hx-swap="outerHTML"
>
{private.i18n.id('workspaces.workspace.title').translate()}
<Neos.Workspace.Ui:Component.Icon icon={props.sortAscending ? 'sort-alpha-down' : 'sort-alpha-up'}/>
<Neos.Workspace.Ui:Component.Icon icon={props.sorting.sortAscending ? 'sort-alpha-down' : 'sort-alpha-up'}/>
</button>
</th>
<th>{private.i18n.id('workspaces.workspace.description')}</th>
Expand Down

0 comments on commit 6b046c2

Please sign in to comment.