Skip to content

Commit

Permalink
fix template slug on creation and duplicate action
Browse files Browse the repository at this point in the history
  • Loading branch information
danilopolani committed Feb 22, 2024
1 parent 8b65584 commit 042fc8d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion resources/dist/css/theme.css

Large diffs are not rendered by default.

24 changes: 13 additions & 11 deletions src/Resources/TemplateResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,21 @@ public static function table(Tables\Table $table): Tables\Table
->filters(static::getTableFilters())
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\ReplicateAction::make()
Tables\Actions\Action::make('duplicate')
->label('Duplicate')
->excludeAttributes([
'user_id',
'is_locked',
'slug',
])
->beforeReplicaSaved(function (Template $replica): void {
$replica->fill([
->icon('heroicon-o-document-duplicate')
->requiresConfirmation()
->action(function (Template $record) {
$name = $record->name . ' (Copy)';

$newRecord = $record->replicate();
$newRecord->fill([
'user_id' => Auth::id(),
'name' => $replica->name . ' (Copy)',
'slug' => (new GenerateSlug())->run($replica->name . ' (Copy)'),
]);
'name' => $name,
'slug' => (new GenerateSlug())->run($name),
])->save();

redirect(TemplateResource::getUrl('edit', ['record' => $newRecord]));
}),
])
->bulkActions([])
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/TemplateResource/Pages/CreateTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class CreateTemplate extends CreateRecord
*/
protected function mutateFormDataBeforeCreate(array $data): array
{
return $data + [
return [
...$data,
'user_id' => Auth::id(),
'slug' => (new GenerateSlug())->run($this->data['name']),
];
Expand Down

0 comments on commit 042fc8d

Please sign in to comment.