Skip to content

Commit

Permalink
Option to show/hide District, City, PostalCode (simplcommerce#399)
Browse files Browse the repository at this point in the history
* Option to show/hide District, City, PostalCode when input address information
  • Loading branch information
tdavisjr authored and thiennn committed Feb 16, 2018
1 parent d8c92f8 commit 1672906
Show file tree
Hide file tree
Showing 17 changed files with 2,691 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public IActionResult List([FromBody] SmartTableParam param)
c.Id,
c.Name,
c.IsShippingEnabled,
c.IsBillingEnabled
c.IsBillingEnabled,
c.IsCityEnabled,
c.IsPostalCodeEnabled,
c.IsDistrictEnabled
});

return Json(countries);
Expand All @@ -83,6 +86,9 @@ public async Task<IActionResult> Get(long id)
Code3 = country.Code3,
IsBillingEnabled = country.IsBillingEnabled,
IsShippingEnabled = country.IsShippingEnabled,
IsCityEnabled = country.IsCityEnabled,
IsPostalCodeEnabled = country.IsPostalCodeEnabled,
IsDistrictEnabled = country.IsDistrictEnabled
};

return Json(model);
Expand All @@ -108,6 +114,9 @@ public async Task<IActionResult> Put(long id, [FromBody] CountryForm model)
country.Code3 = model.Code3;
country.IsShippingEnabled = model.IsShippingEnabled;
country.IsBillingEnabled = model.IsBillingEnabled;
country.IsCityEnabled = model.IsCityEnabled;
country.IsPostalCodeEnabled = model.IsPostalCodeEnabled;
country.IsDistrictEnabled = model.IsDistrictEnabled;

await _countryRepository.SaveChangesAsync();

Expand All @@ -126,8 +135,12 @@ public async Task<IActionResult> Post([FromBody] CountryForm model)
Code2 = model.Code2,
Code3 = model.Code3,
IsBillingEnabled = model.IsBillingEnabled,
IsShippingEnabled = model.IsShippingEnabled
};
IsShippingEnabled = model.IsShippingEnabled,
IsCityEnabled = model.IsCityEnabled,
IsPostalCodeEnabled = model.IsPostalCodeEnabled,
IsDistrictEnabled = model.IsDistrictEnabled

};

_countryRepository.Add(country);
await _countryRepository.SaveChangesAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public async Task<IActionResult> List()
AddressLine2 = x.Address.AddressLine1,
DistrictName = x.Address.District.Name,
StateOrProvinceName = x.Address.StateOrProvince.Name,
CountryName = x.Address.Country.Name
CountryName = x.Address.Country.Name,
DisplayCity = x.Address.Country.IsCityEnabled,
DisplayPostalCode = x.Address.Country.IsPostalCodeEnabled,
DisplayDistrict = x.Address.Country.IsDistrictEnabled
}).ToList();

foreach (var item in model)
Expand Down Expand Up @@ -207,7 +210,7 @@ public async Task<IActionResult> Delete(long id)
return NotFound();
}

if(currentUser.DefaultShippingAddressId == userAddress.Id)
if (currentUser.DefaultShippingAddressId == userAddress.Id)
{
currentUser.DefaultShippingAddressId = null;
}
Expand All @@ -220,16 +223,29 @@ public async Task<IActionResult> Delete(long id)

private void PopulateAddressFormData(UserAddressFormViewModel model)
{
model.Countries = _countryRepository.Query()
var shippableCountries = _countryRepository.Query()
.Where(x => x.IsShippingEnabled)
.OrderBy(x => x.Name)
.OrderBy(x => x.Name);

model.Countries = shippableCountries
.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString()
}).ToList();

var onlyShipableCountryId = model.CountryId > 0 ? model.CountryId : long.Parse(model.Countries.First().Value);

var selectedCountry = shippableCountries.FirstOrDefault(c => c.Id == model.CountryId);

if (selectedCountry != null)
{
model.DisplayCity = selectedCountry.IsCityEnabled;
model.DisplayDistrict = selectedCountry.IsDistrictEnabled;
model.DisplayPostalCode = selectedCountry.IsPostalCodeEnabled;
}


