blob: 614a042347254e0fe718821c616715b0c24080f7 [file] [log] [blame]
Adrià Vilanova Martínezf276ac72022-10-13 22:16:22 +02001import '@material/web/textfield/outlined-text-field.js';
2import '@material/web/switch/switch.js';
3import '@material/web/formfield/formfield.js';
4
5import {html, LitElement} from 'lit';
6import {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},
15 };
16
17 cannedResponseRef = createRef();
18 subscribeRef = createRef();
19 markAsAnswerRef = createRef();
20
21 constructor() {
22 super();
23 this.action = new pb.workflows.Action.ReplyWithCRAction;
24 // this._loadUserCannedResponses();
25 }
26
27 render() {
28 return html`
29 <p>
30 <md-outlined-text-field ${ref(this.cannedResponseRef)}
31 type="number"
32 label="Canned response ID"
33 required
34 value=${this._cannedResponseIdString}
35 ?readonly=${this.readOnly}
36 @input=${this._cannedResponseIdChanged}>
37 </md-outlined-text-field>
38 </p>
39 <p>
40 <md-formfield label="Subscribe to thread">
41 <md-switch ${ref(this.subscribeRef)}
42 ?selected=${this.subscribe}
43 ?disabled=${this.readOnly}
44 @click=${this._subscribeChanged}/>
45 </md-formfield>
46 </p>
47 <p>
48 <md-formfield label="Mark as answer">
49 <md-switch ${ref(this.markAsAnswerRef)}
50 ?selected=${this.markAsAnswer}
51 ?disabled=${this.readOnly}
52 @click=${this._markAsAnswerChanged}/>
53 </md-formfield>
54 </p>
55 `;
56 }
57
58 checkValidity() {
59 return this.cannedResponseRef.value.reportValidity();
60 }
61
62 _loadUserCannedResponses() {
63 if (window.USER_CANNED_RESPONSES_STARTED_TO_LOAD) return;
64
65 window.USER_CANNED_RESPONSES_STARTED_TO_LOAD = true;
66 let searchParams = new URLSearchParams(document.location.search);
67 let authuser = searchParams.get('authuser') ?? 0;
68
69 // @TODO: This isn't as simple as doing this because the request contains
70 // the wrong origin and fails.
71 CCApi('ListCannedResponses', {}, true, authuser).then(res => {
72 console.log(res);
73 });
74 }
75
76 _dispatchUpdateEvent() {
77 // Request an update for this component
78 this.requestUpdate();
79
80 // Transmit to other components that the action has changed
81 const e = new Event(
82 'replywithcr-action-updated', {bubbles: true, composed: true});
83 this.renderRoot.dispatchEvent(e);
84 }
85
86 _cannedResponseIdChanged() {
87 this._cannedResponseIdString = this.cannedResponseRef.value.value;
88 }
89
90 _subscribeChanged() {
91 this.subscribe = this.subscribeRef.value.selected;
92 }
93
94 _markAsAnswerChanged() {
95 this.markAsAnswer = this.markAsAnswerRef.value.selected;
96 }
97
98 get cannedResponseId() {
99 return this.action.getCannedResponseId() ?? 0;
100 }
101
102 set cannedResponseId(value) {
103 this.action.setCannedResponseId(value);
104 this._dispatchUpdateEvent();
105 }
106
107 get _cannedResponseIdString() {
108 let id = this.cannedResponseId;
109 if (id == 0) return '';
110 return id.toString();
111 }
112
113 set _cannedResponseIdString(value) {
114 this.cannedResponseId = parseInt(value);
115 }
116
117 get subscribe() {
118 return this.action.getSubscribe();
119 }
120
121 set subscribe(value) {
122 this.action.setSubscribe(value);
123 this._dispatchUpdateEvent();
124 }
125
126 get markAsAnswer() {
127 return this.action.getMarkAsAnswer();
128 }
129
130 set markAsAnswer(value) {
131 this.action.setMarkAsAnswer(value);
132 this._dispatchUpdateEvent();
133 }
134}
135window.customElements.define('wf-action-reply-with-cr', WFActionReplyWithCR);