Skip to content

Commit

Permalink
feat: add config based fields
Browse files Browse the repository at this point in the history
  • Loading branch information
daurensky committed Apr 3, 2024
1 parent 0fb62a9 commit 88aac48
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 62 deletions.
13 changes: 11 additions & 2 deletions config/admin-kit-feedbacks.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<?php

// config for AdminKit/Feedbacks
// The label can be a translation key
return [
//
'fields' => [
'name' => [
'rules' => 'required|string|max:255',
'label' => 'Name',
],
'phone' => [
'rules' => 'required|string|min:18|max:18',
'label' => 'Phone',
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration {
public function up()
{
Schema::table('admin_kit_feedbacks', function (Blueprint $table) {
$table->dropColumn('name');
$table->dropColumn('phone');
$table->jsonb('fields');
});
}

public function down()
{
Schema::table('admin_kit_feedbacks', function (Blueprint $table) {
$table->dropColumn('fields');
$table->string('name');
$table->string('phone');
});
}
};
6 changes: 0 additions & 6 deletions resources/lang/en/feedbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
'plural_label' => 'Feedback',

'id' => 'ID',
'name' => 'Name',
'phone' => 'Phone',
'locale' => 'Preferred language',

'created_at' => 'Created At',
'updated_at' => 'Updated At',
],
'api' => [
'attributes' => [
'name' => 'name',
'phone' => 'phone',
],
'messages' => [
'required' => 'The :attribute field is required.',
'max' => [
Expand Down
6 changes: 0 additions & 6 deletions resources/lang/ru/feedbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
'plural_label' => 'Обратная связь',

'id' => 'ID',
'name' => 'Имя',
'phone' => 'Телефон',
'locale' => 'Предпочитаемый язык',

'created_at' => 'Создан',
'updated_at' => 'Обновлен',
],
'api' => [
'attributes' => [
'name' => 'имя',
'phone' => 'номер телефона',
],
'messages' => [
'required' => 'Поле :attribute обязательно для заполнения.',
'max' => [
Expand Down
7 changes: 5 additions & 2 deletions src/Models/Feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ class Feedback extends AbstractModel
protected $table = 'admin_kit_feedbacks';

protected $fillable = [
'name',
'phone',
'fields',
'locale',
];

protected $casts = [
'fields' => 'array'
];

protected static function newFactory(): FeedbackFactory
{
return new FeedbackFactory();
Expand Down
8 changes: 4 additions & 4 deletions src/UI/API/Controllers/FeedbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

namespace AdminKit\Feedbacks\UI\API\Controllers;

use Illuminate\Http\Response;
use AdminKit\Feedbacks\Models\Feedback;
use AdminKit\Feedbacks\UI\API\Requests\FeedbackStoreRequest;
use Illuminate\Http\Response;

class FeedbackController extends Controller
{
public function store(FeedbackStoreRequest $request): Response
{
Feedback::query()
->create(
$request->merge([
'locale' => app()->getLocale(),
])->toArray()
collect(['fields' => $request->validated()])
->merge(['locale' => app()->getLocale()])
->toArray()
);

return response('OK');
Expand Down
35 changes: 22 additions & 13 deletions src/UI/API/Requests/FeedbackStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@

namespace AdminKit\Feedbacks\UI\API\Requests;

use Illuminate\Support\Collection;
use Illuminate\Foundation\Http\FormRequest;

class FeedbackStoreRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'phone' => 'required|string|min:18|max:18',
recaptchaFieldName() => recaptchaRuleName(),
];
return collect(config('admin-kit-feedbacks.fields'))
->mapWithKeys(fn (array $item, string $key) => [
$key => __($item['rules']),
])
->when(
app()->environment('production'),
fn (Collection $rules) => $rules->merge([
recaptchaFieldName() => recaptchaRuleName(),
])
)
->toArray();
}

public function attributes(): array
{
return collect(config('admin-kit-feedbacks.fields'))
->filter(fn (array $item) => isset($item['label']))
->mapWithKeys(fn (array $item, string $key) => [
$key => __($item['label']),
])
->toArray();
}

public function messages(): array
Expand All @@ -25,12 +42,4 @@ public function messages(): array
'recaptcha' => __('admin-kit-feedbacks::feedbacks.api.messages.recaptcha'),
];
}

public function attributes(): array
{
return [
'name' => __('admin-kit-feedbacks::feedbacks.api.attributes.name'),
'phone' => __('admin-kit-feedbacks::feedbacks.api.attributes.phone'),
];
}
}
40 changes: 17 additions & 23 deletions src/UI/Filament/Resources/FeedbackResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace AdminKit\Feedbacks\UI\Filament\Resources;

use Filament\Tables;
use Filament\Resources\Resource;
use AdminKit\Feedbacks\Models\Feedback;
use AdminKit\Feedbacks\UI\Filament\Resources\FeedbackResource\Pages;
use Filament\Resources\Resource;
use Filament\Tables;

class FeedbackResource extends Resource
{
Expand All @@ -16,27 +16,21 @@ class FeedbackResource extends Resource
public static function table(Tables\Table $table): Tables\Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->label(__('admin-kit-feedbacks::feedbacks.resource.name')),
Tables\Columns\TextColumn::make('phone')
->label(__('admin-kit-feedbacks::feedbacks.resource.phone')),
Tables\Columns\TextColumn::make('locale')
->label(__('admin-kit-feedbacks::feedbacks.resource.locale')),
Tables\Columns\TextColumn::make('created_at')
->label(__('admin-kit-feedbacks::feedbacks.resource.created_at')),
])
->defaultSort('id', 'desc')
->filters([
//
]);
}

public static function getRelations(): array
{
return [
//
];
->columns(
collect(config('admin-kit-feedbacks.fields'))
->map(
fn (array $item, string $key) => Tables\Columns\TextColumn::make("fields.$key")
->label(__($item['label'] ?? $key))
)
->merge([
Tables\Columns\TextColumn::make('locale')
->label(__('admin-kit-feedbacks::feedbacks.resource.locale')),
Tables\Columns\TextColumn::make('created_at')
->label(__('admin-kit-feedbacks::feedbacks.resource.created_at')),
])
->toArray()
)
->defaultSort('id', 'desc');
}

public static function getPages(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

namespace AdminKit\Feedbacks\UI\Filament\Resources\FeedbackResource\Pages;

use AdminKit\Feedbacks\UI\Filament\Resources\FeedbackResource;
use Filament\Resources\Pages\ListRecords;
use AdminKit\Feedbacks\UI\Filament\Resources\FeedbackResource;

class ListFeedback extends ListRecords
{
protected static string $resource = FeedbackResource::class;

protected function getHeaderActions(): array
{
return [];
}
}

0 comments on commit 88aac48

Please sign in to comment.