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

Setup state management for move Task status #59

Merged
merged 2 commits into from
Aug 30, 2023
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
8 changes: 8 additions & 0 deletions app-modules/task/src/Enums/TaskStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

namespace Assist\Task\Enums;

use Bvtterfly\ModelStateMachine\Attributes\InitialState;
use Bvtterfly\ModelStateMachine\Attributes\AllowTransitionTo;

enum TaskStatus: string
{
#[InitialState]
#[AllowTransitionTo(self::IN_PROGRESS)]
#[AllowTransitionTo(self::CANCELLED)]
case PENDING = 'pending';

#[AllowTransitionTo(self::COMPLETED)]
#[AllowTransitionTo(self::CANCELLED)]
case IN_PROGRESS = 'in_progress';

case COMPLETED = 'completed';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Assist\Task\Filament\Resources\TaskResource\Pages;

use Filament\Forms\Form;
use Assist\Task\Enums\TaskStatus;
use Assist\Prospect\Models\Prospect;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
Expand All @@ -26,12 +25,6 @@ public function form(Form $form): Form
->label('Description')
->required()
->string(),
Select::make('status')
->label('Status')
->options(collect(TaskStatus::cases())->mapWithKeys(fn (TaskStatus $status) => [$status->value => str($status->name)->title()->headline()]))
->required()
->enum(TaskStatus::class)
->default(TaskStatus::PENDING->value),
DateTimePicker::make('due')
->label('Due Date')
->native(false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,18 @@ public function table(Table $table): Table
])
->actions([
ViewAction::make()
//->action(fn (Task $record) => $record->update(['status' => TaskStatus::COMPLETED]))
->extraModalFooterActions(
[
Action::make('mark_as_in_progress')
->label('Mark as In Progress')
->action(fn (Task $record) => $record->getStateMachine('status')->transitionTo(TaskStatus::IN_PROGRESS))
->cancelParentActions()
->hidden(fn (Task $record) => $record->getStateMachine('status')->getStateTransitions()->doesntContain(TaskStatus::IN_PROGRESS->value)),
Action::make('mark_as_completed')
->label('Mark as Completed')
->action(fn (Task $record) => $record->update(['status' => TaskStatus::COMPLETED]))
->action(fn (Task $record) => $record->getStateMachine('status')->transitionTo(TaskStatus::COMPLETED))
->cancelParentActions()
->hidden(fn (Task $record) => $record->status->value === TaskStatus::COMPLETED->value),
->hidden(fn (Task $record) => $record->getStateMachine('status')->getStateTransitions()->doesntContain(TaskStatus::COMPLETED->value)),
]
)
->infolist(
Expand Down
10 changes: 9 additions & 1 deletion app-modules/task/src/Models/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Assist\Task\Enums\TaskStatus;
use OwenIt\Auditing\Contracts\Auditable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Bvtterfly\ModelStateMachine\HasStateMachine;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -21,10 +22,10 @@ class Task extends BaseModel implements Auditable, CanTriggerAutoSubscription
use HasUuids;
use AuditableTrait;
use SoftDeletes;
use HasStateMachine;

protected $fillable = [
'description',
'status',
'due',
'assigned_to',
'concern_id',
Expand All @@ -36,6 +37,13 @@ class Task extends BaseModel implements Auditable, CanTriggerAutoSubscription
'due' => 'datetime',
];

public function getStateMachineFields(): array
{
return [
'status',
];
}

public function concern(): MorphTo
{
return $this->morphTo();
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"php": "^8.2",
"ext-pdo": "*",
"awcodes/filament-tiptap-editor": "^3.0@beta",
"bvtterfly/model-state-machine": "^0.3.0",
"canyon-gbs/assist-data-model": "*",
"canyon-gbs/audit": "*",
"canyon-gbs/authorization": "*",
Expand Down
Loading