Skip to content

Commit

Permalink
LOOP-1097: Improved UX
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed Oct 11, 2021
1 parent 8f87ca2 commit 5cd7dfc
Showing 1 changed file with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$node = $this->getNode();
$subject = $this->helper->getSubject($node);

if ($form_state->getTemporaryValue('is_sent')) {
// Don't build the form if alert has been sent.
return [];
}

$form['subject'] = [
'#type' => 'textfield',
'#title' => $this->t('Subject'),
Expand All @@ -88,7 +93,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$options = [
'all_users' => $this->formatPlural($numberOfUsers, 'One user', 'All @count users'),
];
$defaultValue = 'all';
if (NULL !== $subject) {
$numberOfUsers = $this->helper->getNumberOfSubscribers($subject);
$options['subject_subscribers'] = $this->formatPlural(
Expand All @@ -100,13 +104,12 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'@count' => $numberOfUsers,
]
);
$defaultValue = 'subject_subscribers';
}
$form['recipients'] = [
'#type' => 'radios',
'#title' => $this->t('Recipients'),
'#options' => $options,
'#default_value' => $defaultValue,
'#required' => TRUE,
];

$form['actions']['#type'] = 'actions';
Expand Down Expand Up @@ -151,6 +154,11 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
if (FALSE === strpos($message, '[node:url]')) {
$form_state->setErrorByName('message', $this->t('Message does not contain <code>[node:url]</code>'));
}

$numberOfRecipients = count($this->getRecipients($form_state));
if ($numberOfRecipients < 1) {
$form_state->setErrorByName('recipients', $this->t('Empty list of recipients selected'));
}
}

/**
Expand Down Expand Up @@ -187,11 +195,22 @@ private function sendAlert(FormStateInterface $form_state) {
'node' => $node,
]);
if ($result['result']) {
$this->messenger()->addMessage($this->t('Your message has been sent.'));
$form_state->setRedirect('entity.node.canonical', ['node' => $node->id()]);
$this->messenger()->addMessage(
$this->formatPlural(
count($recipients),
'Your alert on <a href="@url">@title</a> has been sent to one recipient.',
'Your alert on <a href="@url">@title</a> has been sent to @count recipients.',
[
'@url' => Url::fromRoute('entity.node.canonical', ['node' => $node->id()])->toString(),
'@title' => $node->label(),
]
));
$form_state
->setTemporaryValue('is_sent', TRUE)
->setRebuild();
}
else {
$this->messenger()->addError($this->t('There was a problem sending your message and it was not sent.'));
$this->messenger()->addError($this->t('There was a problem sending your alert and it was not sent.'));
$form_state
->setRebuild()
->set('submitted', TRUE);
Expand Down

0 comments on commit 5cd7dfc

Please sign in to comment.