Skip to content

Commit 731942d

Browse files
committed
Calling async methods, validations, delete confirmation.
1 parent 01c3f5a commit 731942d

File tree

9 files changed

+64
-35
lines changed

9 files changed

+64
-35
lines changed

src/Umbraco.Cms.Integrations.Automation.Zapier/App_Plugins/UmbracoCms.Integrations/Automation/Zapier/dashboard.html

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<div ng-controller="Umbraco.Cms.Integrations.Automation.Zapier.ZapConfigController as vm">
1+
<div ng-controller="Umbraco.Cms.Integrations.Automation.Zapier.ZapConfigController as vm">
22
<umb-box>
33
<umb-box-header title="Configuration"></umb-box-header>
44
<umb-content>
55
<div class="mt2 ml2">
6-
<input id="inWebHookUrl" type="text" ng-model="vm.webHookUrl" class="w-20 mb0" placeholder="WebHook URL"/>
6+
<input id="inWebHookUrl" type="text" ng-model="vm.webHookUrl" class="w-20 mb0" placeholder="WebHook URL" />
77
<select id="selContentTypes" ng-model="vm.selectedContentType" class="mb0">
88
<option value="">Please select a content type</option>
99
<option ng-repeat="item in vm.contentTypes" value="{{ item.alias }}">{{ item.name }}</option>
@@ -31,7 +31,7 @@
3131
<span>WebHook URL</span>
3232
</a>
3333
</div>
34-
<div class="umb-table-cell"></div>
34+
<div class="mr7"></div>
3535
</div>
3636
</div>
3737

@@ -46,10 +46,15 @@
4646
<div class="umb-table-cell">
4747
<span ng-bind="row.webHookUrl"></span>
4848
</div>
49-
<div class="umb-table-cell">
50-
<div class="umb-forms-mapping-remove -no-margin-right">
51-
<a href="javascript:void(0)" ng-click="vm.onDelete(row.id)"><i class="icon-trash"></i></a>
52-
</div>
49+
<div class="mr7" style="position: relative;">
50+
<a href="javascript:void(0)" ng-click="row.showDeletePrompt = true">
51+
<i class="icon-trash"></i>
52+
</a>
53+
<umb-confirm-action ng-if="row.showDeletePrompt"
54+
direction="left"
55+
on-confirm="vm.onDelete(row.id)"
56+
on-cancel="row.showDeletePrompt = false">
57+
</umb-confirm-action>
5358
</div>
5459
</div>
5560
</div>

