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

Fix various issues with table assignment UX #4

Merged
merged 7 commits into from
May 18, 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
10 changes: 3 additions & 7 deletions app/Filament/Resources/ApplicationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
use App\Http\Controllers\Applications\ApplicationController;
use App\Models\Application;
use App\Models\TableType;
use App\Notifications\AcceptedNotification;
use App\Notifications\OnHoldNotification;
use App\Notifications\WaitingListNotification;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\Form;
Expand Down Expand Up @@ -120,14 +117,13 @@ public static function table(Table $table): Table
'success' => ApplicationStatus::TableAccepted->value,
'danger' => ApplicationStatus::Canceled->value
]),
Tables\Columns\TextColumn::make('requestedTable.name'),
// FIXME: It is currently not possible to select 'null' to clear the assigned table here, therefore placeholder selection has been disabled.
Tables\Columns\SelectColumn::make('table_type_assigned')->options(TableType::pluck('name', 'id')->toArray())->disablePlaceholderSelection(),
Tables\Columns\TextColumn::make('requestedTable.name')->icon(fn($record) => $record->type === ApplicationType::Dealer && $record->table_type_requested !== $record->table_type_assigned ? 'heroicon-o-exclamation' : '')->iconPosition('after')->color(fn($record) => $record->type === ApplicationType::Dealer && $record->table_type_requested !== $record->table_type_assigned ? 'warning' : ''),
Tables\Columns\SelectColumn::make('tableTypeAssignedAutoNull')->label('Assigned table')->options(TableType::pluck('name', 'id')->toArray()),
Tables\Columns\TextColumn::make('type')->formatStateUsing(function (string $state) {
return ucfirst($state);
})->sortable(),
Tables\Columns\TextInputColumn::make('table_number')->sortable()->searchable(),
Tables\Columns\IconColumn::make('is_ready')->getStateUsing(function (Application $record) {
Tables\Columns\IconColumn::make('is_ready')->label('Ready')->getStateUsing(function (Application $record) {
return $record->isReady();
})->boolean(),
Tables\Columns\TextColumn::make('dlrshp')->getStateUsing(function (Application $record) {
Expand Down
33 changes: 29 additions & 4 deletions app/Models/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enums\ApplicationStatus;
use App\Enums\ApplicationType;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -43,6 +44,7 @@ class Application extends Model
protected static function boot()
{
parent::boot();

// Automatically update application status from Waiting to TableAssigned on table type/number change
// to allow accepting waiting dealerships via table assignment
static::updating(function (Application $model) {
Expand Down Expand Up @@ -350,6 +352,22 @@ public static function getAllApplicationsForExport()
return json_decode(json_encode($applications), true);
}

/**
* Sets table_type_assigned to `null` if input value is empty, converts to integer otherwise.
*
* Hacky fix for issues with selecting the placeholder in Filament\Tables\Columns\SelectColumn,
* which will attempt to set the int field 'table_type_assigned' to '' instead of null and
* triggers an exception in the process instead of storing the value.
*/
public function tableTypeAssignedAutoNull(): Attribute {
return Attribute::make(
get: fn (int|null $value, array $attributes) => $attributes['table_type_assigned'],
set: fn (mixed $value) => [
'table_type_assigned' => empty($value) ? null : intval($value),
]
);
}

/**
* An application is considered ready if all related applications (parent/children) share the
* same status (canceled child applications are ignored for this) and table number and the
Expand All @@ -361,14 +379,21 @@ public function isReady(): bool
return false;
}

if ($this->type !== ApplicationType::Dealer) {
$dealership = $this->parent()->get()->first();
} else {
if ($this->type === ApplicationType::Dealer) {
$dealership = $this;
} else {
$dealership = $this->parent()->get()->first();
}

if (!empty($dealership->table_number) && $dealership->table_type_assigned === null) {
return false;
}

foreach ($dealership->children()->get() as $child) {
if ($child->status !== ApplicationStatus::Canceled && $child->status !== $dealership->status) {
if($child->status === ApplicationStatus::Canceled) {
continue;
}
if ($child->status !== $dealership->status) {
return false;
}
// table numbers must be identical
Expand Down
3 changes: 2 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function canAccessFilament(): bool
"QL89R6583KNDG3WJ", // ???
"M728WGE7ZJKJVO63", // ???
"QL89R6580XKNDG3W", // Pattarchus(?)
"1243MK1XZWKXWJ68" // Jul
"1243MK1XZWKXWJ68", // Jul
"QL89R65833KNDG3W", // Fenrikur
]);
}
}