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/empty attributes #1776

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions packages/admin/src/Support/FieldTypes/Toggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class Toggle extends BaseFieldType

public static function getFilamentComponent(Attribute $attribute): Component
{
return ToggleInput::make($attribute->handle)->default('true')
return ToggleInput::make($attribute->handle)
->helperText(
$attribute->translate('description')
)
->live();
->default(false)
->rule('boolean');
}
}
11 changes: 8 additions & 3 deletions packages/admin/src/Support/Forms/AttributeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ public function getFilamentComponent(Attribute $attribute): Component
$attribute->type
] ?? TextField::class;

return $fieldType::getFilamentComponent($attribute)->label(
$attribute->translate('name')
)
/** @var Component $component */
$component = $fieldType::getFilamentComponent($attribute);

return $component
->label(
$attribute->translate('name')
)
->formatStateUsing(fn ($state) => ($state ?: new $attribute->type))
->mutateDehydratedStateUsing(fn ($state) => ($state ?: new $attribute->type))
->required($attribute->required)
->default($attribute->default_value);
}
Expand Down
119 changes: 59 additions & 60 deletions packages/admin/src/Support/Forms/Components/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Lunar\Admin\Support\Forms\Components;

use Closure;
use Filament\Forms;
use Illuminate\Database\Eloquent\Model;
use Livewire\Component as Livewire;
Expand All @@ -15,85 +14,85 @@

class Attributes extends Forms\Components\Group
{
public static function make(Closure|array $schema = []): static
protected function setUp(): void
{
return app(
static::class,
[
'schema' => ! blank($schema) ? $schema : function (\Filament\Forms\Get $get, Livewire $livewire, ?Model $record) {
$modelClass = $livewire::getResource()::getModel();
parent::setUp();

$productTypeId = null;
$this->key('attributeData');

$attributeQuery = Attribute::where('attribute_type', $modelClass);
if (blank($this->childComponents)) {
$this->schema(function (\Filament\Forms\Get $get, Livewire $livewire, ?Model $record) {
$modelClass = $livewire::getResource()::getModel();

// Products are unique in that they use product types to map attributes, so we need
// to try and find the product type ID
if ($modelClass == Product::class) {
$productTypeId = $record?->product_type_id ?: ProductType::first()->id;
$productTypeId = null;

// If we have a product type, the attributes should be based off that.
if ($productTypeId) {
$attributeQuery = ProductType::find($productTypeId)->productAttributes();
}
}
$attributeQuery = Attribute::where('attribute_type', $modelClass);

if ($modelClass == ProductVariant::class) {
$productTypeId = $record->product?->product_type_id ?: ProductType::first()->id;
// Products are unique in that they use product types to map attributes, so we need
// to try and find the product type ID
if ($modelClass == Product::class) {
$productTypeId = $record?->product_type_id ?: ProductType::first()->id;

// If we have a product type, the attributes should be based off that.
if ($productTypeId) {
$attributeQuery = ProductType::find($productTypeId)->variantAttributes();
}
// If we have a product type, the attributes should be based off that.
if ($productTypeId) {
$attributeQuery = ProductType::find($productTypeId)->productAttributes();
}
}

if ($modelClass == ProductVariant::class) {
$productTypeId = $record->product?->product_type_id ?: ProductType::first()->id;

$attributes = $attributeQuery->orderBy('position')->get();
// If we have a product type, the attributes should be based off that.
if ($productTypeId) {
$attributeQuery = ProductType::find($productTypeId)->variantAttributes();
}
}

$groups = AttributeGroup::where(
'attributable_type',
$modelClass
)->orderBy('position', 'asc')
->get()
->map(function ($group) use ($attributes) {
return [
'model' => $group,
'fields' => $attributes->groupBy('attribute_group_id')->get($group->id, []),
];
});
$attributes = $attributeQuery->orderBy('position')->get();

$groupComponents = [];
$groups = AttributeGroup::where(
'attributable_type',
$modelClass
)->orderBy('position', 'asc')
->get()
->map(function ($group) use ($attributes) {
return [
'model' => $group,
'fields' => $attributes->groupBy('attribute_group_id')->get($group->id, []),
];
});

foreach ($groups as $group) {
$sectionFields = [];
$groupComponents = [];

foreach ($group['fields'] as $field) {
$sectionFields[] = AttributeData::getFilamentComponent($field);
}
foreach ($groups as $group) {
$sectionFields = [];

$groupComponents[] = Forms\Components\Section::make($group['model']->translate('name'))
->schema($sectionFields);
foreach ($group['fields'] as $field) {
$sectionFields[] = AttributeData::getFilamentComponent($field);
}

return $groupComponents;
},
]
)
->configure()
->key('attributeData')
->mutateStateForValidationUsing(function ($state) {
if (! is_array($state)) {
return $state;
$groupComponents[] = Forms\Components\Section::make($group['model']->translate('name'))
->schema($sectionFields);
}

foreach ($state as $key => $value) {
if (! $value instanceof \Lunar\Base\Fieldtype) {
continue;
}
return $groupComponents;
});
}

$this->mutateStateForValidationUsing(function ($state) {
if (! is_array($state)) {
return $state;
}

$state[$key] = $value->getValue();
foreach ($state as $key => $value) {
if (! $value instanceof \Lunar\Base\Fieldtype) {
continue;
}

return $state;
});
$state[$key] = $value->getValue();
}

return $state;
});
}
}
15 changes: 5 additions & 10 deletions packages/core/src/FieldTypes/Toggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Toggle implements FieldType, JsonSerializable
{
/**
* @var string
* @var bool
*/
protected $value;

Expand All @@ -24,11 +24,11 @@ public function jsonSerialize(): mixed
}

/**
* Create a new instance of Text field type.
* Create a new instance of Toggle field type.
*
* @param string $value
*/
public function __construct($value = '')
public function __construct($value = false)
{
$this->setValue($value);
}
Expand All @@ -40,7 +40,7 @@ public function __construct($value = '')
*/
public function __toString()
{
return $this->getValue() ?? '';
return (int) ($this->getValue() ?? false);
}

/**
Expand Down Expand Up @@ -72,11 +72,6 @@ public function setValue($value)
*/
public function getConfig(): array
{
return [
'options' => [
'on_value' => 'nullable|string',
'off_value' => 'nullable|string',
],
];
return [];
}
}
Loading