Skip to content

Commit

Permalink
Disallow editing or deleting transactions from the Admin interface if…
Browse files Browse the repository at this point in the history
… the transaction is applied
  • Loading branch information
Macavity committed Sep 9, 2023
1 parent ed44e81 commit 3b29195
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Controller/Admin/TransactionCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use App\Entity\Transaction;
use App\Enum\TransactionType;
use App\Trait\HasCurrencyFormatter;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
Expand Down Expand Up @@ -51,10 +54,27 @@ public function configureFields(string $pageName): iterable
});
}

public function configureActions(Actions $actions): Actions
{
$disableIfApplied = function (Action $action) {
return $action->displayIf(function ($entity) {
// Only display the 'edit' action if the transaction is not applied
return !$entity->isApplied();
});
};

return $actions
->update(Crud::PAGE_INDEX, Action::EDIT, $disableIfApplied)
->update(Crud::PAGE_DETAIL, Action::EDIT, $disableIfApplied)
->update(Crud::PAGE_INDEX, Action::DELETE, $disableIfApplied)
->update(Crud::PAGE_DETAIL, Action::DELETE, $disableIfApplied);
}

public function configureFilters(Filters $filters): Filters
{
return parent::configureFilters($filters)
->add(BooleanFilter::new('applied')->setFormTypeOption('expanded', true))
->add(BooleanFilter::new('pocketMoney')->setFormTypeOption('expanded', true))
->add('account')
->add('category');
}
Expand Down
25 changes: 25 additions & 0 deletions src/Story/DefaultAccountStory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Story;

use App\Entity\Category;
use App\Factory\AccountFactory;
use App\Factory\AvatarFactory;
use App\Factory\UserFactory;
use Zenstruck\Foundry\Story;

/**
* @method static Category earn()
* @method static Category spent()
*/
final class DefaultAccountStory extends Story
{
public function build(): void
{
AvatarFactory::createOne();
$this->addState('user', UserFactory::createOne());
$this->addState('account', AccountFactory::createOne());
}
}
8 changes: 7 additions & 1 deletion src/Story/DefaultTransactionStory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

namespace App\Story;

use App\Factory\AccountFactory;
use App\Factory\AvatarFactory;
use App\Factory\ExpenseFactory;
use App\Factory\UserFactory;
use Zenstruck\Foundry\Story;

final class DefaultTransactionStory extends Story
{
public function build(): void
{
ExpenseFactory::createMany(100);
AvatarFactory::createOne();
UserFactory::createOne();
AccountFactory::createOne();
ExpenseFactory::createMany(10);
}
}

0 comments on commit 3b29195

Please sign in to comment.