@@ -2,16 +2,28 @@ import { customElement, html, repeat, state } from "@umbraco-cms/backoffice/exte
2
2
import { UmbModalBaseElement } from "@umbraco-cms/backoffice/modal" ;
3
3
import { DynamicsFormPickerModalData , DynamicsFormPickerModalValue } from "./dynamics.modal-token" ;
4
4
import { DYNAMICS_CONTEXT_TOKEN } from "../context/dynamics.context" ;
5
- import { FormDtoModel } from "@umbraco-integrations/dynamics/generated" ;
5
+ import { FormDtoModel , OAuthConfigurationDtoModel , OAuthRequestDtoModel } from "@umbraco-integrations/dynamics/generated" ;
6
+ import { UMB_NOTIFICATION_CONTEXT , UmbNotificationColor } from "@umbraco-cms/backoffice/notification" ;
7
+ import { DynamicsOAuthSetup } from "../models/dynamics-service.model" ;
6
8
7
9
const elementName = "dynamics-forms-modal" ;
8
10
9
11
@customElement ( elementName )
10
12
export default class DynamicsFormModalElement extends UmbModalBaseElement < DynamicsFormPickerModalData , DynamicsFormPickerModalValue > {
11
13
#dynamicsContext! : typeof DYNAMICS_CONTEXT_TOKEN . TYPE ;
12
14
15
+ @state ( )
16
+ private _loading = false ;
13
17
@state ( )
14
18
private _forms : Array < FormDtoModel > = [ ] ;
19
+ @state ( )
20
+ private _oAuthConfig : OAuthConfigurationDtoModel | undefined ;
21
+ @state ( )
22
+ private _oauthSetup : DynamicsOAuthSetup = {
23
+ isConnected : false ,
24
+ isAccessTokenExpired : false ,
25
+ isAccessTokenValid : false
26
+ } ;
15
27
16
28
constructor ( ) {
17
29
super ( ) ;
@@ -24,25 +36,96 @@ export default class DynamicsFormModalElement extends UmbModalBaseElement<Dynami
24
36
25
37
async connectedCallback ( ) {
26
38
super . connectedCallback ( ) ;
27
- await this . getForms ( ) ;
39
+ await this . #checkOAuthConfiguration( ) ;
40
+ }
41
+
42
+ async #checkOAuthConfiguration( ) {
43
+ const { data} = await this . #dynamicsContext. checkOauthConfiguration ( ) ;
44
+ if ( ! data ) return ;
45
+ if ( ! data . isAuthorized ) this . _showError ( "Unable to connect to Dynamics. Please review the settings of the form picker property's data type." ) ;
46
+
47
+ this . _oAuthConfig = data ;
48
+
49
+ await this . #getForms( ) ;
28
50
}
29
51
30
- async getForms ( ) {
52
+ async #getForms( ) {
53
+ this . _loading = true ;
31
54
const { data } = await this . #dynamicsContext. getForms ( "Both" ) ;
32
55
if ( ! data ) return ;
33
56
34
57
this . _forms = data ;
58
+ this . _loading = false ;
35
59
}
36
60
37
61
_onSelect ( form : FormDtoModel ) {
38
62
this . value = { selectedForm : form } ;
39
63
this . _submitModal ( ) ;
40
64
}
41
65
66
+ async #connectButtonClick( ) {
67
+ window . addEventListener ( "message" , async ( event : MessageEvent ) => {
68
+ if ( event . data . type === "dynamics:oauth:success" ) {
69
+
70
+ const oauthRequestDtoModel : OAuthRequestDtoModel = {
71
+ code : event . data . code
72
+ } ;
73
+
74
+ const { data } = await this . #dynamicsContext. getAccessToken ( oauthRequestDtoModel ) ;
75
+ if ( ! data ) return ;
76
+
77
+ if ( data . startsWith ( "Error:" ) ) {
78
+ this . _showError ( data ) ;
79
+ } else {
80
+ this . _oauthSetup = {
81
+ isConnected : true
82
+ } ;
83
+ this . _showSuccess ( "OAuth Connected" ) ;
84
+
85
+ }
86
+
87
+ }
88
+ } , false ) ;
89
+
90
+ const { data } = await this . #dynamicsContext. getAuthorizationUrl ( ) ;
91
+ if ( ! data ) return ;
92
+
93
+ window . open ( data , "Authorize" , "width=900,height=700,modal=yes,alwaysRaised=yes" ) ;
94
+ }
95
+
96
+ async #revokeButtonClick( ) {
97
+ await this . #dynamicsContext. revokeAccessToken ( ) ;
98
+
99
+ this . _oauthSetup = {
100
+ isConnected : false
101
+ } ;
102
+ this . _showSuccess ( "OAuth connection revoked." ) ;
103
+
104
+ this . dispatchEvent ( new CustomEvent ( "revoke" ) ) ;
105
+ }
106
+
107
+ private async _showSuccess ( message : string ) {
108
+ await this . _showMessage ( message , "positive" ) ;
109
+ }
110
+
111
+ private async _showError ( message : string ) {
112
+ await this . _showMessage ( message , "danger" ) ;
113
+ }
114
+
115
+ private async _showMessage ( message : string , color : UmbNotificationColor ) {
116
+ const notificationContext = await this . getContext ( UMB_NOTIFICATION_CONTEXT ) ;
117
+ notificationContext ?. peek ( color , {
118
+ data : { message } ,
119
+ } ) ;
120
+ }
121
+
42
122
render ( ) {
43
123
return html `
44
124
< umb-body-layout >
45
- < uui-box headline ="Dynamics Marketing Forms ">
125
+
126
+ ${ this . _loading ? html `< div class ="center loader "> < uui-loader > </ uui-loader > </ div > ` : "" }
127
+
128
+ < uui-box headline =${ this . data ! . headline } >
46
129
< div slot ="header "> Select a form</ div >
47
130
48
131
${ repeat ( this . _forms , ( form ) => html `
@@ -57,16 +140,20 @@ export default class DynamicsFormModalElement extends UmbModalBaseElement<Dynami
57
140
< uui-box headline ="Dynamics - OAuth Status ">
58
141
< div slot ="header "> Please connect with your Microsoft account.</ div >
59
142
< div >
60
- < p > < b > Connected</ b > : Thanhnd </ p >
143
+ < p > < b > Connected</ b > : ${ this . _oAuthConfig ?. fullName } </ p >
61
144
</ div >
62
145
< div >
63
146
< uui-button
64
147
look ="primary "
65
- label ="Connect "> </ uui-button >
148
+ label ="Connect "
149
+ ?disabled =${ this . _oauthSetup ?. isConnected }
150
+ @click =${ this . #connectButtonClick} > </ uui-button >
66
151
< uui-button
67
152
color ="danger "
68
153
look ="secondary "
69
- label ="Revoke "> </ uui-button >
154
+ label ="Revoke "
155
+ ?disabled =${ ! this . _oauthSetup ?. isConnected }
156
+ @click =${ this . #revokeButtonClick} > </ uui-button >
70
157
</ div >
71
158
</ uui-box >
72
159
0 commit comments