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

OP-297/Redirect to new wishlist after creation #259

Merged
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
3 changes: 1 addition & 2 deletions features/creating_new_wishlist.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ Feature: Creating a new wishlist
And I set new wishlist name "WishlistName"
Then I save new wishlist modal
Then I should wait for one second
Then I should be on "/wishlists"
And I should have 1 wishlists
Then I should be on new wishlist "WishlistName"
6 changes: 3 additions & 3 deletions features/editing_name_of_wishlist.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Feature: Editing wishlists name
And the store has a wishlist named "Wishlist1"
When I go to "/wishlists"
Then I should see "Wishlist1"
When I edit "Wishlist1"
And I edit wishlist name "Wishlist2"
When I save edit wishlist modal
When I press "wishlist-edit-button-Wishlist1"
And I fill in "edit_wishlist_name" with "Wishlist2"
When I press "edit_wishlist_save"
Then I go to "/wishlists"
And I should see "Wishlist2"
2 changes: 2 additions & 0 deletions spec/CommandHandler/Wishlist/CreateNewWishlistHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function it_creates_new_wishlist_for_user(
$wishlist->setName('New wishlist');

$wishlistRepository->add($wishlist)->shouldBeCalled();
$wishlist->getId()->willReturn(1);

$createNewWishlist = new CreateNewWishlist('New wishlist', 'test_channel_code');

Expand Down Expand Up @@ -119,6 +120,7 @@ public function it_creates_new_wishlist_for_guest(
$newWishlist->setChannel($channel)->shouldBeCalled();

$wishlistRepository->add($newWishlist)->shouldBeCalled();
$newWishlist->getId()->willReturn(1);

$createNewWishlist = new CreateNewWishlist('New wishlist', 'test_channel_code');

Expand Down
4 changes: 3 additions & 1 deletion src/CommandHandler/Wishlist/CreateNewWishlistHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
) {
}

public function __invoke(CreateNewWishlist $createNewWishlist): void
public function __invoke(CreateNewWishlist $createNewWishlist): int
{
$token = $this->tokenStorage->getToken();
$user = $this->tokenUserResolver->resolve($token);
Expand Down Expand Up @@ -82,5 +82,7 @@ public function __invoke(CreateNewWishlist $createNewWishlist): void
}

$this->wishlistRepository->add($wishlist);

return $wishlist->getId();
}
}
21 changes: 19 additions & 2 deletions src/Controller/Action/CreateNewWishlistAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class CreateNewWishlistAction
Expand All @@ -30,6 +32,7 @@ public function __construct(
private RequestStack $requestStack,
private TranslatorInterface $translator,
private ChannelContextInterface $channelContext,
private UrlGeneratorInterface $urlGenerator,
) {
}

Expand All @@ -49,7 +52,11 @@ public function __invoke(Request $request): Response
} else {
$createNewWishlist = new CreateNewWishlist($wishlistName, null);
}
$this->commandBus->dispatch($createNewWishlist);

$envelope = $this->commandBus->dispatch($createNewWishlist);
/** @var HandledStamp $handledStamp */
$handledStamp = $envelope->last(HandledStamp::class);
$result = $handledStamp->getResult();

/** @var Session $session */
$session = $this->requestStack->getSession();
Expand All @@ -66,8 +73,18 @@ public function __invoke(Request $request): Response
'error',
$this->translator->trans('bitbag_sylius_wishlist_plugin.ui.wishlist_name_already_exists'),
);

return new JsonResponse([]);
}

return new JsonResponse();
return new JsonResponse(
[
'url' => $this
->urlGenerator
->generate(
'bitbag_sylius_wishlist_plugin_shop_locale_wishlist_show_chosen_wishlist',
['wishlistId' => $result],
)],
);
}
}
17 changes: 8 additions & 9 deletions src/Resources/assets/shop/js/handleAddAnotherWishlistModal.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { WishlistModal } from './wishlistModal';
import {WishlistModal} from './wishlistModal';

const addWishlistBtn = document.querySelector('[data-bb-wishlist-add="add-another-wishlist"]');
const wishlistFormName = 'create_new_wishlist';

