-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK: Improve HTMX integration in workspace UI
Use http headers for proper status codes and transport flash messages
- Loading branch information
Showing
12 changed files
with
207 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Neos.Workspace.Ui package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\Workspace\Ui\Mvc; | ||
|
||
use Neos\Flow\Mvc\ActionRequest; | ||
use Neos\Flow\Security\RequestPatternInterface; | ||
|
||
final class HtmxRequestPattern implements RequestPatternInterface | ||
{ | ||
public function matchRequest(ActionRequest $request): bool | ||
{ | ||
return $request->getFormat() === 'htmx'; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Neos.Workspace.Ui/Classes/Mvc/HttpHeaderFlashMessageStorage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Neos.Workspace.Ui package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\Workspace\Ui\Mvc; | ||
|
||
use Neos\Error\Messages\Message; | ||
use Neos\Flow\Mvc\FlashMessage\FlashMessageContainer; | ||
use Neos\Flow\Mvc\FlashMessage\FlashMessageStorageInterface; | ||
use Psr\Http\Message\ResponseInterface as HttpResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface as HttpRequestInterface; | ||
|
||
final class HttpHeaderFlashMessageStorage implements FlashMessageStorageInterface | ||
{ | ||
private FlashMessageContainer|null $flashMessageContainer = null; | ||
|
||
public function load(HttpRequestInterface $request): FlashMessageContainer | ||
{ | ||
if ($this->flashMessageContainer === null) { | ||
$this->flashMessageContainer = new FlashMessageContainer(); | ||
} | ||
return $this->flashMessageContainer; | ||
} | ||
|
||
public function persist(HttpResponseInterface $response): HttpResponseInterface | ||
{ | ||
$messages = array_map(static fn(Message $message) => [ | ||
'title' => $message->getTitle(), | ||
'message' => $message->render(), | ||
'severity' => $message->getSeverity(), | ||
'code' => $message->getCode(), | ||
], $this->flashMessageContainer?->getMessagesAndFlush() ?? []); | ||
if ($messages === []) { | ||
return $response; | ||
} | ||
return $response->withAddedHeader('X-Flow-FlashMessages', json_encode($messages, JSON_THROW_ON_ERROR)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Neos: | ||
Flow: | ||
mvc: | ||
flashMessages: | ||
containers: | ||
'httpHeaderFlashMessages': | ||
storage: 'Neos\Workspace\Ui\Mvc\HttpHeaderFlashMessageStorage' | ||
requestPatterns: | ||
'htmx': | ||
pattern: 'Neos\Workspace\Ui\Mvc\HtmxRequestPattern' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
Neos.Workspace.Ui/Resources/Private/Fusion/Components/Indicator.fusion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
prototype(Neos.Workspace.Ui:Component.Indicator) < prototype(Neos.Fusion:Component) { | ||
renderer = afx` | ||
<div id="indicator" class="htmx-indicator loadingIndicator__container"> | ||
<div class="loadingIndicator"> | ||
<div class="loadingIndicator__bar"></div> | ||
</div> | ||
</div> | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Neos.Workspace.Ui/Resources/Private/Fusion/Views/Delete.fusion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## | ||
# Empty template for the delete response as the payload is contained in the HTTP headers | ||
# | ||
Neos.Workspace.Ui.WorkspaceController.delete = '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
Neos.Workspace.Ui/Resources/Private/Fusion/Views/Index.htmx.fusion
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* @typedef {Object} Notification | ||
* @property {string} severity | ||
* @property {string} title | ||
* @property {string} message | ||
* @property {number} code | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} EventDetails | ||
* @property {XMLHttpRequest} xhr | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} HtmxEvent | ||
* @property {EventDetails} detail | ||
*/ | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
|
||
if (!window.htmx) { | ||
console.error('htmx is not loaded'); | ||
return; | ||
} | ||
|
||
/** | ||
* Show flash messages after successful requests | ||
*/ | ||
htmx.on('htmx:afterRequest', /** @param {HtmxEvent} e */(e) => { | ||
const flashMessagesJson = e.detail.xhr.getResponseHeader('X-Flow-FlashMessages'); | ||
if (!flashMessagesJson) { | ||
return; | ||
} | ||
|
||
/** @type Notification[] */ | ||
const flashMessages = JSON.parse(flashMessagesJson); | ||
flashMessages.forEach(({ severity, title, message }) => { | ||
if (title) { | ||
NeosCMS.Notification[severity.toLowerCase()](title, message); | ||
} else { | ||
NeosCMS.Notification[severity.toLowerCase()](message); | ||
} | ||
}); | ||
}); | ||
|
||
/** | ||
* Show error notifications for failed requests if no flash messages are present | ||
*/ | ||
htmx.on('htmx:responseError', /** @param {HtmxEvent} e */(e) => { | ||
const flashMessagesJson = e.detail.xhr.getResponseHeader('X-Flow-FlashMessages'); | ||
if (flashMessagesJson) { | ||
return; | ||
} | ||
|
||
const { status, statusText } = e.detail.xhr; | ||
NeosCMS.Notification.error(`Error ${status}: ${statusText}`); | ||
}); | ||
}); |