Skip to content

Commit

Permalink
Merge pull request #68 from JanPalen/refactoring/OP-466-change-select…
Browse files Browse the repository at this point in the history
…-to-autocomplete

OP-466 - Add autocomplete to customer field
  • Loading branch information
senghe authored Aug 13, 2024
2 parents 0b4552d + d86a0b6 commit 09f8bbd
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ jobs:
name: Run PHPSpec
run: vendor/bin/phpspec run --ansi -f progress --no-interaction

-
name: Run PHPUnit
run: vendor/bin/phpunit --colors=always

-
name: Run Behat
run: vendor/bin/behat --colors --strict -vvv --no-interaction || vendor/bin/behat --colors --strict -vvv --no-interaction --rerun
Expand Down
62 changes: 53 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

----

[ ![](https://img.shields.io/packagist/l/bitbag/blacklist-plugin.svg) ](https://packagist.org/packages/bitbag/blacklist-plugin "License")
[ ![](https://img.shields.io/packagist/l/bitbag/blacklist-plugin.svg) ](https://packagist.org/packages/bitbag/blacklist-plugin "License")
[ ![](https://img.shields.io/packagist/v/bitbag/product-bundle-plugin.svg) ](https://packagist.org/packages/bitbag/blacklist-plugin "Version")
[ ![](https://img.shields.io/github/actions/workflow/status/BitBagCommerce/SyliusBlacklistPlugin/build.yml?branch=main) ](https://github.com/BitBagCommerce/SyliusBlacklistPlugin/actions "Build status")
[ ![](https://poser.pugx.org/bitbag/blacklist-plugin/downloads)](https://packagist.org/packages/bitbag/blacklist-plugin "Total Downloads")
[ ![Slack](https://img.shields.io/badge/community%20chat-slack-FF1493.svg)](http://sylius-devs.slack.com)
[ ![](https://img.shields.io/github/actions/workflow/status/BitBagCommerce/SyliusBlacklistPlugin/build.yml?branch=main) ](https://github.com/BitBagCommerce/SyliusBlacklistPlugin/actions "Build status")
[ ![](https://poser.pugx.org/bitbag/blacklist-plugin/downloads)](https://packagist.org/packages/bitbag/blacklist-plugin "Total Downloads")
[ ![Slack](https://img.shields.io/badge/community%20chat-slack-FF1493.svg)](http://sylius-devs.slack.com)
[ ![Support](https://img.shields.io/badge/support-contact%20author-blue])](https://bitbag.io/contact-us/?utm_source=github&utm_medium=referral&utm_campaign=plugins_blacklist)

At BitBag we do believe in open source. However, we are able to do it just because of our awesome clients, who are kind enough to share some parts of our work with the community. Therefore, if you feel like there is a possibility for us to work together, feel free to reach out. You will find out more about our professional services, technologies, and contact details at [https://bitbag.io/](https://bitbag.io/contact-us/?utm_source=github&utm_medium=referral&utm_campaign=plugins_blacklist).
Expand All @@ -22,10 +22,10 @@ Like what we do? Want to join us? Check out our job listings on our [career page
* [Overview](#overview)
* [Support](#we-are-here-to-help)
* [Installation](#installation)
* [Testing](#testing)
* [Customization](#Customization)
* [Testing](#testing)
* [Customization](#Customization)
* [About us](#about-us)
* [Community](#community)
* [Community](#community)
* [Demo](#demo-sylius-shop)
* [License](#license)
* [Contact](#contact)
Expand Down Expand Up @@ -202,7 +202,26 @@ sylius_customer:
customer:
classes:
model: App\Entity\Customer\Customer
repository: App\Repository\Customer\CustomerRepository
```
Create or edit `CustomerRepository.php` file:
```php
<?php
declare(strict_types=1);
namespace App\Repository\Customer;
use BitBag\SyliusBlacklistPlugin\Repository\CustomerRepositoryTrait;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\CustomerRepository as BaseCustomerRepository;
class CustomerRepository extends BaseCustomerRepository
{
use CustomerRepositoryTrait;
}
```

Override Customer grid:

```yaml
Expand Down Expand Up @@ -230,6 +249,31 @@ sylius_grid:
bitbag_sylius_blacklist_plugin.ui.blacklisted: Blacklisted
```

```yaml
# config/packages/twig.yaml
...
twig:
paths: ['%kernel.project_dir%/templates']
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
form_themes:
- '@BitBagSyliusBlacklistPlugin/Form/theme.html.twig'
- '@SyliusUi/Form/theme.html.twig'
services:
_defaults:
public: false
autowire: true
autoconfigure: true
Twig\Extra\Intl\IntlExtension: ~
when@test_cached:
twig:
strict_variables: true
```

Override Customer form template (`@SyliusAdminBundle\Customer\_form.html.twig` or `@SyliusAdminBundle/Customer/Form/_firstColumn.html.twig`) by adding lines below

```html
Expand All @@ -250,13 +294,13 @@ bin/console doctrine:migrations:migrate
Update your database schema:

```bash
doctrine:schema:update --dump-sql
bin/console doctrine:schema:update --dump-sql
```

If the list includes only changes for updating the database by adding 'fraud_status' you can use:

```bash
doctrine:schema:update -f
bin/console doctrine:schema:update -f
```

If there are another changes, please make sure they will not destroy your database schema.
Expand Down
4 changes: 2 additions & 2 deletions features/admin/adding_fraud_suspicion.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Feature: Adding fraud suspicion
And the customer chose "Free" shipping method to "United States" with "Cash on Delivery" payment
And I am logged in as an administrator

@ui
@ui @javascript
Scenario: Adding fraud suspicion
When I go to the create fraud suspicion page
And I fill all required fields basing on order "#00000022" address
Expand All @@ -28,4 +28,4 @@ Feature: Adding fraud suspicion
Then I select "Billing address" as address type
And I add comment "The customer places too much orders"
And I add it
Then I should be notified that the fraud suspicion has been created
Then I should be notified that the fraud suspicion has been created
49 changes: 49 additions & 0 deletions src/Form/Type/CustomerAutocompleteChoiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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 [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusBlacklistPlugin\Form\Type;

use Sylius\Bundle\ResourceBundle\Form\Type\ResourceAutocompleteChoiceType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class CustomerAutocompleteChoiceType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'resource' => 'sylius.customer',
'choice_name' => 'email',
'choice_value' => 'id',
]);
}

public function buildView(
FormView $view,
FormInterface $form,
array $options,
): void {
$view->vars['remote_criteria_type'] = 'contains';
$view->vars['remote_criteria_name'] = 'email';
}

public function getBlockPrefix(): string
{
return 'bitbag_sylius_customer_autocomplete_choice';
}

public function getParent(): string
{
return ResourceAutocompleteChoiceType::class;
}
}
16 changes: 7 additions & 9 deletions src/Form/Type/FraudSuspicionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,30 @@

use BitBag\SyliusBlacklistPlugin\Entity\FraudPrevention\FraudSuspicionInterface;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotNull;

final class FraudSuspicionType extends AbstractResourceType
{
/** @var string */
private $customerClass;

public function __construct(
string $dataClass,
string $customerClass,
array $validationGroups = [],
) {
parent::__construct($dataClass, $validationGroups);

$this->customerClass = $customerClass;
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('customer', EntityType::class, [
'class' => $this->customerClass,
->add('customer', CustomerAutocompleteChoiceType::class, [
'label' => 'sylius.ui.customer',
'priority' => 1,
'constraints' => [
new NotNull(),
],
])
->add('company', TextType::class, [
'required' => false,
Expand Down
25 changes: 25 additions & 0 deletions src/Repository/CustomerRepositoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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 [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusBlacklistPlugin\Repository;

trait CustomerRepositoryTrait
{
public function findByEmailPart(string $email, ?int $limit = null): array
{
return $this->createQueryBuilder('c')
->andWhere('c.email LIKE :email')
->setParameter('email', '%' . $email . '%')
->setMaxResults($limit)
->getQuery()
->getResult();
}
}
26 changes: 26 additions & 0 deletions src/Resources/config/routing/admin/fraud_suspicion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,29 @@ bitbag_sylius_blacklist_plugin_admin_order_mark_suspicious:
method: createForOrder
arguments:
- "expr:notFoundOnNull(service('sylius.repository.order').find($orderId))"

bitbag_sylius_blacklist_plugin_admin_ajax_customer_by_email:
path: /customer-search
methods: [GET]
defaults:
_controller: sylius.controller.customer::indexAction
_format: json
_sylius:
permission: true
repository:
method: findByEmailPart
arguments:
email: $email
limit: "!!int %bitbag.ajax.customer.autocomplete_limit%"

bitbag_sylius_blacklist_plugin_admin_ajax_customer_by_id:
path: /customer-search-by-id
methods: [GET]
defaults:
_controller: sylius.controller.customer::indexAction
_format: json
_sylius:
permission: true
repository:
method: findBy
arguments: [id: $id]
4 changes: 4 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<import resource="services/**/*.xml" />
</imports>

<parameters>
<parameter key="bitbag.ajax.customer.autocomplete_limit">25</parameter>
</parameters>

<services>
<service id="bitbag_sylius_blacklist_plugin.registry_blacklisting_rule_checker" class="Sylius\Component\Registry\ServiceRegistry">
<argument>BitBag\SyliusBlacklistPlugin\Checker\BlacklistingRule\BlacklistingRuleCheckerInterface</argument>
Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
</service>
<service id="bitbag.sylius_blacklist_plugin.form.type.fraud_suspicion" class="BitBag\SyliusBlacklistPlugin\Form\Type\FraudSuspicionType">
<argument>%bitbag_sylius_blacklist_plugin.model.fraud_suspicion.class%</argument>
<argument>%sylius.model.customer.class%</argument>
<argument>%bitbag.sylius_blacklist_plugin.form.type.validation_groups%</argument>
<tag name="form.type"/>
</service>
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/views/Form/theme.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends '@SyliusUi/Form/theme.html.twig' %}

{% block bitbag_sylius_customer_autocomplete_choice_row %}
{{ form_row(form, {
'remote_url': path('bitbag_sylius_blacklist_plugin_admin_ajax_customer_by_email'),
'load_edit_url': path('bitbag_sylius_blacklist_plugin_admin_ajax_customer_by_id')
}) }}
{% endblock %}
3 changes: 3 additions & 0 deletions tests/Application/config/packages/twig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ twig:
paths: ['%kernel.project_dir%/templates']
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
form_themes:
- '@BitBagSyliusBlacklistPlugin/Form/theme.html.twig'
- '@SyliusUi/Form/theme.html.twig'

services:
_defaults:
Expand Down
20 changes: 20 additions & 0 deletions tests/Application/src/Repository/CustomerRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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 [email protected].
*/

declare(strict_types=1);

namespace Tests\BitBag\SyliusBlacklistPlugin\Repository;

use BitBag\SyliusBlacklistPlugin\Repository\CustomerRepositoryTrait;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\CustomerRepository as BaseCustomerRepository;

class CustomerRepository extends BaseCustomerRepository
{
use CustomerRepositoryTrait;
}
3 changes: 2 additions & 1 deletion tests/Application/src/Resources/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ sylius_customer:
resources:
customer:
classes:
model: Tests\BitBag\SyliusBlacklistPlugin\Entity\Customer
model: Tests\BitBag\SyliusBlacklistPlugin\Entity\Customer
repository: Tests\BitBag\SyliusBlacklistPlugin\Repository\CustomerRepository
2 changes: 1 addition & 1 deletion tests/Behat/Context/Ui/Admin/FraudSuspicionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function iFillAllRequiredFieldsBasingOnOrderAddress(string $orderNumber):
$customerLastUsedAddress = $order->getBillingAddress();

$this->resolveCurrentPage()
->selectOption('Customer', $order->getCustomer()->getEmail())
->selectCustomer($order->getCustomer()->getEmail())
->fillField('Company', $customerLastUsedAddress->getCompany())
->fillField('Company', $customerLastUsedAddress->getCompany())
->fillField('First name', $customerLastUsedAddress->getFirstName())
Expand Down
28 changes: 28 additions & 0 deletions tests/Behat/Page/Admin/FraudSuspicion/CreatePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,32 @@ public function selectOption(string $field, string $value): CreatePageInterface

return $this;
}

public function selectCustomer(string $customerEmail): CreatePageInterface
{
$dropdown = $this->getElement('customer_dropdown');
$dropdown->click();

$dropdown->waitFor(1, function () use ($customerEmail) {
return $this->hasElement('customer_dropdown_item', [
'%item%' => $customerEmail,
]);
});

$item = $this->getElement('customer_dropdown_item', [
'%item%' => $customerEmail,
]);

$item->click();

return $this;
}

protected function getDefinedElements(): array
{
return array_merge(parent::getDefinedElements(), [
'customer_dropdown' => '.field > label:contains("Customer") ~ .sylius-autocomplete',
'customer_dropdown_item' => '.field > label:contains("Customer") ~ .sylius-autocomplete > div.menu > div.item:contains("%item%")',
]);
}
}
2 changes: 2 additions & 0 deletions tests/Behat/Page/Admin/FraudSuspicion/CreatePageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ interface CreatePageInterface extends BaseCreatePageInterface, ContainsErrorInte
public function fillField(string $field, ?string $value): self;

public function selectOption(string $field, string $value): self;

public function selectCustomer(string $customerEmail): CreatePageInterface;
}
Loading

0 comments on commit 09f8bbd

Please sign in to comment.