model.StateOrProvinces = _stateOrProvinceRepository
.Query()
.Where(x => x.CountryId == onlyShipableCountryId)
Expand All @@ -240,7 +256,7 @@ private void PopulateAddressFormData(UserAddressFormViewModel model)
Value = x.Id.ToString()
}).ToList();

if(model.StateOrProvinceId > 0)
if (model.StateOrProvinceId > 0)
{
model.Districts = _districtRepository
.Query()
Expand Down
7 changes: 7 additions & 0 deletions src/Modules/SimplCommerce.Module.Core/Models/Country.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ public class Country : EntityBase
public bool IsBillingEnabled { get; set; }

public bool IsShippingEnabled { get; set; }

public bool IsCityEnabled { get; set; } = true;

public bool IsPostalCodeEnabled { get; set; } = true;

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

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ public class CountryForm
public bool IsBillingEnabled { get; set; }

public bool IsShippingEnabled { get; set; }

public bool IsCityEnabled { get; set; }

public bool IsPostalCodeEnabled { get; set; }

public bool IsDistrictEnabled { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ public class UserAddressFormViewModel
public IList<SelectListItem> Districts { get; set; }

public IList<SelectListItem> Countries { get; set; }

public bool DisplayDistrict { get; set; } = true;

public bool DisplayPostalCode { get; set; } = true;

public bool DisplayCity { get; set; } = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@ public class UserAddressListItem
public string CountryName { get; set; }

public bool IsDefaultShippingAddress { get; set; }

public bool DisplayDistrict { get; set; }

public bool DisplayPostalCode { get; set; }

public bool DisplayCity { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
<div>
<strong>@address.ContactName </strong><br />
@address.AddressLine1 <br />
@address.DistrictName, @address.StateOrProvinceName <br />
@if(address.DisplayDistrict){
<text>@address.DistrictName,</text>
}
@address.StateOrProvinceName <br />
Phone: @address.Phone
</div>
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
</select>
</div>
</div>
@if (Model.DisplayDistrict)
{
<div class="form-group">
<label class="col-sm-2 control-label">@Localizer["District"]</label>
<div class="col-sm-10">
Expand All @@ -22,18 +24,28 @@
</select>
</div>
</div>
}

@if (Model.DisplayCity)
{
<div class="form-group">
<label class="col-sm-2 control-label">@Localizer["City"]</label>
<div class="col-sm-10">
<input asp-for="City" type="text" class="form-control">
</div>
</div>
}

@if (Model.DisplayPostalCode)
{
<div class="form-group">
<label class="col-sm-2 control-label">@Localizer["Postal Code"]</label>
<div class="col-sm-10">
<input asp-for="PostalCode" type="text" class="form-control">
</div>
</div>
}

<div class="form-group">
<label class="col-sm-2 control-label">@Localizer["Address"]</label>
<div class="col-sm-10">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ <h2 ng-if="vm.isEditMode">{{::vm.translate.get('Edit Country')}}</h2>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox" ng-model="vm.country.isCityEnabled"> {{::vm.translate.get('Display City')}}
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox" ng-model="vm.country.isPostalCodeEnabled"> {{::vm.translate.get('Display Postal Code')}}
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox" ng-model="vm.country.isDistrictEnabled"> {{::vm.translate.get('Display District')}}
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-primary" ng-click="vm.save()"><span class="glyphicon glyphicon-ok"></span> {{::vm.translate.get('Save')}}</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ <h2>{{::vm.translate.get('Countries')}}</h2>
<th st-sort="Name" class="sortable">{{::vm.translate.get('Name')}}</th>
<th st-sort="IsShippingEnabled" class="sortable" st-sort-default="reverse">{{::vm.translate.get('Is Shipping Enabled')}}</th>
<th st-sort="IsBillingEnabled" class="sortable">{{::vm.translate.get('Is Billing Enabled')}}</th>
<th st-sort="IsBillingEnabled" class="sortable">{{::vm.translate.get('Display City')}}</th>
<th st-sort="IsBillingEnabled" class="sortable">{{::vm.translate.get('Display Postal Code')}}</th>
<th st-sort="IsBillingEnabled" class="sortable">{{::vm.translate.get('Display District')}}</th>
<th class="sortable">&nbsp;</th>
</tr>
<tr>
Expand All @@ -30,10 +33,19 @@ <h2>{{::vm.translate.get('Countries')}}</h2>
<tr ng-repeat="country in vm.countries">
<td>{{country.name}}</td>
<td>
<input type="checkbox" ng-model="country.isShippingEnabled" ng-click="vm.toggleShippingOrBilling(country)">
<input type="checkbox" ng-model="country.isShippingEnabled" ng-click="vm.update(country)">
</td>
<td>
<input type="checkbox" ng-model="country.isBillingEnabled" ng-click="vm.toggleShippingOrBilling(country)">
<input type="checkbox" ng-model="country.isBillingEnabled" ng-click="vm.update(country)">
</td>
<td>
<input type="checkbox" ng-model="country.isCityEnabled" ng-click="vm.update(country)">
</td>
<td>
<input type="checkbox" ng-model="country.isPostalCodeEnabled" ng-click="vm.update(country)">
</td>
<td>
<input type="checkbox" ng-model="country.isDistrictEnabled" ng-click="vm.update(country)">
</td>
<td>
<a ui-sref="country-edit({id: country.id})" title="Edit" class="btn btn-primary btn-xs"> <span class="glyphicon glyphicon-pencil"></span></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
});
};

