Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

feat: Reply to conversation #49

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 57 additions & 9 deletions module/Olcs/src/Controller/ConversationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
use Common\Controller\Interfaces\ToggleAwareInterface;
use Common\Controller\Lva\AbstractController;
use Common\FeatureToggle;
use Common\Form\Form;
use Common\Service\Helper\FlashMessengerHelperService;
use Common\Service\Helper\FormHelperService;
use Common\Service\Table\TableFactory;
use Dvsa\Olcs\Transfer\Command\Messaging\Message\Create as CreateMessageCommand;
use Dvsa\Olcs\Transfer\Query\Messaging\Messages\ByConversation as ByConversationQuery;
use Dvsa\Olcs\Transfer\Query\Messaging\Conversations\ByOrganisation as ByOrganisationQuery;
use Dvsa\Olcs\Utils\Translation\NiTextTranslation;
use Laminas\Http\Response;
use Laminas\View\Model\ViewModel;
use Laminas\View\View;
use LmcRbacMvc\Service\AuthorizationService;
use Olcs\Form\Model\Form\Message\Reply as ReplyForm;

class ConversationsController extends AbstractController implements ToggleAwareInterface
{
Expand All @@ -25,17 +29,19 @@ class ConversationsController extends AbstractController implements ToggleAwareI
];

protected FlashMessengerHelperService $flashMessengerHelper;
protected TableFactory $tableFactory;
protected TableFactory $tableFactory;
protected FormHelperService $formHelperService;

public function __construct(
NiTextTranslation $niTextTranslationUtil,
AuthorizationService $authService,
NiTextTranslation $niTextTranslationUtil,
AuthorizationService $authService,
FlashMessengerHelperService $flashMessengerHelper,
TableFactory $tableFactory
)
{
TableFactory $tableFactory,
FormHelperService $formHelperService
) {
$this->flashMessengerHelper = $flashMessengerHelper;
$this->tableFactory = $tableFactory;
$this->formHelperService = $formHelperService;

parent::__construct($niTextTranslationUtil, $authService);
}
Expand Down Expand Up @@ -77,7 +83,8 @@ public function addAction(): ViewModel
return $view;
}

public function viewAction(): ViewModel
/** @return ViewModel|Response */
public function viewAction()
{
$params = [
'page' => $this->params()->fromQuery('page', 1),
Expand All @@ -95,12 +102,53 @@ public function viewAction(): ViewModel
$messages = [];
}

$form = $this->formHelperService->createForm(ReplyForm::class, true, false);
$this->formHelperService->setFormActionFromRequest($form, $this->getRequest());

$table = $this->tableFactory
->buildTable('messages-view', $messages, $params);

$view = new ViewModel(['table' => $table]);
$view = new ViewModel(
[
'table' => $table,
'form' => $form,
],
);
$view->setTemplate('messages-view');

if ($this->getRequest()->isPost() && $this->params()->fromPost('action') === 'reply') {
return $this->parseReply($view, $form);
}

return $view;
}

