Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: recurring frequency remaining recurrences adjust #10

Closed
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
21 changes: 21 additions & 0 deletions app/Enums/Frequency.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,30 @@ enum Frequency: string implements HasLabel
case MONTHLY = 'monthly';
case QUARTERLY = 'quarterly';
case YEARLY = 'yearly';
case FOREVER = 'forever';

public function getLabel(): ?string
{
return $this->name;
}

public static function getRemainingRecurrences(string $frequency, ?int $remainingRecurrences): ?int
{
if ($frequency === self::ONCE->value) {
return 1;
}
if ($frequency === self::FOREVER->value) {
return null;
}

return $remainingRecurrences;
}

public static function canUpdateRemainingRecurrences(?string $frequency): bool
{
return ! in_array($frequency, [
Frequency::ONCE->value,
Frequency::FOREVER->value,
]);
}
}
14 changes: 12 additions & 2 deletions app/Filament/Concerns/RecurringIncomeRecurringExpenseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Tables\Actions\BulkActionGroup;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
Expand Down Expand Up @@ -58,10 +60,18 @@ public static function form(Form $form): Form

Select::make('frequency')
->options(Frequency::class)
->required(),
->required()
->live()
->afterStateUpdated(fn (Set $set, ?string $state, Get $get) => $set(
'remaining_recurrences',
Frequency::getRemainingRecurrences($state, $get('remaining_recurrences')))
),

TextInput::make('remaining_recurrences')
->integer(),
->nullable()
->integer()
->readOnly(fn (Get $get) => ! Frequency::canUpdateRemainingRecurrences($get('frequency'))),


Select::make('tags')
->relationship('tags', 'name')
Expand Down
13 changes: 11 additions & 2 deletions app/Filament/Resources/RecurringTransferResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Resources\Resource;
use Filament\Tables\Actions\BulkActionGroup;
use Filament\Tables\Actions\DeleteAction;
Expand Down Expand Up @@ -60,10 +62,17 @@ public static function form(Form $form): Form

Select::make('frequency')
->options(Frequency::class)
->required(),
->required()
->live()
->afterStateUpdated(fn (Set $set, ?string $state, Get $get) => $set(
'remaining_recurrences',
Frequency::getRemainingRecurrences($state, $get('remaining_recurrences')))
),

TextInput::make('remaining_recurrences')
->integer(),
->nullable()
->integer()
->readOnly(fn (Get $get) => ! Frequency::canUpdateRemainingRecurrences($get('frequency'))),

Select::make('tags')
->relationship('tags', 'name')
Expand Down
Loading