@@ -5,7 +5,7 @@ import { html, css, state, customElement } from "@umbraco-cms/backoffice/externa
5
5
import type { ProductDtoModel } from "../../generated" ;
6
6
import type { ShopifyProductPickerModalData , ShopifyProductPickerModalValue } from "./shopify.modal-token.js" ;
7
7
import type { ShopifyServiceStatus } from "../models/shopify-service.model.js" ;
8
- import type { UmbTableColumn , UmbTableConfig , UmbTableItem } from '@umbraco-cms/backoffice/components' ;
8
+ import type { UmbTableColumn , UmbTableConfig , UmbTableItem , UmbTableSelectedEvent , UmbTableElement , UmbTableDeselectedEvent } from '@umbraco-cms/backoffice/components' ;
9
9
import type { ShopifyCollectionModel } from "../types/types.js" ;
10
10
import type { UmbDefaultCollectionContext } from '@umbraco-cms/backoffice/collection' ;
11
11
import { UMB_COLLECTION_CONTEXT } from '@umbraco-cms/backoffice/collection' ;
@@ -17,9 +17,12 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
17
17
#shopifyContext! : typeof SHOPIFY_CONTEXT_TOKEN . TYPE ;
18
18
#collectionContext! : UmbDefaultCollectionContext < ShopifyCollectionModel > ;
19
19
20
+ @state ( )
21
+ private _selection : Array < string > = [ ] ;
22
+
20
23
@state ( )
21
24
private _tableConfig : UmbTableConfig = {
22
- allowSelection : false ,
25
+ allowSelection : true ,
23
26
} ;
24
27
25
28
@state ( )
@@ -84,7 +87,6 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
84
87
85
88
this . consumeContext ( UMB_COLLECTION_CONTEXT , ( instance ) => {
86
89
this . #collectionContext = instance ;
87
- this . #observeCollectionItems( ) ;
88
90
} ) ;
89
91
}
90
92
@@ -120,43 +122,66 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
120
122
if ( ! data . isValid || data . isExpired ) {
121
123
this . _showError ( "Data is invalid or expired." ! ) ;
122
124
}
123
- }
124
125
125
- private _onSelect ( products : Array < ProductDtoModel > ) {
126
- this . value = { products } ;
127
- this . _submitModal ( ) ;
126
+ this . #createTableItems( this . _products ) ;
128
127
}
129
128
129
+ #onSelected( event : UmbTableSelectedEvent ) {
130
+ event . stopPropagation ( ) ;
131
+ const table = event . target as UmbTableElement ;
132
+ const selection = table . selection ;
133
+ this . #collectionContext?. selection . setSelection ( selection ) ;
134
+ }
135
+
136
+ #onDeselected( event : UmbTableDeselectedEvent ) {
137
+ event . stopPropagation ( ) ;
138
+ const table = event . target as UmbTableElement ;
139
+ const selection = table . selection ;
140
+ this . #collectionContext?. selection . setSelection ( selection ) ;
141
+ }
142
+
130
143
private async _showError ( message : string ) {
131
144
const notificationContext = await this . getContext ( UMB_NOTIFICATION_CONTEXT ) ;
132
145
notificationContext ?. peek ( "danger" , {
133
146
data : { message } ,
134
147
} ) ;
135
148
}
136
149
137
- #observeCollectionItems( ) {
138
- if ( ! this . #shopifyContext) return ;
139
- this . observe ( this . #collectionContext. items , ( items ) => this . #createTableItems( items , this . _products ) , 'umbCollectionItemsObserver' ) ;
140
- }
141
-
142
- #createTableItems( products : Array < ShopifyCollectionModel > , actual : Array < ProductDtoModel > ) {
150
+ #createTableItems( products : Array < ProductDtoModel > ) {
143
151
this . _tableItems = products . map ( ( product ) => {
144
152
return {
145
- id : product . unique ,
146
- icon : '' ,
147
- data : [
148
- {
149
- columnAlias : 'productName' ,
150
- value : actual [ 0 ] . title
151
- } ,
152
- ...actual . map ( a => {
153
- return {
154
- columnAlias : 'Vendor' ,
155
- value : a . vendor
156
- } ;
157
- } ) ,
158
- ] ,
159
- } ;
153
+ id : product . id . toString ( ) ,
154
+ icon : 'icon-book-alt-2' ,
155
+ data : [ {
156
+ columnAlias : "productName" ,
157
+ value : product . title ,
158
+ } ,
159
+ {
160
+ columnAlias : "vendor" ,
161
+ value : product . vendor ,
162
+ } ,
163
+ {
164
+ columnAlias : "status" ,
165
+ value : product . status ,
166
+ } ,
167
+ {
168
+ columnAlias : "tags" ,
169
+ value : product . tags ,
170
+ } ,
171
+ {
172
+ columnAlias : "sku" ,
173
+ value : product . variants . map ( v => v . sku ) . join ( "," ) ,
174
+ } ,
175
+ {
176
+ columnAlias : "barcode" ,
177
+ value : product . variants . map ( v => v . barcode ) . join ( "," ) ,
178
+ } ,
179
+ {
180
+ columnAlias : "price" ,
181
+ value : product . variants [ 0 ] . price ,
182
+ } ,
183
+ ]
184
+ }
160
185
} ) ;
161
186
}
162
187
@@ -165,13 +190,19 @@ export default class ShopifyProductsModalElement extends UmbModalBaseElement<Sho
165
190
< umb-body-layout >
166
191
< uui-box headline ="Shopify Products ">
167
192
${ this . _loading ? html `< div class ="center "> < uui-loader > </ uui-loader > </ div > ` : "" }
168
- < umb-table .config =${ this . _tableConfig } .columns =${ this . _tableColumns } .items=${ this . _tableItems } > </ umb-table >
193
+ < umb-table
194
+ .config =${ this . _tableConfig }
195
+ .columns =${ this . _tableColumns }
196
+ .items=${ this . _tableItems }
197
+ .selection=${ this . _selection }
198
+ @selected="${ this . #onSelected} "
199
+ @deselected="${ this . #onDeselected} "> </ umb-table >
169
200
170
201
</ uui-box >
171
202
< span > Add up to x items(s)</ span >
172
203
173
- < uui-button slot ="actions " label ="Submit "> </ uui-button >
174
- < uui-button slot ="actions " label ="Close "> </ uui-button >
204
+ < uui-button slot ="actions " label ="Submit " @click = ${ this . _submitModal } > </ uui-button >
205
+ < uui-button slot ="actions " label ="Close " @click = ${ this . _rejectModal } > </ uui-button >
175
206
</ umb-body-layout >
176
207
` ;
177
208
}
0 commit comments