Skip to content

Commit 7d87d29

Browse files
committed
UI and JS functionality updates; Deleting Algolia index operation.
1 parent 16a3bec commit 7d87d29

File tree

9 files changed

+107
-40
lines changed

9 files changed

+107
-40
lines changed

src/Umbraco.Cms.Integrations.Search.Algolia/App_Plugins/UmbracoCms.Integrations/Search/Algolia/js/dashboard.controller.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,23 @@
2020
vm.search = search;
2121

2222
function init() {
23-
// contentData property:
24-
// array of objects:
25-
// contentType -> string value
26-
// contentTypeIcon -> string value
27-
// properties -> string array
23+
/* contentData property:
24+
[
25+
{
26+
"contentType": {
27+
"alias": "",
28+
"name": "",
29+
"icon": ""
30+
},
31+
"properties": [
32+
{
33+
"alias": "",
34+
"name": ""
35+
}
36+
]
37+
}
38+
]
39+
*/
2840
vm.manageIndex = {
2941
id: 0,
3042
name: "",
@@ -49,10 +61,10 @@
4961
algoliaService.getPropertiesByContentTypeId(contentType.id, (response) => {
5062
this.propertiesList = response;
5163

52-
var contentTypeData = this.contentData.find(obj => obj.contentType == contentType.alias);
64+
var contentTypeData = this.contentData.find(obj => obj.contentType.alias == contentType.alias);
5365
if (contentTypeData && contentTypeData.properties.length > 0) {
5466
vm.manageIndex.propertiesList = vm.manageIndex.propertiesList.map((obj) => {
55-
if (contentTypeData.properties.find(p => p == obj.alias)) {
67+
if (contentTypeData.properties.find(p => p.alias == obj.alias)) {
5668
obj.selected = true;
5769
}
5870

@@ -63,7 +75,7 @@
6375
},
6476
removeContentType: function (contentType) {
6577

66-
const contentTypeIndex = this.contentData.map(obj => obj.contentType).indexOf(contentType.alias);
78+
const contentTypeIndex = this.contentData.map(obj => obj.contentType.alias).indexOf(contentType.alias);
6779
this.contentData.splice(contentTypeIndex, 1);
6880

6981
this.selectedContentType = {};
@@ -77,7 +89,7 @@
7789
},
7890
selectProperty: function (property) {
7991

80-
var contentDataItem = vm.manageIndex.contentData.find(obj => obj.contentType == vm.manageIndex.selectedContentType.alias);
92+
var contentDataItem = vm.manageIndex.contentData.find(obj => obj.contentType.alias == vm.manageIndex.selectedContentType.alias);
8193

8294
var selected = !property.selected;
8395
if (selected) {
@@ -87,8 +99,11 @@
8799
// check if content type exists in the contentData array
88100
if (!contentDataItem) {
89101
var contentItem = {
90-
contentType: vm.manageIndex.selectedContentType.alias,
91-
contentTypeIcon: vm.manageIndex.selectedContentType.icon,
102+
contentType: {
103+
alias: vm.manageIndex.selectedContentType.alias,
104+
name: vm.manageIndex.selectedContentType.name,
105+
icon: vm.manageIndex.selectedContentType.icon
106+
},
92107
properties: []
93108
};
94109
vm.manageIndex.contentData.push(contentItem);
@@ -103,23 +118,30 @@
103118
}
104119

105120
// add property
106-
vm.manageIndex.contentData.find(obj => obj.contentType == vm.manageIndex.selectedContentType.alias).properties.push(property.alias);
121+
vm.manageIndex.contentData
122+
.find(obj => obj.contentType.alias == vm.manageIndex.selectedContentType.alias)
123+
.properties.push({
124+
alias: property.alias,
125+
name: property.name
126+
});
107127
}
108128
else {
109129
// deselect item
110130
vm.manageIndex.propertiesList.find(obj => obj.alias == property.alias).selected = false;
111131

112132
// remove property item
113133
const propertyIndex = vm.manageIndex.contentData
114-
.find(obj => obj.contentType == vm.manageIndex.selectedContentType.alias).properties.indexOf(property.alias);
134+
.find(obj => obj.contentType.alias == vm.manageIndex.selectedContentType.alias)
135+
.properties.map(obj => obj.alias).indexOf(property.alias);
115136
vm.manageIndex.contentData
116-
.find(obj => obj.contentType == vm.manageIndex.selectedContentType.alias).properties.splice(propertyIndex, 1);
137+
.find(obj => obj.contentType.alias == vm.manageIndex.selectedContentType.alias).properties.splice(propertyIndex, 1);
117138

118139
// remove content type item with no properties and deselect
119-
if (vm.manageIndex.contentData.find(obj => obj.contentType == vm.manageIndex.selectedContentType.alias).properties.length == 0) {
140+
if (vm.manageIndex.contentData.find(obj => obj.contentType.alias == vm.manageIndex.selectedContentType.alias).properties.length == 0) {
120141
vm.manageIndex.contentTypesList.find(obj => obj.alias == vm.manageIndex.selectedContentType.alias).selected = false;
142+
vm.manageIndex.contentTypesList.find(obj => obj.alias == vm.manageIndex.selectedContentType.alias).allowRemove = false;
121143

122-
const contentTypeIndex = vm.manageIndex.contentData.map(obj => obj.contentType).indexOf(vm.manageIndex.selectedContentType.alias);
144+
const contentTypeIndex = vm.manageIndex.contentData.map(obj => obj.contentType.alias).indexOf(vm.manageIndex.selectedContentType.alias);
123145
vm.manageIndex.contentData.splice(contentTypeIndex, 1);
124146
}
125147
}
@@ -194,7 +216,7 @@
194216
for (var i = 0; i < vm.manageIndex.contentData.length; i++) {
195217

196218
vm.manageIndex.contentTypesList.forEach(obj => {
197-
if (obj.alias == vm.manageIndex.contentData[i].contentType) {
219+
if (obj.alias == vm.manageIndex.contentData[i].contentType.alias) {
198220
obj.selected = true;
199221
obj.allowRemove = true;
200222
}

src/Umbraco.Cms.Integrations.Search.Algolia/App_Plugins/UmbracoCms.Integrations/Search/Algolia/views/dashboard.html

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@
2727
<p>
2828
To get started, you need to create an index and define the content schema - document types and properties.
2929
Then you can build your index, push data to Algolia and run searches across created indices.
30-
<br/>
31-
<a style="text-decoration: underline" href="https://www.algolia.com/doc/guides/getting-started/quick-start/">read more</a>
30+
<br />
31+
<a style="text-decoration: underline" target="_blank" href="https://www.algolia.com/doc/guides/getting-started/quick-start/">
32+
Read more about integrating Algolia Search
33+
</a>
3234
</p>
3335
</div>
3436
<div class="umb-panel-group__details-check-description">
3537
<umb-button type="button"
3638
button-style="primary"
3739
action="vm.addIndex()"
38-
label="Add">
40+
label="Add New Index Definition">
3941
</umb-button>
4042
</div>
4143
</div>
@@ -57,11 +59,11 @@
5759
</td>
5860
<td>
5961
<umb-node-preview ng-repeat="item in index.contentData"
60-
icon="item.contentTypeIcon"
61-
name="item.contentType"
62-
alias="item.contentType"
62+
icon="item.contentType.icon"
63+
name="item.contentType.name"
64+
alias="item.contentType.alias"
6365
published="true"
64-
description="item.properties.join(', ')">
66+
description="item.propertiesDescription.join(', ')">
6567
</umb-node-preview>
6668
</td>
6769
<td>
@@ -100,16 +102,12 @@
100102
<div class="umb-panel-group__details-group">
101103
<div class="umb-panel-group__details-group-title">
102104
<div class="umb-panel-group__details-group-name">
103-
Manage Index
105+
{{ vm.manageIndex.id == 0 ? "Add" : "Edit" }} Index Definition
104106
</div>
105107
</div>
106108
<div class="umb-panel-group__details-checks">
107109
<!-- Content Type, Index Name -->
108110
<div class="umb-panel-group__details-check">
109-
<div class="umb-panel-group__details-check-title">
110-
<div class="umb-panel-group__details-check-name">Content Types</div>
111-
<div class="umb-panel-group__details-check-description">Please select the content types you want to index</div>
112-
</div>
113111
<div class="umb-panel-group__details-status">
114112
<div class="umb-panel-group__details-status-content">
115113
<div class="row">
@@ -126,6 +124,9 @@
126124
<div class="flex justify-start">
127125
<div class="mr5" ng-model="vm.manageIndex.contentTypesList">
128126
<h5>Document Types</h5>
127+
<div class="umb-panel-group__details-check-description">
128+
Please select the document types you would like to index,<br/> and click <b>Open</b> to choose the fields to include.
129+
</div>
129130
<umb-node-preview ng-repeat="contentType in vm.manageIndex.contentTypesList"
130131
icon="contentType.icon"
131132
name="contentType.name"
@@ -188,7 +189,7 @@ <h5>{{ vm.manageIndex.selectedContentType.name }} Properties</h5>
188189
<div class="umb-panel-group__details-check-name">Search over index</div>
189190
<div class="umb-panel-group__details-check-description">
190191
Please enter the query you want to search by against index <b>{{ vm.selectedSearchIndex.name }}</b>
191-
</div>
192+
</div>
192193
</div>
193194
<div class="umb-panel-group__details-status">
194195
<div class="umb-panel-group__details-status-content">

src/Umbraco.Cms.Integrations.Search.Algolia/Controllers/SearchController.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public async Task<IActionResult> BuildIndex([FromBody] IndexConfiguration indexC
9292
foreach (var contentItem in contentItems)
9393
{
9494
var record = new RecordBuilder()
95-
.BuildFromContent(contentItem, (p) => contentDataItem.Properties.Any(q => q == p.Alias))
95+
.BuildFromContent(contentItem, (p) => contentDataItem.Properties.Any(q => q.Alias == p.Alias))
9696
.Build();
9797

9898
payload.Add(record);
@@ -110,12 +110,16 @@ public async Task<IActionResult> BuildIndex([FromBody] IndexConfiguration indexC
110110
}
111111

112112
[HttpDelete]
113-
public IActionResult DeleteIndex(int id)
113+
public async Task<IActionResult> DeleteIndex(int id)
114114
{
115115
try
116116
{
117+
var indexName = _indexStorage.GetById(id).Name;
118+
117119
_indexStorage.Delete(id);
118120

121+
await _indexService.DeleteIndex(indexName);
122+
119123
return new JsonResult(Result.Ok());
120124
}
121125
catch(Exception ex)

src/Umbraco.Cms.Integrations.Search.Algolia/Handlers/BaseContentHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ protected async Task RebuildIndex(IEnumerable<IContent> entities, bool deleteInd
3939
foreach (var index in indices)
4040
{
4141
var indexConfiguration = JsonSerializer.Deserialize<List<ContentData>>(index.SerializedData)
42-
.FirstOrDefault(p => p.ContentType == entity.ContentType.Alias);
43-
if (indexConfiguration == null || indexConfiguration.ContentType != entity.ContentType.Alias) continue;
42+
.FirstOrDefault(p => p.ContentType.Alias == entity.ContentType.Alias);
43+
if (indexConfiguration == null || indexConfiguration.ContentType.Alias != entity.ContentType.Alias) continue;
4444

4545
var record = new RecordBuilder()
46-
.BuildFromContent(entity, (p) => indexConfiguration.Properties.Any(q => q == p.Alias))
46+
.BuildFromContent(entity, (p) => indexConfiguration.Properties.Any(q => q.Alias == p.Alias))
4747
.Build();
4848

4949
var result = deleteIndexData
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-

1+
using System.Text.Json.Serialization;
2+
23
namespace Umbraco.Cms.Integrations.Search.Algolia.Models
34
{
45
public class ContentData
56
{
6-
public string ContentType { get; set; }
7+
[JsonPropertyName("contentType")]
8+
public ContentEntity ContentType { get; set; }
79

8-
public string ContentTypeIcon { get; set; }
10+
[JsonPropertyName("properties")]
11+
public IEnumerable<ContentEntity> Properties { get; set; }
912

10-
public string[] Properties { get; set; }
13+
[JsonPropertyName("propertiesDescription")]
14+
public IEnumerable<string> PropertiesDescription => Properties.Select(p => p.Name);
1115
}
1216
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+

2+
using System.Text.Json.Serialization;
3+
4+
namespace Umbraco.Cms.Integrations.Search.Algolia.Models
5+
{
6+
public class ContentEntity
7+
{
8+
[JsonPropertyName("alias")]
9+
public string Alias { get; set; }
10+
11+
[JsonPropertyName("name")]
12+
public string Name { get; set; }
13+
14+
[JsonPropertyName("icon")]
15+
public string Icon { get; set; }
16+
}
17+
}

src/Umbraco.Cms.Integrations.Search.Algolia/Services/AlgoliaIndexService.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,23 @@ public async Task<Result> DeleteData(string name, string objectId)
8383
return Result.Fail(ex.Message);
8484
}
8585
}
86+
87+
public async Task<Result> DeleteIndex(string name)
88+
{
89+
try
90+
{
91+
var client = new SearchClient(_settings.ApplicationId, _settings.AdminApiKey);
92+
93+
var index = client.InitIndex(name);
94+
95+
await index.DeleteAsync();
96+
97+
return Result.Ok();
98+
}
99+
catch (AlgoliaException ex)
100+
{
101+
return Result.Fail(ex.Message);
102+
}
103+
}
86104
}
87105
}

src/Umbraco.Cms.Integrations.Search.Algolia/Services/AlgoliaSearchService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Algolia.Search.Clients;
2-
using Algolia.Search.Http;
32
using Algolia.Search.Models.Search;
43

54
using Microsoft.Extensions.Options;

src/Umbraco.Cms.Integrations.Search.Algolia/Services/IAlgoliaIndexService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ public interface IAlgoliaIndexService
99
Task<Result> UpdateData(string name, Record record);
1010

1111
Task<Result> DeleteData(string name, string objectId);
12+
13+
Task<Result> DeleteIndex(string name);
1214
}
1315
}

0 commit comments

Comments
 (0)