- 2.x - sites settings

- tags
- source-control soft deletes
This commit is contained in:
Saeed Vaziry
2024-10-06 00:04:57 +02:00
parent d1f2add699
commit 3c50e2c947
44 changed files with 972 additions and 119 deletions

View File

@ -2,25 +2,53 @@
namespace App\Web\Pages\Settings\SourceControls\Actions;
use App\Actions\SourceControl\ConnectSourceControl;
use App\Actions\SourceControl\EditSourceControl;
use App\Models\SourceControl;
use App\Enums\SourceControl;
use Exception;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
class Edit
{
public static function form(): array
{
return Create::form();
return [
TextInput::make('name')
->rules(fn (Get $get) => ConnectSourceControl::rules($get())['name']),
TextInput::make('token')
->label('API Key')
->validationAttribute('API Key')
->visible(fn ($get) => in_array($get('provider'), [
SourceControl::GITHUB,
SourceControl::GITLAB,
]))
->rules(fn (Get $get) => ConnectSourceControl::rules($get())['token']),
TextInput::make('url')
->label('URL (optional)')
->visible(fn ($get) => $get('provider') == SourceControl::GITLAB)
->rules(fn (Get $get) => ConnectSourceControl::rules($get())['url'])
->helperText('If you run a self-managed gitlab enter the url here, leave empty to use gitlab.com'),
TextInput::make('username')
->visible(fn ($get) => $get('provider') == SourceControl::BITBUCKET)
->rules(fn (Get $get) => ConnectSourceControl::rules($get())['username']),
TextInput::make('password')
->visible(fn ($get) => $get('provider') == SourceControl::BITBUCKET)
->rules(fn (Get $get) => ConnectSourceControl::rules($get())['password']),
Checkbox::make('global')
->label('Is Global (Accessible in all projects)'),
];
}
/**
* @throws Exception
*/
public static function action(SourceControl $provider, array $data): void
public static function action(\App\Models\SourceControl $sourceControl, array $data): void
{
try {
app(EditSourceControl::class)->edit($provider, auth()->user(), $data);
app(EditSourceControl::class)->edit($sourceControl, auth()->user(), $data);
} catch (Exception $e) {
Notification::make()
->title($e->getMessage())

View File

@ -57,8 +57,9 @@ public function getTable(): Table
EditAction::make('edit')
->label('Edit')
->modalHeading('Edit Source Control')
->mutateRecordDataUsing(function (array $data, SourceControl $record) {
->fillForm(function (array $data, SourceControl $record) {
return [
'provider' => $record->provider,
'name' => $record->profile,
'token' => $record->provider_data['token'] ?? null,
'username' => $record->provider_data['username'] ?? null,

View File

@ -0,0 +1,49 @@
<?php
namespace App\Web\Pages\Settings\Tags\Actions;
use App\Actions\Tag\CreateTag;
use App\Models\Tag;
use Exception;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
class Create
{
public static function form(): array
{
return [
TextInput::make('name')
->rules(fn ($get) => CreateTag::rules()['name']),
Select::make('color')
->prefixIcon('heroicon-s-tag')
->prefixIconColor(fn (Get $get) => $get('color'))
->searchable()
->options(
collect(config('core.tag_colors'))
->mapWithKeys(fn ($color) => [$color => $color])
)
->reactive()
->rules(fn ($get) => CreateTag::rules()['color']),
];
}
/**
* @throws Exception
*/
public static function action(array $data): Tag
{
try {
return app(CreateTag::class)->create(auth()->user(), $data);
} catch (Exception $e) {
Notification::make()
->title($e->getMessage())
->danger()
->send();
throw $e;
}
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Web\Pages\Settings\Tags\Actions;
use App\Actions\Tag\EditTag;
use App\Models\Tag;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
class Edit
{
public static function form(): array
{
return [
TextInput::make('name')
->rules(EditTag::rules()['name']),
Select::make('color')
->searchable()
->options(
collect(config('core.tag_colors'))
->mapWithKeys(fn ($color) => [$color => $color])
)
->rules(fn ($get) => EditTag::rules()['color']),
];
}
public static function action(Tag $tag, array $data): void
{
app(EditTag::class)->edit($tag, $data);
}
}

View File

@ -0,0 +1,61 @@
<?php
namespace App\Web\Pages\Settings\Tags\Actions;
use App\Actions\Tag\SyncTags;
use App\Models\Server;
use App\Models\Site;
use Filament\Forms\Components\Select;
use Filament\Infolists\Components\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Support\Enums\MaxWidth;
class EditTags
{
/**
* @param Site|Server $taggable
*/
public static function infolist(mixed $taggable): Action
{
return Action::make('edit_tags')
->icon('heroicon-o-pencil-square')
->tooltip('Edit Tags')
->hiddenLabel()
->modalSubmitActionLabel('Save')
->modalHeading('Edit Tags')
->modalWidth(MaxWidth::Medium)
->form([
Select::make('tags')
->default($taggable->tags()->pluck('tags.id')->toArray())
->options(function () {
return auth()->user()->currentProject->tags()->pluck('name', 'id')->toArray();
})
->nestedRecursiveRules(SyncTags::rules(auth()->user()->currentProject->id)['tags.*'])
->suffixAction(
\Filament\Forms\Components\Actions\Action::make('create_tag')
->icon('heroicon-o-plus')
->tooltip('Create a new tag')
->modalSubmitActionLabel('Create')
->modalHeading('Create Tag')
->modalWidth(MaxWidth::Medium)
->form(Create::form())
->action(function (array $data) {
Create::action($data);
}),
)
->multiple(),
])
->action(function (array $data) use ($taggable) {
app(SyncTags::class)->sync(auth()->user(), [
'taggable_id' => $taggable->id,
'taggable_type' => get_class($taggable),
'tags' => $data['tags'],
]);
Notification::make()
->success()
->title('Tags updated!')
->send();
});
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace App\Web\Pages\Settings\Tags;
use App\Models\Tag;
use App\Web\Components\Page;
use Filament\Actions\CreateAction;
use Filament\Notifications\Notification;
use Filament\Support\Enums\MaxWidth;
class Index extends Page
{
protected static ?string $navigationGroup = 'Settings';
protected static ?string $slug = 'settings/tags';
protected static ?string $title = 'Tags';
protected static ?string $navigationIcon = 'heroicon-o-tag';
protected static ?int $navigationSort = 7;
public static function canAccess(): bool
{
return auth()->user()?->can('viewAny', Tag::class) ?? false;
}
public function getWidgets(): array
{
return [
[Widgets\TagsList::class],
];
}
protected function getHeaderActions(): array
{
return [
CreateAction::make()
->label('Create')
->icon('heroicon-o-plus')
->modalHeading('Create a Tag')
->modalSubmitActionLabel('Save')
->createAnother(false)
->form(Actions\Create::form())
->authorize('create', Tag::class)
->modalWidth(MaxWidth::ExtraLarge)
->using(function (array $data) {
Actions\Create::action($data);
$this->dispatch('$refresh');
Notification::make()
->success()
->title('Tag created!')
->send();
}),
];
}
}

View File

@ -0,0 +1,78 @@
<?php
namespace App\Web\Pages\Settings\Tags\Widgets;
use App\Actions\Tag\DeleteTag;
use App\Models\Tag;
use App\Web\Pages\Settings\Tags\Actions\Edit;
use Filament\Support\Enums\MaxWidth;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Columns\ColorColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as Widget;
use Illuminate\Database\Eloquent\Builder;
class TagsList extends Widget
{
protected $listeners = ['$refresh'];
protected function getTableQuery(): Builder
{
return Tag::getByProjectId(auth()->user()->current_project_id);
}
protected function getTableColumns(): array
{
return [
ColorColumn::make('color'),
TextColumn::make('name')
->searchable()
->sortable(),
TextColumn::make('created_at')
->label('Created At')
->formatStateUsing(fn (Tag $record) => $record->created_at_by_timezone)
->searchable()
->sortable(),
];
}
public function table(Table $table): Table
{
return $table
->heading('')
->query($this->getTableQuery())
->columns($this->getTableColumns())
->actions([
$this->editAction(),
$this->deleteAction(),
]);
}
private function editAction(): Action
{
return EditAction::make('edit')
->fillForm(function (Tag $record) {
return [
'name' => $record->name,
'color' => $record->color,
'global' => $record->project_id === null,
];
})
->form(Edit::form())
->authorize(fn (Tag $record) => auth()->user()->can('update', $record))
->using(fn (array $data, Tag $record) => Edit::action($record, $data))
->modalWidth(MaxWidth::Medium);
}
private function deleteAction(): Action
{
return DeleteAction::make('delete')
->authorize(fn (Tag $record) => auth()->user()->can('delete', $record))
->using(function (Tag $record) {
app(DeleteTag::class)->delete($record);
});
}
}