71 lines
2.1 KiB
PHP
71 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\UserAddressesBundle\Twig\Components\Account;
|
|
|
|
use KupShop\ComponentsBundle\Attributes\Component;
|
|
use KupShop\ComponentsBundle\Attributes\Entrypoint;
|
|
use KupShop\ComponentsBundle\Attributes\Version;
|
|
use KupShop\ComponentsBundle\Twig\BaseComponent;
|
|
use KupShop\UserAddressesBundle\Entity\DeliveryAddressEntity;
|
|
use KupShop\UserAddressesBundle\Util\UserAddressesUtil;
|
|
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
|
|
use Symfony\UX\LiveComponent\Attribute\LiveAction;
|
|
use Symfony\UX\LiveComponent\Attribute\LiveArg;
|
|
use Symfony\UX\LiveComponent\Attribute\LiveListener;
|
|
use Symfony\UX\LiveComponent\Attribute\LiveProp;
|
|
use Symfony\UX\LiveComponent\ComponentToolsTrait;
|
|
use Symfony\UX\LiveComponent\DefaultActionTrait;
|
|
|
|
#[AsLiveComponent(template: '@UserAddresses/components/Account/UserAddress/UserAddress.1.html.twig')]
|
|
#[Component(1, [new Version(1)])]
|
|
#[Entrypoint('base')]
|
|
class UserAddress extends BaseComponent
|
|
{
|
|
use DefaultActionTrait;
|
|
use ComponentToolsTrait;
|
|
|
|
#[LiveProp(writable: true)]
|
|
public ?int $id = null;
|
|
|
|
#[LiveProp(writable: true)]
|
|
public bool $edit = false;
|
|
|
|
private ?DeliveryAddressEntity $address = null;
|
|
|
|
public function __construct(private readonly UserAddressesUtil $userAddressesUtil)
|
|
{
|
|
}
|
|
|
|
public function getAddress(bool $force = false): DeliveryAddressEntity
|
|
{
|
|
if ($this->address && !$force) {
|
|
return $this->address;
|
|
}
|
|
|
|
return $this->address = $this->userAddressesUtil->getDeliveryAddress($this->id);
|
|
}
|
|
|
|
#[LiveAction]
|
|
public function toggleEdit(): void
|
|
{
|
|
$this->edit = !$this->edit;
|
|
}
|
|
|
|
#[LiveListener('toggleEdit')]
|
|
public function toggleLiveEdit(#[LiveArg] ?int $id, #[LiveArg] ?int $newId = null): void
|
|
{
|
|
// Listeners are called for all components, so we need to check if the id matches
|
|
if ($this->id !== $id) {
|
|
return;
|
|
}
|
|
|
|
if ($newId) {
|
|
$this->id = $newId;
|
|
$this->emit('addressCreated');
|
|
}
|
|
$this->edit = !$this->edit;
|
|
}
|
|
}
|