-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CC-16986 Yves Cart: Dynamic Page Update without Full Page Reload. (#2…
…485) CC-16986 Yves Cart: Dynamic Page Update without Full Page Reload
- Loading branch information
1 parent
e387a35
commit a5296ec
Showing
8 changed files
with
501 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
src/SprykerShop/Yves/ShoppingListWidget/Controller/CartToShoppingListAsyncController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/SprykerShop/Yves/ShoppingListWidget/Controller/ShoppingListWidgetAsyncController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...Shop/Yves/ShoppingListWidget/Plugin/Router/ShoppingListWidgetAsyncRouteProviderPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ault/views/create-shopping-list-from-cart-async/create-shopping-list-from-cart-async.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
Oops, something went wrong.