Skip to content

Commit

Permalink
Merge pull request #25 from delyriand/feature/improve-slot-config-form
Browse files Browse the repository at this point in the history
Improve slot config form for more flexibility
  • Loading branch information
delyriand authored Mar 19, 2024
2 parents 6dd99f9 + 5f22a70 commit 00ee560
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 28 deletions.
41 changes: 29 additions & 12 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ global.MonsieurBizShippingSlotManager = class {
saveSlotUrl,
resetSlotUrl,
slotSelectError,
shippingSlotConfigSelects,
shippingSlotConfigContainers,
shippingSlotConfigSelectSelector,
) {
this.shippingMethodInputs = shippingMethodInputs;
this.nextStepButtons = nextStepButtons;
Expand All @@ -34,7 +35,8 @@ global.MonsieurBizShippingSlotManager = class {
this.saveSlotUrl = saveSlotUrl;
this.resetSlotUrl = resetSlotUrl;
this.slotSelectError = slotSelectError;
this.shippingSlotConfigSelects = shippingSlotConfigSelects;
this.shippingSlotConfigContainers = shippingSlotConfigContainers;
this.shippingSlotConfigSelectSelector = shippingSlotConfigSelectSelector;
this.previousSlot = null;
this.initShippingMethodInputs();
}
Expand All @@ -49,7 +51,12 @@ global.MonsieurBizShippingSlotManager = class {
this.initShippingMethodInput(shippingMethodInput);
}

this.shippingSlotConfigSelects.forEach(shippingSlotConfigSelect => {
this.shippingSlotConfigContainers.forEach(shippingSlotConfigContainer => {
let shippingSlotConfigSelect = shippingSlotConfigContainer.querySelector(this.shippingSlotConfigSelectSelector);
if (shippingSlotConfigSelect === null) {
return;
}

shippingSlotConfigSelect.addEventListener("change", function () {
let checkedShippingMethodInput = Array.from(this.shippingMethodInputs).find(shippingMethodInput => shippingMethodInput.checked);
if (checkedShippingMethodInput !== null) {
Expand Down Expand Up @@ -94,9 +101,9 @@ global.MonsieurBizShippingSlotManager = class {

let data = JSON.parse(this.responseText);

// Hide calendars and shipping slot config selects
// Hide calendars and shipping slot config containers
shippingSlotManager.hideCalendars();
shippingSlotManager.hideShippingSlotConfigSelects();
shippingSlotManager.hideShippingSlotConfigContainers();

// Authorize user to go to next step if no slot needed
if (typeof data.events === "undefined") {
Expand Down Expand Up @@ -211,9 +218,9 @@ global.MonsieurBizShippingSlotManager = class {
}
}

hideShippingSlotConfigSelects() {
for (let shippingSlotConfigSelect of this.shippingSlotConfigSelects) {
shippingSlotConfigSelect.style.display = "none";
hideShippingSlotConfigContainers() {
for (let shippingSlotConfigContainer of this.shippingSlotConfigContainers) {
shippingSlotConfigContainer.style.display = "none";
}
}

Expand Down Expand Up @@ -256,7 +263,12 @@ global.MonsieurBizShippingSlotManager = class {
initCalendar(calendarContainer, events, timezone, shippingMethodCode, shippingSlotConfigSelect) {
calendarContainer.style.display = "block";
if (shippingSlotConfigSelect) {
shippingSlotConfigSelect.style.display = "block";
let currentShippingSlotConfigContainer = Array.from(this.shippingSlotConfigContainers).find(
shippingSlotConfigContainer => shippingSlotConfigContainer.classList.contains(shippingMethodCode)
);
if (currentShippingSlotConfigContainer) {
currentShippingSlotConfigContainer.style.display = "block";
}
}
let shippingSlotManager = this;
let calendar = new Calendar(
Expand Down Expand Up @@ -345,8 +357,13 @@ global.MonsieurBizShippingSlotManager = class {
}

getShippingSlotConfigSelect(shippingMethodCode) {
return Array.from(this.shippingSlotConfigSelects).find(
shippingSlotConfigSelect => shippingSlotConfigSelect.name.includes(shippingMethodCode)
) ?? null;
for (let shippingSlotConfigContainer of this.shippingSlotConfigContainers) {
let shippingSlotConfigSelect = shippingSlotConfigContainer.querySelector(this.shippingSlotConfigSelectSelector);
if (shippingSlotConfigSelect !== null && shippingSlotConfigSelect.name.includes(shippingMethodCode)) {
return shippingSlotConfigSelect;
}
}

return null;
}
};
21 changes: 11 additions & 10 deletions src/Form/Type/ShippingSlotConfigsByMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace MonsieurBiz\SyliusShippingSlotPlugin\Form\Type;

use MonsieurBiz\SyliusShippingSlotPlugin\Entity\ShipmentInterface;
use MonsieurBiz\SyliusShippingSlotPlugin\Entity\ShippingMethodInterface as MonsieurBizShippingMethodInterface;
use MonsieurBiz\SyliusShippingSlotPlugin\Resolver\ShippingSlotConfigResolverInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Shipping\Model\ShippingMethodInterface;
Expand Down Expand Up @@ -48,15 +48,17 @@ public function __construct(
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$subject = $options['subject'] ?? null;
$currentShippingSlotConfig = $this->shippingSlotConfigResolver->getShippingSlotConfig($subject);
$currentShippingMethod = $this->getCurrentShippingMethod($subject);
foreach ($this->getShippingMethods($subject) as $shippingMethod) {
if (!$this->isShippingSlotMethod($shippingMethod)) {
continue;
}

$builder->add($shippingMethod->getCode(), ChoiceType::class, [
'choices' => $shippingMethod->getShippingSlotConfigs(),
'choice_label' => 'name',
'choice_value' => 'id',
'label' => false,
'data' => $currentShippingMethod === $shippingMethod ? $currentShippingSlotConfig : null,
'data' => $this->shippingSlotConfigResolver->getShippingSlotConfig($subject, $shippingMethod),
]);
}
}
Expand All @@ -81,12 +83,11 @@ private function getShippingMethods(?ShippingSubjectInterface $subject): array
return $this->repository->findAll();
}

private function getCurrentShippingMethod(?ShippingSubjectInterface $subject): ?ShippingMethodInterface
private function isShippingSlotMethod(ShippingMethodInterface $shippingMethod): bool
{
if (!$subject instanceof ShipmentInterface) {
return null;
}

return $subject->getMethod();
return null !== $shippingMethod->getCode()
&& $shippingMethod instanceof MonsieurBizShippingMethodInterface
&& !$shippingMethod->getShippingSlotConfigs()->isEmpty()
;
}
}
16 changes: 15 additions & 1 deletion src/Resolver/ShippingSlotConfigResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,29 @@
use MonsieurBiz\SyliusShippingSlotPlugin\Entity\ShipmentInterface as MonsieurBizShipmentInterface;
use MonsieurBiz\SyliusShippingSlotPlugin\Entity\ShippingSlotConfigInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;

final class ShippingSlotConfigResolver implements ShippingSlotConfigResolverInterface
{
public function getShippingSlotConfig(?ShipmentInterface $shipment): ?ShippingSlotConfigInterface
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getShippingSlotConfig(?ShipmentInterface $shipment, ShippingMethodInterface $shippingMethod): ?ShippingSlotConfigInterface
{
if (!$shipment instanceof MonsieurBizShipmentInterface) {
return null;
}
$currentShippingMethod = $this->getCurrentShippingMethod($shipment);
if ($currentShippingMethod !== $shippingMethod) {
return null;
}

return null !== $shipment->getSlot() ? $shipment->getSlot()->getShippingSlotConfig() : null;
}

private function getCurrentShippingMethod(?ShipmentInterface $subject): ?ShippingMethodInterface
{
/** @phpstan-ignore-next-line */
return $subject->getMethod();
}
}
3 changes: 2 additions & 1 deletion src/Resolver/ShippingSlotConfigResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

use MonsieurBiz\SyliusShippingSlotPlugin\Entity\ShippingSlotConfigInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;

interface ShippingSlotConfigResolverInterface
{
public function getShippingSlotConfig(?ShipmentInterface $shipment): ?ShippingSlotConfigInterface;
public function getShippingSlotConfig(?ShipmentInterface $shipment, ShippingMethodInterface $shippingMethod): ?ShippingSlotConfigInterface;
}
Loading

0 comments on commit 00ee560

Please sign in to comment.