Skip to content

Commit 5202ff0

Browse files
committed
#390 completed option to show/hide District, City, PostalCode when input address information
1 parent 1672906 commit 5202ff0

File tree

7 files changed

+56
-32
lines changed

7 files changed

+56
-32
lines changed

src/Modules/SimplCommerce.Module.Core/Controllers/CountryApiController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ public async Task<IActionResult> Post([FromBody] CountryForm model)
139139
IsCityEnabled = model.IsCityEnabled,
140140
IsPostalCodeEnabled = model.IsPostalCodeEnabled,
141141
IsDistrictEnabled = model.IsDistrictEnabled
142-
143142
};
144143

145144
_countryRepository.Add(country);

src/Modules/SimplCommerce.Module.Core/Controllers/UserAddressController.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ public async Task<IActionResult> List()
6363
return View(model);
6464
}
6565

66+
[HttpGet("api/country-states-provinces/{countryId}")]
67+
public async Task<IActionResult> Get(long countryId)
68+
{
69+
var country = await _countryRepository.Query().Include(x => x.StatesOrProvinces).FirstOrDefaultAsync(x => x.Id == countryId);
70+
if (country == null)
71+
{
72+
return NotFound();
73+
}
74+
75+
var model = new
76+
{
77+
CountryId = country.Id,
78+
CountryName = country.Name,
79+
country.IsBillingEnabled,
80+
country.IsShippingEnabled,
81+
country.IsCityEnabled,
82+
country.IsPostalCodeEnabled,
83+
country.IsDistrictEnabled,
84+
StatesOrProvinces = country.StatesOrProvinces.Select(x => new { x.Id, x.Name })
85+
};
86+
87+
return Json(model);
88+
}
89+
6690
[Route("user/address/create")]
6791
public IActionResult Create()
6892
{
@@ -227,28 +251,30 @@ private void PopulateAddressFormData(UserAddressFormViewModel model)
227251
.Where(x => x.IsShippingEnabled)
228252
.OrderBy(x => x.Name);
229253

254+
if (!shippableCountries.Any())
255+
{
256+
return;
257+
}
258+
230259
model.Countries = shippableCountries
231260
.Select(x => new SelectListItem
232261
{
233262
Text = x.Name,
234263
Value = x.Id.ToString()
235264
}).ToList();
236265

237-
var onlyShipableCountryId = model.CountryId > 0 ? model.CountryId : long.Parse(model.Countries.First().Value);
238-
239-
var selectedCountry = shippableCountries.FirstOrDefault(c => c.Id == model.CountryId);
240-
266+
var selectedShipableCountryId = model.CountryId > 0 ? model.CountryId : long.Parse(model.Countries.First().Value);
267+
var selectedCountry = shippableCountries.FirstOrDefault(c => c.Id == selectedShipableCountryId);
241268
if (selectedCountry != null)
242269
{
243270
model.DisplayCity = selectedCountry.IsCityEnabled;
244271
model.DisplayDistrict = selectedCountry.IsDistrictEnabled;
245272
model.DisplayPostalCode = selectedCountry.IsPostalCodeEnabled;
246273
}
247274

248-
249275
model.StateOrProvinces = _stateOrProvinceRepository
250276
.Query()
251-
.Where(x => x.CountryId == onlyShipableCountryId)
277+
.Where(x => x.CountryId == selectedShipableCountryId)
252278
.OrderBy(x => x.Name)
253279
.Select(x => new SelectListItem
254280
{

src/Modules/SimplCommerce.Module.Core/Models/Country.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SimplCommerce.Infrastructure.Models;
1+
using System.Collections.Generic;
2+
using SimplCommerce.Infrastructure.Models;
23

34
namespace SimplCommerce.Module.Core.Models
45
{
@@ -20,5 +21,7 @@ public class Country : EntityBase
2021

2122
public bool IsDistrictEnabled { get; set; } = true;
2223

24+
public IList<StateOrProvince> StatesOrProvinces { get; set; } = new List<StateOrProvince>();
25+
2326
}
2427
}

src/Modules/SimplCommerce.Module.Core/Views/UserAddress/_AddressForm.cshtml

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,38 @@
1414
</select>
1515
</div>
1616
</div>
17-
@if (Model.DisplayDistrict)
18-
{
19-
<div class="form-group">
17+
<div id="form-group-district" class="form-group @(@Model.DisplayDistrict ? "" : "hidden")">
2018
<label class="col-sm-2 control-label">@Localizer["District"]</label>
2119
<div class="col-sm-10">
2220
<select asp-for="DistrictId" asp-items="Model.Districts" class="form-control">
2321
<option value=""></option>
2422
</select>
2523
</div>
2624
</div>
27-
}
28-
29-
@if (Model.DisplayCity)
30-
{
31-
<div class="form-group">
25+
<div id="form-group-city" class="form-group @(@Model.DisplayCity ? "" : "hidden")">
3226
<label class="col-sm-2 control-label">@Localizer["City"]</label>
3327
<div class="col-sm-10">
3428
<input asp-for="City" type="text" class="form-control">
3529
</div>
3630
</div>
37-
}
38-
39-
@if (Model.DisplayPostalCode)
40-
{
41-
<div class="form-group">
31+
<div id="form-group-postalcode" class="form-group @(@Model.DisplayPostalCode ? "" : "hidden")">
4232
<label class="col-sm-2 control-label">@Localizer["Postal Code"]</label>
4333
<div class="col-sm-10">
4434
<input asp-for="PostalCode" type="text" class="form-control">
4535
</div>
4636
</div>
47-
}
48-
4937
<div class="form-group">
5038
<label class="col-sm-2 control-label">@Localizer["Address"]</label>
5139
<div class="col-sm-10">
5240
<input asp-for="AddressLine1" type="text" class="form-control">
5341
</div>
5442
</div>
55-
<div class="form-group">
43+
@*<div class="form-group">
5644
<label class="col-sm-2 control-label">@Localizer["Address Line 2"]</label>
5745
<div class="col-sm-10">
5846
<input asp-for="AddressLine2" type="text" class="form-control">
5947
</div>
60-
</div>
48+
</div>*@
6149
<div class="form-group">
6250
<label class="col-sm-2 control-label">@Localizer["Contact name"]</label>
6351
<div class="col-sm-10">

src/Modules/SimplCommerce.Module.Core/wwwroot/user-address.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
return;
1212
}
1313

14-
$.getJSON('/api/countries/' + selectedCountryId + '/states-provinces', function (data) {
14+
$.getJSON('/api/country-states-provinces/' + selectedCountryId, function (data) {
1515
var $stateOrProvinceSelect = $("#StateOrProvinceId");
1616
resetSelect($stateOrProvinceSelect);
1717

1818
var $districtSelect = $("#DistrictId");
1919
resetSelect($districtSelect);
2020

21-
$.each(data, function (index, option) {
21+
$.each(data.statesOrProvinces, function (index, option) {
2222
$stateOrProvinceSelect.append($("<option></option>").attr("value", option.id).text(option.name));
2323
});
24+
25+
$("#form-group-district").toggleClass("hidden", !data.isDistrictEnabled);
26+
$("#form-group-city").toggleClass("hidden", !data.isCityEnabled);
27+
$("#form-group-postalcode").toggleClass("hidden", !data.isPostalCodeEnabled);
2428
});
2529
});
2630

src/Modules/SimplCommerce.Module.Orders/Views/Checkout/Shipping.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
</div>
5555
</div>
5656

57-
<div class="form-group">
57+
<div id="form-group-district" class="form-group">
5858
<label class="col-sm-4 control-label">@Localizer["District"]</label>
5959
<div class="col-sm-8">
6060
<select asp-for="NewAddressForm.DistrictId" asp-items="Model.NewAddressForm.Districts" class="form-control">
@@ -63,14 +63,14 @@
6363
</div>
6464
</div>
6565

66-
<div class="form-group">
66+
<div id="form-group-city" class="form-group">
6767
<label class="col-sm-4 control-label">@Localizer["City"]</label>
6868
<div class="col-sm-8">
6969
<input asp-for="NewAddressForm.City" type="text" class="form-control">
7070
</div>
7171
</div>
7272

73-
<div class="form-group">
73+
<div id="form-group-postalcode" class="form-group">
7474
<label class="col-sm-4 control-label">@Localizer["Postal Code"]</label>
7575
<div class="col-sm-8">
7676
<input asp-for="NewAddressForm.PostalCode" type="text" class="form-control">

src/Modules/SimplCommerce.Module.Orders/wwwroot/checkout.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,20 @@
7777
$('#NewAddressForm_CountryId').on('change', function () {
7878
var countryId = this.value;
7979

80-
$.getJSON('/api/countries/' + countryId + '/states-provinces', function (data) {
80+
$.getJSON('/api/country-states-provinces/' + countryId, function (data) {
8181
var $stateOrProvinceSelect = $("#NewAddressForm_StateOrProvinceId");
8282
resetSelect($stateOrProvinceSelect);
8383

8484
var $districtSelect = $("#NewAddressForm_DistrictId");
8585
resetSelect($districtSelect);
8686

87-
$.each(data, function (index, option) {
87+
$.each(data.statesOrProvinces, function (index, option) {
8888
$stateOrProvinceSelect.append($("<option></option>").attr("value", option.id).text(option.name));
8989
});
90+
91+
$("#form-group-district").toggleClass("hidden", !data.isDistrictEnabled);
92+
$("#form-group-city").toggleClass("hidden", !data.isCityEnabled);
93+
$("#form-group-postalcode").toggleClass("hidden", !data.isPostalCodeEnabled);
9094
});
9195
});
9296

0 commit comments

Comments
 (0)