blob: e3deee7fb9829fff26f2deec21432849a1ddcd4e [file] [log] [blame]
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02001import '@material/web/formfield/formfield.js';
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +01002import '@material/web/switch/switch.js';
3import '@material/web/textfield/outlined-text-field.js';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02004
Adrià Vilanova Martínez41493f22022-11-06 22:38:21 +01005import {css, html, LitElement, nothing} from 'lit';
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02006import {createRef, ref} from 'lit/directives/ref.js';
7
8import {CCApi} from '../../../../common/api.js';
9import * as pb from '../../../proto/main_pb.js';
10
11export default class WFActionReplyWithCR extends LitElement {
12 static properties = {
13 action: {type: Object},
14 readOnly: {type: Boolean},
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010015 _importerWindow: {type: Object, state: true},
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020016 };
17
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010018 static styles = css`
19 .form-line {
20 display: flex;
21 flex-direction: row;
22 align-items: center;
23 margin-block: 1em;
24 gap: .5rem;
25 }
26 `;
27
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020028 cannedResponseRef = createRef();
29 subscribeRef = createRef();
30 markAsAnswerRef = createRef();
31
32 constructor() {
33 super();
34 this.action = new pb.workflows.Action.ReplyWithCRAction;
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010035 this._importerWindow = undefined;
36
37 window.addEventListener('message', e => {
38 if (e.source === this._importerWindow &&
39 e.data?.action === 'importCannedResponse') {
40 this._cannedResponseIdString = e.data?.cannedResponseId;
41 this._importerWindow?.close?.();
42 }
43 });
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020044 }
45
46 render() {
47 return html`
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010048 <div class="form-line">
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020049 <md-outlined-text-field ${ref(this.cannedResponseRef)}
50 type="number"
51 label="Canned response ID"
52 required
53 value=${this._cannedResponseIdString}
54 ?readonly=${this.readOnly}
55 @input=${this._cannedResponseIdChanged}>
56 </md-outlined-text-field>
Adrià Vilanova Martínez41493f22022-11-06 22:38:21 +010057 ${this.readOnly ? nothing : html`
58 <md-outlined-button
59 icon="more"
60 label="Select CR"
61 @click=${this._openCRImporter}>
62 </md-outlined-button>
63 `}
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010064 </div>
65 <div class="form-line">
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020066 <md-formfield label="Subscribe to thread">
67 <md-switch ${ref(this.subscribeRef)}
68 ?selected=${this.subscribe}
69 ?disabled=${this.readOnly}
70 @click=${this._subscribeChanged}/>
71 </md-formfield>
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010072 </div>
73 <div class="form-line">
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020074 <md-formfield label="Mark as answer">
75 <md-switch ${ref(this.markAsAnswerRef)}
76 ?selected=${this.markAsAnswer}
77 ?disabled=${this.readOnly}
78 @click=${this._markAsAnswerChanged}/>
79 </md-formfield>
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010080 </div>
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020081 `;
82 }
83
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010084 disconnectedCallback() {
85 super.disconnectedCallback();
86 this._importerWindow?.close?.();
87 }
88
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020089 checkValidity() {
90 return this.cannedResponseRef.value.reportValidity();
91 }
92
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +020093 _dispatchUpdateEvent() {
94 // Request an update for this component
95 this.requestUpdate();
96
97 // Transmit to other components that the action has changed
98 const e = new Event(
99 'replywithcr-action-updated', {bubbles: true, composed: true});
100 this.renderRoot.dispatchEvent(e);
101 }
102
103 _cannedResponseIdChanged() {
104 this._cannedResponseIdString = this.cannedResponseRef.value.value;
105 }
106
107 _subscribeChanged() {
108 this.subscribe = this.subscribeRef.value.selected;
109 }
110
111 _markAsAnswerChanged() {
112 this.markAsAnswer = this.markAsAnswerRef.value.selected;
113 }
114
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +0100115 _openCRImporter() {
116 if (!(this._importerWindow?.closed ?? true))
117 this._importerWindow?.close?.();
118
119 this._importerWindow = window.open(
120 'https://support.google.com/s/community/cannedresponses?TWPTImportToWorkflow&TWPTSelectedId=' +
121 encodeURIComponent(this._cannedResponseIdString),
122 '', 'popup,width=720,height=540');
123 }
124
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +0200125 get cannedResponseId() {
126 return this.action.getCannedResponseId() ?? 0;
127 }
128
129 set cannedResponseId(value) {
130 this.action.setCannedResponseId(value);
131 this._dispatchUpdateEvent();
132 }
133
134 get _cannedResponseIdString() {
135 let id = this.cannedResponseId;
136 if (id == 0) return '';
137 return id.toString();
138 }
139
140 set _cannedResponseIdString(value) {
141 this.cannedResponseId = parseInt(value);
142 }
143
144 get subscribe() {
145 return this.action.getSubscribe();
146 }
147
148 set subscribe(value) {
149 this.action.setSubscribe(value);
150 this._dispatchUpdateEvent();
151 }
152
153 get markAsAnswer() {
154 return this.action.getMarkAsAnswer();
155 }
156
157 set markAsAnswer(value) {
158 this.action.setMarkAsAnswer(value);
159 this._dispatchUpdateEvent();
160 }
161}
162window.customElements.define('wf-action-reply-with-cr', WFActionReplyWithCR);