Skip to content

Commit

Permalink
LegalDocuments: Fix duplicated onscreen message
Browse files Browse the repository at this point in the history
  • Loading branch information
lscharmer authored and mjansenDatabay committed Jan 8, 2025
1 parent c6fc7e9 commit 8a72a0f
Showing 1 changed file with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,9 @@ public function addCriterion(): void

$form = $this->admin->withFormData($form, function (array $x) use ($document) {
$content = new CriterionContent(...$x[0]['content']);
$this->config->legalDocuments()->document()->validateCriteriaContent($document->criteria(), $content)->map(
$this->returnWithResult($this->config->legalDocuments()->document()->validateCriteriaContent($document->criteria(), $content)->map(
fn() => $this->config->legalDocuments()->document()->repository()->createCriterion($document, $content)
)->except($this->criterionInvalid(...))->value();

$this->returnWithMessage('doc_crit_attached', 'documents');
), 'doc_crit_attached', 'documents');
});

$this->admin->setContent($form);
Expand All @@ -154,11 +152,9 @@ public function editCriterion(): void
$form = $this->admin->withFormData($form, function (array $data) use ($document, $criterion) {
$content = new CriterionContent(...$data[0]['content']);
$criteria = array_filter($document->criteria(), fn(Criterion $other) => $other->id() !== $criterion->id());
$this->config->legalDocuments()->document()->validateCriteriaContent($criteria, $content)->map(
$this->returnWithResult($this->config->legalDocuments()->document()->validateCriteriaContent($criteria, $content)->map(
fn() => $this->config->legalDocuments()->document()->repository()->updateCriterionContent($criterion->id(), $content)
)->except($this->criterionInvalid(...))->value();

$this->returnWithMessage('doc_crit_changed', 'documents');
), 'doc_crit_changed', 'documents');
});

$this->container->tabs()->clearTargets();
Expand Down Expand Up @@ -310,24 +306,33 @@ private function deleteDocumentsConfirmation(array $documents): void
/**
* @param string|Exception $error
*/
private function criterionInvalid($error): Result
private function criterionErrorMessage($error): string
{
if (!is_string($error)) {
return new Error($error);
throw $error;
}

$message = match ($error) {
return match ($error) {
ProvideDocument::CRITERION_ALREADY_EXISTS => $this->ui->txt('criterion_assignment_must_be_unique'),
ProvideDocument::CRITERION_WOULD_NEVER_MATCH => $this->ui->txt('criterion_assignment_cannot_match'),
default => $error,
};

return new Ok($this->ui->mainTemplate()->setOnScreenMessage('failure', $message, true));
}

private function returnWithMessage(string $message, string $command): void
{
$this->ui->mainTemplate()->setOnScreenMessage('success', $this->ui->txt($message), true);
$this->ctrlTo('redirectByClass', $command);
}

private function returnWithResult(Result $result, string $success_message, string $target): void
{
if ($result->isOk()) {
$this->ui->mainTemplate()->setOnScreenMessage('success', $this->ui->txt($success_message), true);
} else {
$this->ui->mainTemplate()->setOnScreenMessage('failure', $this->criterionErrorMessage($result->error()), true);
}

$this->ctrlTo('redirectByClass', $target);
}
}

0 comments on commit 8a72a0f

Please sign in to comment.