Skip to content
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
17 changes: 13 additions & 4 deletions src/Filament/Resources/Products/Schemas/ProductForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@
return ($state instanceof PlanKind ? $state : PlanKind::tryFrom((string) $state)) === $kind;
}

/** @param array<string, mixed> $state A price row: its currency is the enum once saved, a string while unsaved. */
private static function priceLabel(array $state): ?string
{
if (blank($state['amount'] ?? null) || blank($state['currency'] ?? null)) {
return null;
}

$currency = $state['currency'] instanceof Currency ? $state['currency'] : Currency::from($state['currency']);

return $currency->formatPrice((int) $state['amount'], $state['interval'] ?? null, (int) ($state['interval_count'] ?? 1));

Check warning on line 57 in src/Filament/Resources/Products/Schemas/ProductForm.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 129 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=saucebase-dev_billing&issues=AaGoY14e86HzVn2-072z&open=AaGoY14e86HzVn2-072z&pullRequest=38
}

/**
* The stored `{features, limits}` as one row per entitlement. `EditProduct`
* fills the form with it.
Expand Down Expand Up @@ -329,10 +341,7 @@
->defaultItems(0)
->columnSpanFull()
->collapsible()
->itemLabel(fn (array $state): ?string => filled($state['amount'] ?? null) && filled($state['currency'] ?? null)
? Currency::from($state['currency'])->formatPrice((int) $state['amount'], $state['interval'] ?? null, (int) ($state['interval_count'] ?? 1))
: null
),
->itemLabel(fn (array $state): ?string => self::priceLabel($state)),
]),

]],
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/ProductFormLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public function test_a_plan_is_created_through_the_wizard(): void
$this->assertTrue($plan->entitlements()->allows('export data'));
}

public function test_a_plan_with_a_price_opens_for_editing(): void
{
$plan = Product::factory()->create();
$plan->prices()->create(['amount' => 900, 'currency' => 'USD', 'interval' => 'month', 'interval_count' => 1]);

Livewire::test(EditProduct::class, ['record' => $plan->getKey()])
->assertOk()
->assertSee('$9.00');
}

public function test_the_wizard_stops_on_a_missing_name(): void
{
Livewire::test(CreateProduct::class)
Expand Down
Loading