Time flies, and this blog of mine is long forgotten. But my career and work are still going, so I decided to continue my blogs on a separate site - devops-a-palooza.
Please join me there!
Time flies, and this blog of mine is long forgotten. But my career and work are still going, so I decided to continue my blogs on a separate site - devops-a-palooza.
Please join me there!
[HttpPost]
public void ReceiveContactIds(Guid[] contactIds) { ... }
private static void SendContactIds(IEnumerable<Guid> contactIds)
{
var credentials = CredentialCache.DefaultCredentials;
var handler = new HttpClientHandler { Credentials = credentials };
using (var client = new HttpClient(handler))
{
var content = new StringContent(
JsonConvert.SerializeObject(new { contactIds }),
UnicodeEncoding.UTF8,
"application/json");
var action = "http://intranet.local/ReceiveContactIds";
var result = client.PostAsync(action, content).Result;
result.EnsureSuccessStatusCode();
}
}
public class DuplicatesController : Controller
{
public ActionResult Index()
{
var model = new DuplicatesCollectionViewModel();
model.Duplicates = new List<DuplicateViewModel>
{
new DuplicateViewModel {ContactName = "Duplicate 1", ContactId = Guid.NewGuid(), Email = "mail1@mail.com", Reason = ""},
new DuplicateViewModel {ContactName = "Duplicate 2", ContactId = Guid.NewGuid(), Email = "mail1@mail.com", Reason = "same email"}
};
return View(model);
}
public JsonResult GetAnotherDuplicate()
{
var number = new Random().Next(100);
var result = new JsonResult
{
Data = new DuplicateViewModel
{
ContactName = string.Format("Duplicate {0}", number),
ContactId = Guid.NewGuid(),
Email = string.Format("mail{0}@mail.com", number),
Reason = "another one"
}
};
return result;
}
public void MergeContacts(List<Guid> duplicates, Guid originalId)
{
//do stuff
}
}
public class DuplicatesCollectionViewModel
{
public List<DuplicateViewModel> Duplicates { get; set; }
}
public class DuplicateViewModel
{
public Guid ContactId { get; set; }
public string ContactName { get; set; }
public string Email { get; set; }
public string Reason { get; set; }
}
For the frontend I will use jquery and knockout.
@model KnockoutRadiobutton.Models.DuplicatesCollectionViewModel
<div id="duplicatesForm">
<table id="duplicatesTable">
<thead>
<tr>
<th></th>
<th></th>
<th>Merge</th>
<th>Primary</th>
</tr>
</thead>
<tbody data-bind="foreach: duplicates">
<tr>
<td>
<span style="font-weight: bold" data-bind="text: contactName"></span>
</td>
<td>
<span data-bind="text: reason"></span>
</td>
<td>
<input type="checkbox" data-bind="value: contactId, checked: $parent.mergeItems, attr: {disabled: isDisabled}" />
</td>
<td>
<input type="radio" name="PrimaryContactRadioGroup" data-bind="value: contactId, checked: $parent.primaryItem" />
</td>
</tr>
<tr>
<td></td>
<td style="padding-top: 0; padding-bottom: 0;">
<span data-bind="text: email"></span>
</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div>
<input type="button" data-bind="click: addNewDuplicate" value="Add another one" />
<input type="button" data-bind="click: submitDuplicates" value="Send a merge request" />
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/knockout-3.4.0.js"></script>
<script>
function DuplicateContact(contactId, contactName, email, reason) {
var self = this;
self.contactId = contactId;
self.contactName = contactName;
self.email = email;
self.reason = reason;
self.isDisabled = ko.observable(false);
}
function DuplicatesViewModel() {
var self = this;
self.duplicates = ko.observableArray();
self.mergeItems = ko.observableArray();
self.primaryItem = ko.observable();
self.primaryItem.subscribe(function (item) {
//mark primary item as selected for merge
var isForMerge = ko.utils.arrayFirst(self.mergeItems(), function(mergeItem) {
return mergeItem === item;
});
if (!isForMerge) self.mergeItems.push(item);
//mark new primary item
ko.utils.arrayForEach(self.duplicates(), function(duplicate) {
duplicate.isDisabled(false);
if (duplicate.contactId === item) {
duplicate.isDisabled(true);
}
});
});
self.getPrimaryContact = function() {
var primaryContactId = self.primaryItem();
var filter = ko.utils.arrayFilter(self.duplicates(), function(duplicate) {
return duplicate.contactId === primaryContactId;
});
if (filter.length === 1) return filter[0].contactId;
return null;
};
self.addNewDuplicate = function() {
$.ajax({
type: "POST",
url: "@Url.Action("GetAnotherDuplicate")",
success: function(data) {
var matchDuplicate = ko.utils.arrayFirst(self.duplicates(), function(item) {
return data.ContactId === item.ContactId;
});
if (matchDuplicate) return;
self.duplicates.push(new DuplicateContact(
data.ContactId,
data.ContactName,
data.Email,
data.Reason));
}
});
};
self.submitDuplicates = function () {
var duplicates = self.mergeItems();
var primary = self.getPrimaryContact();
if (duplicates.length == 0) {
alert("You have to select duplicates!");
return;
}
if (!primary) {
alert("You have to select the primary contact!");
return;
}
$.ajax(
{
type: "POST",
url: "@Url.Action("MergeContacts")",
data: {
duplicates: duplicates,
originalId: primary,
},
dataType: "json",
traditional: true
});
};
}
$(function () {
var viewModel = new DuplicatesViewModel();
@foreach (var duplicate in Model.Duplicates)
{
<text>
viewModel.duplicates.push(new DuplicateContact(
'@duplicate.ContactId',
'@duplicate.ContactName',
'@duplicate.Email',
'@duplicate.Reason'));
</text>
}
ko.applyBindings(viewModel);
});
// allows debugging of dynamically loaded scripts
//# sourceURL=Duplicates.js
</script>