const setWishlistModal = () => {
addWishlistBtn.addEventListener('click', () => {
addWishlistBtn.addEventListener('click', () => {
new WishlistModal(
{
headerTitle: document.querySelector("[data-bb-wishlist-add-title]").dataset.bbWishlistAddTitle,
Expand Down Expand Up @@ -32,19 +32,18 @@ const setWishlistModal = () => {

const requestConfig = {
method: 'POST',
headers: headers,
headers: headers,
body: formData
}

try {
const response = await fetch(url, requestConfig);
const data = await response.json();

window.location.href = `${window.location.origin}${data.url}`;

} catch (error) {
console.error(error);
} finally {
location.reload();
}
}
},
}
).init();
Expand All @@ -55,7 +54,7 @@ const turnOnListener = () => {
if (!addWishlistBtn) {
return;
}

setWishlistModal();
};

Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: "services/**/*.yml" }
1 change: 1 addition & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
<argument type="service" id="request_stack"/>
<argument type="service" id="translator"/>
<argument type="service" id="sylius.context.channel"/>
<argument type="service" id="router.default"/>
<tag name="controller.service_arguments"/>
</service>

Expand Down
27 changes: 10 additions & 17 deletions tests/Behat/Context/Ui/WishlistContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Symfony\Component\Routing\RouterInterface;
use Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\ProductIndexPageInterface;
use Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\ProductShowPageInterface;
use Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\Wishlist\ChosenShowPageInterface;
use Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\Wishlist\IndexPageInterface;
use Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\WishlistPageInterface;
use Tests\BitBag\SyliusWishlistPlugin\Behat\Service\LoginerInterface;
Expand All @@ -58,6 +59,7 @@ public function __construct(
private ChannelRepositoryInterface $channelRepository,
private RepositoryInterface $shopUserRepository,
private IndexPageInterface $wishlistIndexPage,
private ChosenShowPageInterface $chosenShowPage,
) {
}

Expand Down Expand Up @@ -113,27 +115,18 @@ public function iSaveNewWishlistModal(): void
}

/**
* @When I edit wishlist name :wishlistName
* @Then I should be on new wishlist :wishlistName
*/
public function iEditWishlistName(string $wishlistName): void
public function iShouldBeOnNewWishlist(string $wishlistName): void
{
$this->wishlistIndexPage->fillEditWishlistName($wishlistName);
}
/** @var ?WishlistInterface $wishlist */
$wishlist = $this->wishlistRepository->findOneBy(['name' => $wishlistName]);

/**
* @When I edit :wishlistName
*/
public function iEditWishlist(string $wishlistName): void
{
$this->wishlistIndexPage->editWishlistName($wishlistName);
}
if (null === $wishlist) {
throw new WishlistNotFoundException();
}

/**
* @When I save edit wishlist modal
*/
public function iSaveEditWishlistModal(): void
{
$this->wishlistIndexPage->saveEditWishlist();
$this->chosenShowPage->verify(['wishlistId' => $wishlist->getId()]);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/Behat/Page/Shop/Wishlist/ChosenShowPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on hello@bitbag.io.
*/

declare(strict_types=1);

namespace Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\Wishlist;

use FriendsOfBehat\PageObjectExtension\Page\SymfonyPage;

final class ChosenShowPage extends SymfonyPage implements ChosenShowPageInterface
{
public function getRouteName(): string
{
return 'bitbag_sylius_wishlist_plugin_shop_locale_wishlist_show_chosen_wishlist';
}
}
18 changes: 18 additions & 0 deletions tests/Behat/Page/Shop/Wishlist/ChosenShowPageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on hello@bitbag.io.
*/

declare(strict_types=1);

namespace Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\Wishlist;

use FriendsOfBehat\PageObjectExtension\Page\SymfonyPageInterface;

interface ChosenShowPageInterface extends SymfonyPageInterface
{
}
6 changes: 6 additions & 0 deletions tests/Behat/Resources/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ services:
- "@sylius.repository.channel"
- "@sylius.repository.shop_user"
- "@bitbag_wishlist_plugin.behat.page.wishlist.index_page"
- "@bitbag_wishlist_plugin.behat.page.wishlist.chosen_show_page"

bitbag_sylius_cms_plugin.behat.page.shop.wishlist:
class: Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\WishlistPage
Expand All @@ -46,6 +47,11 @@ services:
parent: sylius.behat.symfony_page
public: false

bitbag_wishlist_plugin.behat.page.wishlist.chosen_show_page:
class: Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\Wishlist\ChosenShowPage
parent: sylius.behat.symfony_page
public: false

bitbag_sylius_cms_plugin.behat.page.shop.product_index:
class: Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\ProductIndexPage
parent: sylius.behat.page.shop.product.index
Expand Down
Loading