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: tenancy #55

Merged
merged 1 commit into from
Jun 15, 2024
Merged
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
64 changes: 63 additions & 1 deletion src/Components/QuickCreateMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Actions\CreateAction;
use Filament\Facades\Filament;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Facades\App;
use InvalidArgumentException;
use Livewire\Component;

class QuickCreateMenu extends Component implements HasForms, HasActions
class QuickCreateMenu extends Component implements HasActions, HasForms
{
use InteractsWithActions;
use InteractsWithForms;
Expand Down Expand Up @@ -78,6 +82,64 @@ public function getActions(): array
->slideOver(fn (): bool => QuickCreatePlugin::get()->shouldUseSlideOver())
->form(function ($arguments, $form) use ($r) {
return $r->form($form->operation('create')->columns());
})->action(function (array $arguments, Form $form, CreateAction $action) use ($r): void {
$model = $action->getModel();

$record = $action->process(function (array $data, HasActions $livewire) use ($model, $action, $r): Model {
if ($translatableContentDriver = $livewire->makeFilamentTranslatableContentDriver()) {
$record = $translatableContentDriver->makeRecord($model, $data);
} else {
$record = new $model();
$record->fill($data);
}

if ($relationship = $action->getRelationship()) {
/** @phpstan-ignore-next-line */
$relationship->save($record);

return $record;
}

if (
$r::isScopedToTenant() &&
($tenant = Filament::getTenant())
) {
$relationship = $r::getTenantRelationship($tenant);

if ($relationship instanceof HasManyThrough) {
$record->save();

return $record;
}

return $relationship->save($record);
}

$record->save();

return $record;
});

$action->record($record);
$form->model($record)->saveRelationships();

if ($arguments['another'] ?? false) {
$action->callAfter();
$action->sendSuccessNotification();

$action->record(null);

// Ensure that the form record is anonymized so that relationships aren't loaded.
$form->model($model);

$form->fill();

$action->halt();

return;
}

$action->success();
});
})
->values()
Expand Down