src/Umbraco.Cms.Integrations.Automation.Zapier/App_Plugins/UmbracoCms.Integrations/Automation/Zapier/js/zapier-validation.service.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
function zapierValidationService() {
2+
3+
const resources = {
4+
WebHookEmpty: "WebHook Url is required.",
5+
WebHookUrlInvalid: "WebHook Url format is invalid.",
6+
ContentTypeEmpty: "Content type is required."
7+
};
8+
29
return {
3-
isValidConfig: (webHookUrl, contentTypeAlias) => {
10+
validateConfiguration: (webHookUrl, contentTypeAlias) => {
411

5-
if (webHookUrl === undefined || webHookUrl.length === 0) return false;
12+
if (webHookUrl === undefined || webHookUrl.length === 0) return resources.WebHookEmpty;
613

714
let url;
815

916
try {
1017
url = new URL(webHookUrl);
1118
}
1219
catch (_) {
13-
return false;
20+
return resources.WebHookUrlInvalid;
1421
}
1522

16-
if (!(url.protocol === "http:" || url.protocol === "https:")) return false;
23+
if (!(url.protocol === "http:" || url.protocol === "https:")) return resources.WebHookUrlInvalid;
1724

18-
if (contentTypeAlias === undefined || contentTypeAlias.length === 0) return false;
25+
if (contentTypeAlias === undefined || contentTypeAlias.length === 0) return resources.ContentTypeEmpty;
1926

20-
return true;
27+
return "";
2128
}
2229
}
2330
}

src/Umbraco.Cms.Integrations.Automation.Zapier/App_Plugins/UmbracoCms.Integrations/Automation/Zapier/js/zapier.controller.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
getContentConfigs();
1111

1212
vm.onAdd = function () {
13-
14-
const isValid =
15-
umbracoCmsIntegrationsAutomationZapierValidationService.isValidConfig(vm.webHookUrl,
13+
const validationResult =
14+
umbracoCmsIntegrationsAutomationZapierValidationService.validateConfiguration(vm.webHookUrl,
1615
vm.selectedContentType);
1716

18-
if (!isValid) {
19-
notificationsService.warning("Zapier Content Config", "Zapier configuration is invalid");
17+
if (validationResult.length > 0) {
18+
notificationsService.warning("Zapier Content Config", validationResult);
2019
return;
2120
}
2221

@@ -54,6 +53,8 @@
5453
vm.webHookUrl = "";
5554
vm.selectedContentType = "";
5655
}
56+
57+
5758
}
5859

5960
angular.module("umbraco")

src/Umbraco.Cms.Integrations.Automation.Zapier/Components/NewContentPublishedComponent.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Net.Http;
4-
4+
using System.Threading.Tasks;
55
using Umbraco.Cms.Integrations.Automation.Zapier.Services;
66
using Umbraco.Core;
77
using Umbraco.Core.Composing;
@@ -50,15 +50,17 @@ private void ContentServiceOnPublished(IContentService sender, ContentPublishedE
5050
foreach (var node in e.PublishedEntities)
5151
{
5252
var zapContentConfig = _zapConfigService.GetByAlias(node.ContentType.Alias);
53-
if (zapContentConfig == null) return;
53+
if (zapContentConfig == null) continue;
5454

5555
var content = new Dictionary<string, string>
5656
{
5757
{ Constants.Content.Name, node.ContentType.Name },
5858
{ Constants.Content.PublishDate, DateTime.UtcNow.ToString() }
5959
};
6060

61-
var result = _zapierService.TriggerAsync(zapContentConfig.WebHookUrl, content);
61+
var t = Task.Run(async () => await _zapierService.TriggerAsync(zapContentConfig.WebHookUrl, content));
62+
63+
var result = t.Result;
6264

6365
if(!string.IsNullOrEmpty(result))
6466
_logger.Error<NewContentPublishedComponent>(result);

src/Umbraco.Cms.Integrations.Automation.Zapier/Controllers/ZapConfigController.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using System.Web.Http;
@@ -34,11 +35,14 @@ public IEnumerable<ContentTypeDto> GetContentTypes()
3435

3536
var configEntities = _zapConfigService.GetAll().Select(p => p.ContentTypeAlias);
3637

37-
return contentTypes.Where(p => !configEntities.Contains(p.Alias)).Select(p => new ContentTypeDto
38-
{
39-
Alias = p.Alias,
40-
Name = p.Name
41-
});
38+
return contentTypes
39+
.Where(p => !configEntities.Contains(p.Alias))
40+
.OrderBy(p => p.Name)
41+
.Select(p => new ContentTypeDto
42+
{
43+
Alias = p.Alias,
44+
Name = p.Name
45+
});
4246
}
4347

4448
[HttpPost]
@@ -47,8 +51,10 @@ public string Add([FromBody] ContentConfigDto dto)
4751
var result = _zapConfigService.Add(dto);
4852
if (!string.IsNullOrEmpty(result)) return result;
4953

50-
result = _zapierService.TriggerAsync(dto.WebHookUrl,
51-
new Dictionary<string, string> {{Constants.Content.Name, dto.ContentTypeAlias}});
54+
var t = Task.Run(async () => await _zapierService.TriggerAsync(dto.WebHookUrl,
55+
new Dictionary<string, string> {{Constants.Content.Name, dto.ContentTypeAlias}}));
56+
57+
result = t.Result;
5258

5359
if (!string.IsNullOrEmpty(result)) return result;
5460

src/Umbraco.Cms.Integrations.Automation.Zapier/Models/Dtos/ContentConfigDto.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@ public ContentConfigDto(string webHookUrl)
1919

2020
[JsonProperty("webHookUrl")]
2121
public string WebHookUrl { get; set; }
22+
23+
24+
[JsonProperty("showDeletePrompt")]
25+
public bool ShowDeletePrompt { get; set; }
2226
}
2327
}

src/Umbraco.Cms.Integrations.Automation.Zapier/Services/ZapConfigService.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ public string Delete(int id)
7171
{
7272
using (var scope = _scopeProvider.CreateScope())
7373
{
74-
var entity = scope.Database.Query<ZapContentConfigTable.ZapContentConfig>().First(p => p.Id == id);
75-
76-
scope.Database.Delete(entity);
74+
var entity = scope.Database.Query<ZapContentConfigTable.ZapContentConfig>().FirstOrDefault(p => p.Id == id);
75+
if (entity != null)
76+
{
77+
scope.Database.Delete(entity);
78+
}
79+
7780
scope.Complete();
7881
}
7982

src/Umbraco.Cms.Integrations.Automation.Zapier/Services/ZapierService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Net.Http;
4+
using System.Threading.Tasks;
45

56
namespace Umbraco.Cms.Integrations.Automation.Zapier.Services
67
{
@@ -12,9 +13,9 @@ public class ZapierService
1213
// Access to the client within the class is via ClientFactory(), allowing us to mock the responses in tests.
1314
public static Func<HttpClient> ClientFactory = () => s_client;
1415

15-
public string TriggerAsync(string requestUri, Dictionary<string, string> content)
16+
public async Task<string> TriggerAsync(string requestUri, Dictionary<string, string> content)
1617
{
17-
var result = ClientFactory().PostAsync(requestUri, new FormUrlEncodedContent(content)).GetAwaiter().GetResult();
18+
var result = await ClientFactory().PostAsync(requestUri, new FormUrlEncodedContent(content));
1819

1920
return result.IsSuccessStatusCode ? string.Empty : result.ReasonPhrase;
2021
}

src/Umbraco.Cms.Integrations.Automation.Zapier/ZapierDashboard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Umbraco.Cms.Integrations.Automation.Zapier
77
{
8-
[Weight(-10)]
8+
[Weight(30)]
99
public class ZapierDashboard : IDashboard
1010
{
1111
public string Alias => "zapierDashboard";

0 commit comments

Comments
 (0)