/** @return Response|ViewModel */
protected function parseReply(ViewModel $view, Form $form)
{
$form->setData((array)$this->params()->fromPost());
$form->get('id')->setValue($this->params()->fromRoute('conversation'));

if (!$form->isValid()) {
return $view;
}

$response = $this->handleCommand(
CreateMessageCommand::create(
[
'conversation' => $this->params()->fromRoute('conversationId'),
'messageContent' => $form->get('form-actions')->get('reply')->getValue(),
],
),
);

if ($response->isOk()) {
$this->flashMessengerHelper->addSuccessMessage('Reply submitted successfully');
return $this->redirect()->toRoute('conversations/view', $this->params()->fromRoute());
}

$this->handleErrors($response->getResult());

return parent::indexAction();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Olcs\Controller\Factory;

use Common\Service\Helper\FlashMessengerHelperService;
use Common\Service\Helper\FormHelperService;
use Common\Service\Table\TableFactory;
use Dvsa\Olcs\Utils\Translation\NiTextTranslation;
use Interop\Container\ContainerInterface;
Expand All @@ -15,18 +16,23 @@

class ConversationsControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): ConversationsController
{
public function __invoke(
ContainerInterface $container,
$requestedName,
array $options = null
): ConversationsController {
$niTextTranslationUtil = $container->get(NiTextTranslation::class);
$authService = $container->get(AuthorizationService::class);
$flashMessengerHelper = $container->get(FlashMessengerHelperService::class);
$tableFactory = $container->get(TableFactory::class);
$formHelperService = $container->get(FormHelperService::class);

return new ConversationsController(
$niTextTranslationUtil,
$authService,
$flashMessengerHelper,
$tableFactory,
$formHelperService,
);
}
}
43 changes: 43 additions & 0 deletions module/Olcs/src/Form/Model/Fieldset/Message/Reply.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Olcs\Form\Model\Fieldset\Message;

use Common\Form\Elements\InputFilters\ActionButton;
use Laminas\Form\Annotation as Form;
use Laminas\Form\Element\File;
use Laminas\Form\Element\Textarea;

class Reply
{
/**
* @Form\Attributes({
* "class": "extra-long",
* "maxlength": 1000
* })
* @Form\Options({"label": "You can enter up to 1000 characters"})
* @Form\Required(true)
* @Form\Type(\Laminas\Form\Element\Textarea::class)
* @Form\Filter({"name": \Laminas\Filter\StringTrim::class})
* @Form\Validator({
* "name": \Laminas\Validator\StringLength::class,
* "options": {"max":1000}
* })
*/
public ?TextArea $reply = null;

/**
* @Form\Attributes({
* "type": "submit",
* "data-module": "govuk-button",
* "class": "govuk-button govuk-button--default",
* "id": "send"
* })
* @Form\Options({
* "label": "Send message"
* })
* @Form\Type(\Common\Form\Elements\InputFilters\ActionButton::class)
*/
public ?ActionButton $send = null;
}
37 changes: 37 additions & 0 deletions module/Olcs/src/Form/Model/Form/Message/Reply.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Olcs\Form\Model\Form\Message;

use Laminas\Form\Annotation as Form;
use Laminas\Form\Element\Hidden;
use Olcs\Form\Model\Fieldset\Message\Reply as ReplyFieldset;

/**
* @codeCoverageIgnore No methods
* @Form\Name("licence_message_reply")
* @Form\Attributes({"method": "post"})
* @Form\Type(\Common\Form\Form::class)
* @Form\Options({"prefer_form_input_filter": true})
*/
class Reply
{
/**
* @Form\Attributes({"value": ""})
* @Form\Type(\Laminas\Form\Element\Hidden::class)
*/
public ?Hidden $id = null;

/**
* @Form\Attributes({"value": "reply"})
* @Form\Type(\Laminas\Form\Element\Hidden::class)
*/
public ?Hidden $action = null;

/**
* @Form\Name("form-actions")
* @Form\ComposedObject(\Olcs\Form\Model\Fieldset\Message\Reply::class)
*/
public ?ReplyFieldset $formActions = null;
}
16 changes: 15 additions & 1 deletion module/Olcs/view/messages-view.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@ echo $this->partial(
echo $menu->setMinDepth(0)
->setMaxDepth(0)
->setPartial('partials/tabs-nav');

?>
<details class="govuk-details" data-module="govuk-details">
<summary class="govuk-details__summary" aria-controls="details-content-operating-centre">
<span class="govuk-details__summary-text">
Send a reply
</span>
</summary>
<div class="govuk-details__text" id="details-content-operating-centre">
<?php
echo $this->formErrors($this->form);
echo $this->form($this->form);
?>
</div>
</details>
<?php
echo $this->table;
?>
</div>
Expand Down
Loading
Loading