vm.toggleShippingOrBilling = function (country) {
vm.update = function (country) {
countryService.editCountry(country)
.then(function (result) {
toastr.success(country.name + ' has been updated');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ private void PopulateShippingForm(DeliveryInformationVm model, User currentUser)
AddressLine2 = x.Address.AddressLine1,
DistrictName = x.Address.District.Name,
StateOrProvinceName = x.Address.StateOrProvince.Name,
CountryName = x.Address.Country.Name
CountryName = x.Address.Country.Name,
DisplayCity = x.Address.Country.IsCityEnabled,
DisplayPostalCode = x.Address.Country.IsPostalCodeEnabled,
DisplayDistrict = x.Address.Country.IsDistrictEnabled
}).ToList();

model.ShippingAddressId = currentUser.DefaultShippingAddressId ?? 0;
Expand All @@ -174,6 +177,7 @@ private void PopulateShippingForm(DeliveryInformationVm model, User currentUser)
if (model.NewAddressForm.ShipableContries.Count == 1)
{
var onlyShipableCountryId = long.Parse(model.NewAddressForm.ShipableContries.First().Value);

model.NewAddressForm.StateOrProvinces = _stateOrProvinceRepository
.Query()
.Where(x => x.CountryId == onlyShipableCountryId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@ public class ShippingAddressVm
public long CountryId { get; set; }

public string CountryName { get; set; }

public bool DisplayDistrict { get; set; }

public bool DisplayPostalCode { get; set; }

public bool DisplayCity { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
<div class="col-md-8">
<form asp-controller="Checkout" asp-action="Shipping" method="POST" id="checkout-delivery-address" class="form-horizontal">
<h3>@Localizer["Shipping address"]</h3>
@foreach(var address in Model.ExistingShippingAddresses)
@foreach (var address in Model.ExistingShippingAddresses)
{
<div class="radio">
<label>
<input type="radio" name="shippingAddressId" value="@address.UserAddressId" checked="@(Model.ShippingAddressId == address.UserAddressId ? "checked" : null)">
<strong>@address.ContactName </strong><br />
@address.AddressLine1 <br />
@address.DistrictName, @address.StateOrProvinceName <br />
@if (address.DisplayDistrict)
{
<text>@address.DistrictName,</text>
}
@address.StateOrProvinceName <br />
Phone: @address.Phone
</label>
</div>
Expand Down Expand Up @@ -49,6 +53,7 @@
</select>
</div>
</div>

<div class="form-group">
<label class="col-sm-4 control-label">@Localizer["District"]</label>
<div class="col-sm-8">
Expand All @@ -57,18 +62,21 @@
</select>
</div>
</div>

<div class="form-group">
<label class="col-sm-4 control-label">@Localizer["City"]</label>
<div class="col-sm-8">
<input asp-for="NewAddressForm.City" type="text" class="form-control">
</div>
</div>

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

<div class="form-group">
<label class="col-sm-4 control-label">@Localizer["Address"]</label>
<div class="col-sm-8">
Expand Down
Loading

0 comments on commit 1672906

Please sign in to comment.