Skip to content

Commit

Permalink
Setup state management for move Task status
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Ullyott <ullyott.kevin@gmail.com>
  • Loading branch information
Orrison committed Aug 29, 2023
1 parent 6f44c28 commit 378d269
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 12 deletions.
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
73 changes: 72 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 378d269

Please sign in to comment.