@@ -11,9 +11,8 @@ import { UmbElementMixin } from "@umbraco-cms/backoffice/element-api";
11
11
import {
12
12
UMB_NOTIFICATION_CONTEXT ,
13
13
} from "@umbraco-cms/backoffice/notification" ;
14
-
15
- import { type AlgoliaIndexContext , ALGOLIA_CONTEXT_TOKEN } from '@umbraco-integrations/algolia/context' ;
16
-
14
+ import type { UUIInputEvent } from "@umbraco-cms/backoffice/external/uui" ;
15
+ import { ALGOLIA_CONTEXT_TOKEN } from '@umbraco-integrations/algolia/context' ;
17
16
import type {
18
17
IndexConfigurationModel ,
19
18
ContentTypeDtoModel
@@ -23,7 +22,7 @@ const elementName = "algolia-index";
23
22
24
23
@customElement ( elementName )
25
24
export class AlgoliaIndexElement extends UmbElementMixin ( LitElement ) {
26
- #algoliaIndexContext?: AlgoliaIndexContext ;
25
+ #algoliaIndexContext! : typeof ALGOLIA_CONTEXT_TOKEN . TYPE ;
27
26
28
27
@property ( )
29
28
indexId ! : string ;
@@ -39,23 +38,21 @@ export class AlgoliaIndexElement extends UmbElementMixin(LitElement) {
39
38
private _contentTypes : Array < ContentTypeDtoModel > = [ ] ;
40
39
41
40
@state ( )
42
- private _showContentTypeProperties : boolean ;
41
+ private _showContentTypeProperties = false ;
43
42
44
43
constructor ( ) {
45
44
super ( ) ;
46
45
47
- this . consumeContext ( ALGOLIA_CONTEXT_TOKEN , ( _instance ) => {
48
- this . #algoliaIndexContext = _instance ;
46
+ this . consumeContext ( ALGOLIA_CONTEXT_TOKEN , ( context ) => {
47
+ this . #algoliaIndexContext = context ;
49
48
} ) ;
50
-
51
- this . _showContentTypeProperties = false ;
52
49
}
53
50
54
- connectedCallback ( ) {
51
+ async connectedCallback ( ) {
55
52
super . connectedCallback ( ) ;
56
53
57
54
if ( this . indexId . length > 0 ) {
58
- this . #getContentTypesWithIndex( ) ;
55
+ await this . #getContentTypesWithIndex( ) ;
59
56
this . #getIndex( ) ;
60
57
}
61
58
else {
@@ -64,38 +61,54 @@ export class AlgoliaIndexElement extends UmbElementMixin(LitElement) {
64
61
}
65
62
66
63
async #getContentTypes( ) {
67
- var response = await this . #algoliaIndexContext?. getContentTypes ( ) ;
68
- this . _contentTypes = response ?. data as Array < ContentTypeDtoModel > ;
64
+ const { data } = await this . #algoliaIndexContext. getContentTypes ( ) ;
65
+ if ( ! data ) return ;
66
+
67
+ this . _contentTypes = data ;
69
68
}
70
69
71
70
async #getContentTypesWithIndex( ) {
72
- var response = await this . #algoliaIndexContext?. getContentTypesWithIndex ( Number ( this . indexId ) ) ;
73
- this . _contentTypes = response ?. data as Array < ContentTypeDtoModel > ;
71
+ const { data } = await this . #algoliaIndexContext. getContentTypesWithIndex ( Number ( this . indexId ) ) ;
72
+ if ( ! data ) return ;
73
+
74
+ this . _contentTypes = data ;
74
75
}
75
76
76
77
async #getIndex( ) {
77
- var response = await this . #algoliaIndexContext?. getIndexById ( Number ( this . indexId ) ) ;
78
- this . _model = response ?. data as IndexConfigurationModel ;
78
+ const { data } = await this . #algoliaIndexContext. getIndexById ( Number ( this . indexId ) ) ;
79
+ if ( ! data ) return ;
80
+
81
+ this . _model = data ;
82
+
83
+ if ( this . _model . contentData . length ) {
84
+ this . _showContentTypeProperties = true ;
85
+ }
79
86
}
80
87
88
+ #handleNameChange( e : UUIInputEvent ) {
89
+ this . _model . name = e . target . value . toString ( ) ;
90
+ }
91
+
81
92
async #contentTypeSelected( id : number ) {
82
93
this . _contentTypes = this . _contentTypes . map ( ( obj ) => {
83
- if ( obj . id == id ) {
94
+ if ( obj . id === id ) {
84
95
obj . selected = true ;
85
96
}
86
97
return obj ;
87
98
} ) ;
99
+
88
100
this . _showContentTypeProperties = true ;
89
101
}
90
102
91
103
async #contentTypeDeselected( id : number ) {
92
104
this . _contentTypes = this . _contentTypes . map ( ( obj ) => {
93
- if ( obj . id == id ) {
105
+ if ( obj . id === id ) {
94
106
obj . selected = false ;
95
107
}
96
108
return obj ;
97
109
} ) ;
98
- this . _showContentTypeProperties = false ;
110
+
111
+ this . _showContentTypeProperties = this . _contentTypes . filter ( x => x . selected ) . length !== 0 ;
99
112
}
100
113
101
114
async #contentTypePropertySelected( contentType : ContentTypeDtoModel | undefined , id : number ) {
@@ -134,27 +147,21 @@ export class AlgoliaIndexElement extends UmbElementMixin(LitElement) {
134
147
async #handleSubmit( e : SubmitEvent ) {
135
148
e . preventDefault ( ) ;
136
149
137
- const form = e . target as HTMLFormElement ;
138
- const formData = new FormData ( form ) ;
139
-
140
- const indexName = this . indexId . length > 0
141
- ? this . _model . name
142
- : formData . get ( "indexName" ) as string ;
143
-
144
- if ( indexName . length == 0 || this . _contentTypes === undefined || this . _contentTypes . filter ( obj => obj . selected ) . length == 0 ) {
150
+ if ( this . _model . name . length == 0 || this . _contentTypes === undefined || this . _contentTypes ?. filter ( obj => obj . selected ) . length == 0 ) {
145
151
this . #showError( "Index name and content schema are required." ) ;
146
152
return ;
147
153
}
148
154
149
155
const indexConfiguration : IndexConfigurationModel = {
150
156
id : 0 ,
151
- name : indexName ,
157
+ name : this . _model . name ,
152
158
contentData : [ ]
153
159
} ;
154
160
155
161
if ( this . indexId . length > 0 ) {
156
162
indexConfiguration . id = Number ( this . indexId ) ;
157
163
}
164
+
158
165
indexConfiguration . contentData = this . _contentTypes ;
159
166
160
167
await this . #algoliaIndexContext?. saveIndex ( indexConfiguration ) ;
@@ -174,12 +181,13 @@ export class AlgoliaIndexElement extends UmbElementMixin(LitElement) {
174
181
return html `
175
182
${ this . _contentTypes . map ( ( contentType ) => {
176
183
return html `
177
- <uui- ref- node id= "dc_${ contentType . alias } _${ contentType . id } "
178
- selectable
179
- name = ${ contentType . name }
180
- @selected = ${ ( ) => this . #contentTypeSelected( contentType . id ) }
181
- @deselected = ${ ( ) => this . #contentTypeDeselected( contentType . id ) } >
182
- <uui- icon slot= "icon" name = ${ contentType . icon } > </ uui- icon>
184
+ <uui- ref- node
185
+ selectable
186
+ ?selected= ${ contentType . selected }
187
+ name= ${ contentType . name }
188
+ @selected = ${ ( ) => this . #contentTypeSelected( contentType . id ) }
189
+ @deselected = ${ ( ) => this . #contentTypeDeselected( contentType . id ) } >
190
+ <umb- icon slot= "icon" name = ${ contentType . icon } > </ umb- icon>
183
191
${ contentType . selected ? html `<uui- tag size= "s" slot = "tag" color = "positive" > Selected </ uui- tag> ` : '' }
184
192
<uui- action- bar slot= "actions" >
185
193
<uui- butto n label= "Remove" color = "danger" >
@@ -195,29 +203,33 @@ export class AlgoliaIndexElement extends UmbElementMixin(LitElement) {
195
203
private renderContentTypeProperties ( ) {
196
204
if ( this . _showContentTypeProperties === false ) return nothing ;
197
205
198
- const selectedContentType = this . _contentTypes . find ( ( obj ) => obj . selected == true ) ;
206
+ const selectedContentTypes = this . _contentTypes . filter ( ( obj ) => obj . selected == true ) ;
199
207
200
- if ( selectedContentType === undefined ) return nothing ;
208
+ if ( ! selectedContentTypes ?. length ) return nothing ;
201
209
202
210
return html `
203
- <uui- for m- layout- item>
204
- <uui- label slot= "label" > ${ selectedContentType . name } Properties </ uui- label>
205
- <div class= "alg-col-3" >
206
- ${ selectedContentType . properties . map ( ( property ) => {
207
- return html `
208
- <uui- card- content- node selectable
209
- @selected = ${ ( ) => this . #contentTypePropertySelected( selectedContentType , property . id ) }
210
- @deselected = ${ ( ) => this . #contentTypePropertyDeselected( selectedContentType , property . id ) }
211
- name= ${ property . name } >
212
- ${ property . selected ? html `<uui- tag size= "s" slot = "tag" color = "positive"> Selected </ uui- tag> ` : '' }
213
- <ul style= "list-style: none; padding-inline-start: 0px; margin: 0;" >
214
- <li> <span style= "font-weight: 700" > Group : </ span> ${ property . group } </ li>
215
- </ ul>
216
- </ uui- card- content- node>
217
- ` ;
218
- } ) }
219
- </ div>
220
- </ uui- for m- layout- item>
211
+ ${ selectedContentTypes . map ( selectedContentType => html `
212
+ <uui- for m- layout- item>
213
+ <uui- label slot= "label" > ${ selectedContentType . name } Properties </ uui- label>
214
+ <div id= "grid" >
215
+ ${ selectedContentType . properties . map ( ( property ) => {
216
+ return html `
217
+ <uui- card- content- node
218
+ selectable
219
+ ?selected= ${ property . selected }
220
+ @selected = ${ ( ) => this . #contentTypePropertySelected( selectedContentType , property . id ) }
221
+ @deselected = ${ ( ) => this . #contentTypePropertyDeselected( selectedContentType , property . id ) }
222
+ name= ${ property . name } >
223
+ ${ property . selected ? html `<uui- tag size= "s" slot = "tag" color = "positive"> Selected </ uui- tag> ` : '' }
224
+ <ul style= "list-style: none; padding-inline-start: 0px; margin: 0;" >
225
+ <li> <span style= "font-weight: 700" > Group : </ span> ${ property . group } </ li>
226
+ </ ul>
227
+ </ uui- card- content- node>
228
+ ` ;
229
+ } ) }
230
+ </ div>
231
+ </ uui- for m- layout- item>
232
+ ` ) }
221
233
` ;
222
234
}
223
235
@@ -226,22 +238,25 @@ export class AlgoliaIndexElement extends UmbElementMixin(LitElement) {
226
238
<uui- box headline= ${ this . indexId . length > 0 ? "Create Index Definition" : "Edit Index Definition" } >
227
239
<uui- for m>
228
240
<for m id= "manageIndexFrm" name = "manageIndexFrm" @submit = ${ this . #handleSubmit} >
229
- <uui- for m- layout- item>
230
- <uui- label slot= "label" for = "inName" required= ""> Name </ uui- label>
231
- <span class= "alg-description" slot = "description"> Please enter a name for the index . After save , <br / > you will not be able to change it .</ span>
232
- <div>
233
- <uui- input type= "text" name = "indexName" label= "indexName" ?dis abled= ${ this . indexId . length > 0 } .value = ${ this . _model . name } style= "width: 17%"> </ uui- input>
234
- </ div>
235
- </ uui- for m- layout- item>
236
-
237
- <div class= "alg-col-2" >
238
- <uui- for m- layout- item>
239
- <uui- label slot= "label" > Document Types </ uui- label>
240
- <span class= "alg-description" slot = "description"> Please select the document types you would like to index , and choose the fields to include .</ span>
241
+ <umb- property- layout
242
+ label= "Name"
243
+ description = "Please enter a name for the index. After save , you will not be able to change it .">
244
+ <uui-input
245
+ slot=" editor"
246
+ ?disabled=${ this . indexId . length > 0 }
247
+ .value=${ this . _model . name }
248
+ @change=${ this . #handleNameChange} ></uui-input>
249
+ </umb-property-layout>
250
+
251
+ <umb-property-layout
252
+ label=" Document Types "
253
+ description=" Please select the document types you would like to index , and choose the fields to include .">
254
+ <div slot=" editor">
241
255
${ this . renderContentTypes ( ) }
242
- </ uui- for m- layout- item>
243
- ${ this . renderContentTypeProperties ( ) }
244
- </ div>
256
+ ${ this . renderContentTypeProperties ( ) }
257
+ </div>
258
+ </umb-property-layout>
259
+
245
260
<uui-button type=" submit" label=${ this . localize . term ( "buttons_save" ) } look=" primary" color=" positive"></uui-button>
246
261
</form>
247
262
</uui-form>
@@ -251,16 +266,7 @@ export class AlgoliaIndexElement extends UmbElementMixin(LitElement) {
251
266
252
267
static styles = [
253
268
css `
254
- .center {
255
- display : grid;
256
- place-items : center;
257
- }
258
- .alg-col-2 {
259
- display : grid;
260
- grid-template-columns : 25% 60% ;
261
- gap : 20px ;
262
- }
263
- .alg-col-3 {
269
+ #grid {
264
270
display: grid;
265
271
grid-template-columns: 33% 33% 33%;
266
272
gap: 10px;
0 commit comments