Skip to content

Commit

Permalink
CC-16986 Yves Cart: Dynamic Page Update without Full Page Reload. (#2…
Browse files Browse the repository at this point in the history
…485)

CC-16986 Yves Cart: Dynamic Page Update without Full Page Reload
  • Loading branch information
ilyakubanov authored May 22, 2024
1 parent e387a35 commit a5296ec
Show file tree
Hide file tree
Showing 8 changed files with 501 additions and 94 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": ">=8.1",
"spryker-shop/quick-order-page-extension": "^1.0.0",
"spryker-shop/shop-application": "^1.0.0",
"spryker-shop/shop-ui": "^1.54.0",
"spryker-shop/shop-ui": "^1.74.0",
"spryker/application": "^3.8.0",
"spryker/customer": "^7.4.0",
"spryker/kernel": "^3.52.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerShop\Yves\ShoppingListWidget\Controller;

use SprykerShop\Yves\ShopApplication\Controller\AbstractController;
use SprykerShop\Yves\ShoppingListWidget\Plugin\Router\ShoppingListWidgetAsyncRouteProviderPlugin;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* @method \SprykerShop\Yves\ShoppingListWidget\ShoppingListWidgetFactory getFactory()
*/
class CartToShoppingListAsyncController extends AbstractController
{
/**
* @var string
*/
protected const PARAM_ID_QUOTE = 'idQuote';

/**
* @var string
*/
protected const PARAM_SHOPPING_LIST_FROM_CART_FORM = 'shopping_list_from_cart_form';

/**
* @var string
*/
protected const GLOSSARY_KEY_SHOPPING_LIST_CART_ITEMS_ADD_SUCCESS = 'shopping_list.cart.items_add.success';

/**
* @var string
*/
protected const GLOSSARY_KEY_SHOPPING_LIST_CART_ITEMS_ADD_FAILED = 'shopping_list.cart.items_add.failed';

/**
* @var string
*/
protected const FLASH_MESSAGE_LIST_TEMPLATE_PATH = '@ShopUi/components/organisms/flash-message-list/flash-message-list.twig';

/**
* @var string
*/
protected const KEY_CONTENT = 'content';

/**
* @var string
*/
protected const KEY_MESSAGES = 'messages';

/**
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*
* @return void
*/
public function initialize(): void
{
parent::initialize();

$customerTransfer = $this->getFactory()
->getCustomerClient()
->getCustomer();

if ($customerTransfer === null || !$customerTransfer->getCompanyUserTransfer()) {
throw new NotFoundHttpException('Only company users are allowed to access this page');
}
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function createFromCartAction(Request $request)
{
$idQuote = $request->get(static::PARAM_SHOPPING_LIST_FROM_CART_FORM)[static::PARAM_ID_QUOTE];

$shoppingListFromCartForm = $this->getFactory()
->getShoppingListFromCartForm($idQuote)
->handleRequest($request);

if (!$shoppingListFromCartForm->isSubmitted() || !$shoppingListFromCartForm->isValid()) {
$this->addErrorMessage(static::GLOSSARY_KEY_SHOPPING_LIST_CART_ITEMS_ADD_FAILED);

return $this->getMessagesJsonResponse();
}

$this->getFactory()
->createCreateFromCartHandler()
->createShoppingListFromCart($shoppingListFromCartForm);

$this->addSuccessMessage(static::GLOSSARY_KEY_SHOPPING_LIST_CART_ITEMS_ADD_SUCCESS);

return $this->redirectResponseInternal(
ShoppingListWidgetAsyncRouteProviderPlugin::ROUTE_NAME_SHOPPING_LIST_ASYNC_CREATE_FROM_CART_VIEW,
[static::PARAM_ID_QUOTE => $idQuote],
);
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function viewAction(Request $request): JsonResponse
{
$idQuote = $request->get(static::PARAM_ID_QUOTE);
$shoppingListFromCartForm = $this->getFactory()
->getShoppingListFromCartForm($idQuote)
->handleRequest($request);

return $this->jsonResponse([
static::KEY_MESSAGES => $this->renderView(static::FLASH_MESSAGE_LIST_TEMPLATE_PATH)->getContent(),
static::KEY_CONTENT => $this->getTwig()->render(
'@ShoppingListWidget/views/create-shopping-list-from-cart-async/create-shopping-list-from-cart-async.twig',
$this->getViewData($shoppingListFromCartForm),
),
]);
}

/**
* @param \Symfony\Component\Form\FormInterface $shoppingListFromCartForm
*
* @return array<string, mixed>
*/
protected function getViewData(FormInterface $shoppingListFromCartForm): array
{
$isWidgetVisible = $this->getIsWidgetVisible();

return [
'isVisible' => $isWidgetVisible,
'form' => $isWidgetVisible ? $shoppingListFromCartForm->createView() : null,
];
}

/**
* @return bool
*/
protected function getIsWidgetVisible(): bool
{
$customerTransfer = $this->getFactory()->getCustomerClient()->getCustomer();

return $customerTransfer && $customerTransfer->getCompanyUserTransfer();
}

/**
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
protected function getMessagesJsonResponse(): JsonResponse
{
return $this->jsonResponse([
static::KEY_MESSAGES => $this->renderView(static::FLASH_MESSAGE_LIST_TEMPLATE_PATH)->getContent(),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerShop\Yves\ShoppingListWidget\Controller;

use Spryker\Yves\Kernel\View\View;
use SprykerShop\Yves\ShopApplication\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;

/**
* @method \SprykerShop\Yves\ShoppingListWidget\ShoppingListWidgetFactory getFactory()
*/
class ShoppingListWidgetAsyncController extends AbstractController
{
/**
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Spryker\Yves\Kernel\View\View
*/
public function navigationWidgetViewAction(Request $request): View
{
return $this->view(
$this->getViewData(),
[],
'@ShoppingListWidget/views/shopping-list-shop-list-async/shopping-list-shop-list-async.twig',
);
}

/**
* @return array<string, mixed>
*/
protected function getViewData(): array
{
return [
'shoppingListCollection' => $this->getFactory()
->getShoppingListSessionClient()
->getCustomerShoppingListCollection(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerShop\Yves\ShoppingListWidget\Plugin\Router;

use Spryker\Yves\Router\Plugin\RouteProvider\AbstractRouteProviderPlugin;
use Spryker\Yves\Router\Route\RouteCollection;

class ShoppingListWidgetAsyncRouteProviderPlugin extends AbstractRouteProviderPlugin
{
/**
* @var string
*/
public const ROUTE_NAME_SHOPPING_LIST_ASYNC_CREATE_FROM_CART = 'shopping-list/async/create-from-cart';

/**
* @var string
*/
public const ROUTE_NAME_SHOPPING_LIST_ASYNC_CREATE_FROM_CART_VIEW = 'shopping-list/async/create-from-cart/view';

/**
* @var string
*/
public const ROUTE_NAME_SHOPPING_LIST_ASYNC_NAVIGATION_WIDGET_VIEW = 'shopping-list/async/navigation-widget/view';

/**
* Specification:
* - Adds Routes to the RouteCollection.
*
* @api
*
* @param \Spryker\Yves\Router\Route\RouteCollection $routeCollection
*
* @return \Spryker\Yves\Router\Route\RouteCollection
*/
public function addRoutes(RouteCollection $routeCollection): RouteCollection
{
$routeCollection = $this->addShoppingListAsyncCreateFromCartRoute($routeCollection);
$routeCollection = $this->addShoppingListAsyncCreateFromCartViewRoute($routeCollection);
$routeCollection = $this->addShoppingListAsyncNavigationWidgetViewRoute($routeCollection);

return $routeCollection;
}

/**
* @uses \SprykerShop\Yves\ShoppingListWidget\Controller\CartToShoppingListAsyncController::createFromCartAction()
*
* @param \Spryker\Yves\Router\Route\RouteCollection $routeCollection
*
* @return \Spryker\Yves\Router\Route\RouteCollection
*/
protected function addShoppingListAsyncCreateFromCartRoute(RouteCollection $routeCollection): RouteCollection
{
$route = $this->buildRoute('/shopping-list/async/create-from-cart', 'ShoppingListWidget', 'CartToShoppingListAsync', 'createFromCartAction');
$routeCollection->add(static::ROUTE_NAME_SHOPPING_LIST_ASYNC_CREATE_FROM_CART, $route);

return $routeCollection;
}

/**
* @uses \SprykerShop\Yves\ShoppingListWidget\Controller\CartToShoppingListAsyncController::viewAction()
*
* @param \Spryker\Yves\Router\Route\RouteCollection $routeCollection
*
* @return \Spryker\Yves\Router\Route\RouteCollection
*/
protected function addShoppingListAsyncCreateFromCartViewRoute(RouteCollection $routeCollection): RouteCollection
{
$route = $this->buildRoute('/shopping-list/async/create-from-cart/view', 'ShoppingListWidget', 'CartToShoppingListAsync', 'viewAction');
$routeCollection->add(static::ROUTE_NAME_SHOPPING_LIST_ASYNC_CREATE_FROM_CART_VIEW, $route);

return $routeCollection;
}

/**
* @uses \SprykerShop\Yves\ShoppingListWidget\Controller\ShoppingListWidgetAsyncController::navigationWidgetViewAction()
*
* @param \Spryker\Yves\Router\Route\RouteCollection $routeCollection
*
* @return \Spryker\Yves\Router\Route\RouteCollection
*/
protected function addShoppingListAsyncNavigationWidgetViewRoute(RouteCollection $routeCollection): RouteCollection
{
$route = $this->buildRoute('/shopping-list/async/navigation-widget/view', 'ShoppingListWidget', 'ShoppingListWidgetAsync', 'navigationWidgetViewAction');
$routeCollection->add(static::ROUTE_NAME_SHOPPING_LIST_ASYNC_NAVIGATION_WIDGET_VIEW, $route);

return $routeCollection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends view('create-shopping-list-from-cart', 'ShoppingListWidget') %}

{% set _widget = {
form: form,
isVisible: isVisible,
}%}

{% set data = {
ajaxTriggerAttribute: true,
} %}

{% block template %}
{{ block('contentInner') }}
{% endblock %}
Loading

0 comments on commit a5296ec

Please sign in to comment.