Skip to content

Commit

Permalink
fix create mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh committed Aug 25, 2023
1 parent d5c6756 commit 763384c
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/Resources/RoleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ public static function form(Form $form): Form
->options(fn (): array => static::experimentalGetPageOptions())
->searchable()
->live()
->afterStateHydrated(function (Component $component, $livewire, Model $record, Forms\Set $set) {
->afterStateHydrated(function (Component $component, $livewire, string $operation, ?Model $record, Forms\Set $set) {
static::experimentalSetPermissionStateForRecordPermissions(
component: $component,
operation: $operation,
permissions: static::experimentalGetPageOptions(),
record: $record
);
Expand All @@ -119,9 +120,10 @@ public static function form(Form $form): Form
->options(fn (): array => static::experimentalGetWidgetOptions())
->searchable()
->live()
->afterStateHydrated(function (Component $component, $livewire, Model $record, Forms\Set $set) {
->afterStateHydrated(function (Component $component, $livewire, string $operation, ?Model $record, Forms\Set $set) {
static::experimentalSetPermissionStateForRecordPermissions(
component: $component,
operation: $operation,
permissions: static::experimentalGetWidgetOptions(),
record: $record
);
Expand All @@ -148,9 +150,10 @@ public static function form(Form $form): Form
->options(fn (): array => static::experimentalGetCustomPermissionOptions())
->searchable()
->live()
->afterStateHydrated(function (Component $component, $livewire, Model $record, Forms\Set $set) {
->afterStateHydrated(function (Component $component, $livewire, string $operation, ?Model $record, Forms\Set $set) {
static::experimentalSetPermissionStateForRecordPermissions(
component: $component,
operation: $operation,
permissions: static::experimentalGetCustomPermissionOptions(),
record: $record
);
Expand Down Expand Up @@ -304,9 +307,10 @@ public static function getResourceEntitiesSchema(): ?array
->hint(Utils::showModelPath($entity['fqcn']))
->options(fn (): array => static::experimentalGetResourcePermissionOptions($entity))
->live()
->afterStateHydrated(function (Component $component, Model $record) use ($entity) {
->afterStateHydrated(function (Component $component, string $operation, ?Model $record) use ($entity) {
static::experimentalSetPermissionStateForRecordPermissions(
component: $component,
operation: $operation,
permissions: static::experimentalGetResourcePermissionOptions($entity),
record: $record
);
Expand Down Expand Up @@ -340,20 +344,23 @@ public static function experimentalGetResourcePermissionOptions(array $entity):
->toArray();
}

public static function experimentalSetPermissionStateForRecordPermissions(Component $component, array $permissions, Model $record): void
public static function experimentalSetPermissionStateForRecordPermissions(Component $component, string $operation, array $permissions, ?Model $record): void
{

if (blank($record)) {
return;
}
if (in_array($operation, ['edit', 'view'])) {

if (blank($record)) {
return;
}

$component->state(
collect($permissions)
/** @phpstan-ignore-next-line */
->filter(fn ($value, $key) => $record->checkPermissionTo($key))
->keys()
->toArray()
);
$component->state(
collect($permissions)
/** @phpstan-ignore-next-line */
->filter(fn ($value, $key) => $record->checkPermissionTo($key))
->keys()
->toArray()
);
}
}

public static function experimentalToggleEntitiesViaSelectAll($livewire, Forms\Set $set, bool $state): void
Expand Down

0 comments on commit 763384c

Please sign in to comment.