1
+ import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api' ;
2
+ import { LitElement , customElement , html , css , property , state } from "@umbraco-cms/backoffice/external/lit" ;
3
+ import { UMB_MODAL_MANAGER_CONTEXT } from "@umbraco-cms/backoffice/modal" ;
4
+ import {
5
+ UMB_NOTIFICATION_CONTEXT ,
6
+ } from "@umbraco-cms/backoffice/notification" ;
7
+ import { SHOPIFY_MODAL_TOKEN } from "../modal/shopify.modal-token" ;
8
+ import { ConfigDescription , type ShopifyServiceStatus } from "../models/shopify-service.model" ;
9
+ import { SHOPIFY_CONTEXT_TOKEN } from "../context/shopify.context" ;
10
+ import type { ProductDtoModel } from "../generated/models" ;
11
+
12
+ const elementName = "shopify-picker" ;
13
+ export class ShopifyPickerPropertyEditor extends UmbElementMixin ( LitElement ) {
14
+ #modalManagerContext?: typeof UMB_MODAL_MANAGER_CONTEXT . TYPE ;
15
+ #shopifyContext! : typeof SHOPIFY_CONTEXT_TOKEN . TYPE ;
16
+
17
+ @property ( { type : String } )
18
+ public value = "" ;
19
+
20
+ @state ( )
21
+ private products : Array < ProductDtoModel > = [ ] ;
22
+
23
+ @state ( )
24
+ private _serviceStatus : ShopifyServiceStatus = {
25
+ isValid : false ,
26
+ type : "" ,
27
+ description : "" ,
28
+ useOAuth : false
29
+ } ;
30
+
31
+ constructor ( ) {
32
+ super ( ) ;
33
+ this . consumeContext ( UMB_MODAL_MANAGER_CONTEXT , ( instance ) => {
34
+ this . #modalManagerContext = instance ;
35
+ } ) ;
36
+ this . consumeContext ( SHOPIFY_CONTEXT_TOKEN , ( context ) => {
37
+ this . #shopifyContext = context ;
38
+ } ) ;
39
+ }
40
+
41
+ async connectedCallback ( ) {
42
+ super . connectedCallback ( ) ;
43
+
44
+ if ( this . value == null || this . value . length == 0 ) return ;
45
+
46
+ const { data } = await this . #shopifyContext. checkConfiguration ( ) ;
47
+ if ( ! data || ! data . type ?. value ) return ;
48
+
49
+ this . _serviceStatus = {
50
+ isValid : data . isValid ,
51
+ type : data . type . value ,
52
+ description : "" ,
53
+ useOAuth : data . isValid && data . type . value === "OAuth"
54
+ }
55
+
56
+ if ( ! this . _serviceStatus . isValid ) {
57
+ this . _showError ( ConfigDescription . none ) ;
58
+ }
59
+
60
+ const dto : Array < ProductDtoModel > = JSON . parse ( JSON . stringify ( this . value ) ) ;
61
+ this . products = dto ;
62
+ }
63
+
64
+ private async _openModal ( ) {
65
+ const pickerContext = this . #modalManagerContext?. open ( this , SHOPIFY_MODAL_TOKEN , {
66
+ data : {
67
+ headline : "HubSpot Forms" ,
68
+ } ,
69
+ } ) ;
70
+
71
+ const data = await pickerContext ?. onSubmit ( ) ;
72
+ if ( ! data ) return ;
73
+
74
+ this . value = JSON . stringify ( data . products ) ;
75
+ console . log ( this . value ) ;
76
+ this . dispatchEvent ( new CustomEvent ( 'property-value-change' ) ) ;
77
+ }
78
+
79
+ private async _showError ( message : string ) {
80
+ const notificationContext = await this . getContext ( UMB_NOTIFICATION_CONTEXT ) ;
81
+ notificationContext ?. peek ( "danger" , {
82
+ data : { message : message } ,
83
+ } ) ;
84
+ }
85
+
86
+ render ( ) {
87
+ return html `
88
+ ${ this . value == null || this . value . length == 0
89
+ ? html `
90
+ <uui- butto n
91
+ class= "add-button"
92
+ @click = ${ this . _openModal }
93
+ label= ${ this . localize . term ( 'general_add' ) }
94
+ look= "placeholder" > </ uui- butto n>
95
+ `
96
+ : html `
97
+ <div> </ div>
98
+ ` }
99
+ ` ;
100
+ }
101
+
102
+ static styles = [
103
+ css `
104
+ .add-button {
105
+ width : 100% ;
106
+ }
107
+ ` ] ;
108
+ }
109
+
110
+
111
+ export default ShopifyPickerPropertyEditor ;
112
+
113
+ declare global {
114
+ interface HTMLElementTagNameMap {
115
+ [ elementName ] : ShopifyPickerPropertyEditor ;
116
+ }
117
+ }
0 commit comments