Skip to content

Commit

Permalink
Merge pull request #97 from EaDesgin/feature/Romcity-Fix-admin
Browse files Browse the repository at this point in the history
Romcity: Fix admin select
  • Loading branch information
eadesignro authored Feb 15, 2021
2 parents b9d1c0d + 48e693e commit ed54421
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 50 deletions.
59 changes: 59 additions & 0 deletions Block/Checkout/CityUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Eadesigndev\RomCity\Block\Checkout;

use Magento\Backend\Block\Template;
use Eadesigndev\RomCity\Model\RomCityRepository;
use Eadesigndev\RomCity\Model\RomCity;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Backend\Block\Template\Context;

/**
* Class CityUpdater
* @package Eadesigndev\RomCity\Block\Checkout
*/
class CityUpdater extends Template
{
/** @var RomCityRepository */
private $romCityRepository;

/** @var SearchCriteriaBuilder */
private $searchCriteria;

/** @var SerializerInterface */
private $serializer;

public function __construct(
Context $context,
RomCityRepository $romCityRepository,
SearchCriteriaBuilder $searchCriteria,
SerializerInterface $serializer,
array $data = []
)
{
$this->searchCriteria = $searchCriteria;
$this->romCityRepository = $romCityRepository;
$this->serializer = $serializer;
parent::__construct($context, $data);
}

public function citiesJson()
{

$searchCriteriaBuilder = $this->searchCriteria;
$searchCriteria = $searchCriteriaBuilder->create();

$citiesList = $this->romCityRepository->getList($searchCriteria);
$items = $citiesList->getItems();

$return = [];

/** @var RomCity $item */
foreach ($items as $item) {
$return[] = ['region_id' => $item->getRegionId(), 'city_name' => $item->getCityName()];
}

return $this->serializer->serialize($return);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "eadesignro/romcity",
"description": "A module that adds checkout city field to checkout quote shipping address",
"type": "magento2-module",
"version": "2.0.0",
"version": "2.0.1",
"autoload": {
"files": [
"registration.php"
Expand Down
95 changes: 47 additions & 48 deletions view/adminhtml/web/js/city-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,64 @@ define([

var string = JSON.stringify($eaCitiesJson),
obj = JSON.parse(string),
romania = obj.RO;
cityInput = $("[name*='city']").val();

$(document).on('change', "[name*='region_id']", function () {
var region_id = $(this).val(),
region = romania[region_id],
regionName = this.name,
cityInputName = regionName.replace("region_id", "city");
$(document).ready(function (){
var region_id = $("[name*='region_id']").val();
var region = [];

if (region_id) {
var city = $("[name*='" + cityInputName + "']"),
cityHtml = city.parent().html(),
selectCity = cityHtml.replace("input", "select") + '</select>',
cityObject = $(selectCity),
htmlSelect = '<option></option>',
cityName,
options,
selectOptions,
initialInput = $('<input />', {
'class': 'admin__control-text',
'type': 'text',
'data-bind': 'value:value,' +
'hasFocus:focused,' +
'valueUpdate: valueUpdate,' +
'attr: {' +
'name: inputName,' +
'placeholder: placeholder,' +
'id: uid,' +
'disabled: disabled,' +
'maxlength: 255,' +
'event: {change: userChanges}',
'name': cityInputName,
'maxlength': '255'
});

cityObject.empty();
$.each(obj, function (index, value) {
if (value.region_id == region_id) {
region.push(value.city_name);
}
});
var city = $("[name*='city']"),
selectCity = city.replaceWith("<select class='required-entry select admin__control-select' name='city' id='city'>") + '</select>',
htmlSelect = '<option>Selectati localitatea</option>',
options;

$.each(region, function (index, value) {
if ($.isPlainObject(value)) {
$.each(value, function (index, romCity) {
if ( value == cityInput) {
options = '<option value="' + value + '" selected>' + value + '</option>';
} else {
options = '<option value="' + value + '">' + value + '</option>';
}

htmlSelect += options;
});

cityName = romCity.name;
options = '<option value=' + cityName + '>' + cityName + '</option>';
htmlSelect += options;
$('#city').append(htmlSelect);
}

})
});

$(document).on('change', "[name*='region_id']", function () {

var region_id = $(this).val(),
regionName = this.name,
cityInputName = regionName.replace("region_id", "city"),
region = [];

if (region_id) {
$.each(obj, function (index, value) {
if (value.region_id == region_id) {
region.push(value.city_name);
}
});
var city = $("[name*='" + cityInputName + "']"),
selectCity = city.replaceWith("<select class='required-entry select admin__control-select' name='"+cityInputName+"' id='city'>") + '</select>',
htmlSelect = '<option>Selectati localitatea</option>',
options;

selectOptions = cityObject.append(htmlSelect);
$.each(region, function (index, value) {
options = '<option value="' + value + '">' + value + '</option>';
htmlSelect += options;
});

if (region.cities === undefined) {
return;
}
if (Object.keys(region.cities).length !== 0) {
city.replaceWith(selectOptions);
} else {
city.replaceWith(initialInput);
}
$('#city').append(htmlSelect);
}

});
};
});
});
1 change: 0 additions & 1 deletion view/frontend/web/js/city-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ define([
options;

$.each(region, function (index, value) {
console.log(value+' - '+cityInput);
if ( value == cityInput.toUpperCase()) {
options = '<option value="' + value + '" selected>' + value + '</option>';
} else {
Expand Down

0 comments on commit ed54421

Please sign in to comment.