-
Notifications
You must be signed in to change notification settings - Fork 6
/
contact-form.ts
246 lines (232 loc) · 7.7 KB
/
contact-form.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { html } from "lit";
import { customElement, query, state } from "lit/decorators.js";
import { View } from "../view";
import "@vaadin/date-picker";
import "@vaadin/text-field";
import "@vaadin/combo-box";
import "@vaadin/select";
import "@vaadin/button";
import "@vaadin/icon";
import "@vaadin/icons";
import "@vaadin/confirm-dialog";
import "@vaadin/tooltip";
import { Binder, field } from "@hilla/form";
import ContactModel from "Frontend/generated/com/example/application/data/entity/ContactModel";
import { crmStore, uiStore } from "Frontend/stores/app-store";
import { listViewStore } from "./list-view-store";
import { DatePicker } from "@vaadin/date-picker";
import { OverlayClosedEvent } from "@vaadin/overlay";
import { ComboBox } from "@vaadin/combo-box";
import { PastOrPresentWeekdayAndRequired } from "Frontend/util/validators";
import { lang } from "Frontend/util/localization";
import { statusSelectRenderer } from "./renderers";
import { selectRenderer } from "@vaadin/select/lit";
import "Frontend/components/live-currency-field";
import "@vaadin/number-field";
import { checkAllowedFormat } from "Frontend/util/datepicker-util";
import { Notification } from "@vaadin/notification";
import "Frontend/components/custom-confirm-dialog";
@customElement("contact-form")
export class ContactForm extends View {
// binder is public as we need to access it from ListView
binder = new Binder<ContactModel>(this, ContactModel);
@state()
private confirm = false;
@query("#datepicker")
private datePicker!: DatePicker;
@query("#company")
private companyBox!: ComboBox;
firstUpdated() {
const { model } = this.binder;
this.binder
.for(model.date)
.addValidator(new PastOrPresentWeekdayAndRequired());
lang.setDatePickerFormatter(this.datePicker);
this.companyBox.inputElement.addEventListener("keypress", () =>
this.companyBox.open()
);
this.datePicker.inputElement.addEventListener('change', e => {
const elm = e.target as HTMLInputElement;
if (!checkAllowedFormat(elm.value)) {
this.datePicker.helperText = lang.getText(uiStore.lang, "datepicker-format");
} else {
this.datePicker.helperText = null;
}
})
}
updated() {
lang.updateDatePickerI18n(this.datePicker, uiStore.lang);
}
@state()
private loading = false;
constructor() {
super();
this.autorun(() =>
this.binder.read(
listViewStore.selectedContact || ContactModel.createEmptyValue()
)
);
}
// Form has various modes of operation
// - When offline we show fields and buttons disabled
// - We allow editing only for ADMIN role
// - Note, elso update and delete end points are protected
render() {
const { model } = this.binder;
return html`
<vaadin-text-field
id="firstname"
label=${lang.getText(uiStore.lang, "first-name")}
?disabled=${uiStore.offline}
?readonly=${!uiStore.isAdmin()}
${field(model.firstName)}
></vaadin-text-field>
<vaadin-text-field
id="lastname"
label=${lang.getText(uiStore.lang, "last-name")}
?disabled=${uiStore.offline}
?readonly=${!uiStore.isAdmin()}
${field(model.lastName)}
></vaadin-text-field>
<vaadin-text-field
id="emailfield"
label=${lang.getText(uiStore.lang, "email")}
?readonly=${!uiStore.isAdmin()}
?disabled=${uiStore.offline}
${field(model.email)}
></vaadin-text-field>
<vaadin-combo-box
id="company"
label=${lang.getText(uiStore.lang, "company")}
.items=${crmStore.companies}
auto-open-disabled
clear-button-visible
?disabled=${uiStore.offline}
?readonly=${!uiStore.isAdmin()}
item-label-path="name"
${field(model.company)}
>
</vaadin-combo-box>
<vaadin-select
id="status"
?disabled=${uiStore.offline}
?readonly=${!uiStore.isAdmin()}
label=${lang.getText(uiStore.lang, "status")}
${selectRenderer(statusSelectRenderer, [
crmStore.statuses,
uiStore.lang,
])}
${field(model.status.id)}
></vaadin-select>
<vaadin-date-picker
id="datepicker"
label=${lang.getText(uiStore.lang, "date")}
theme="weekend"
auto-open-disabled
clear-button-visible
?disabled=${uiStore.offline}
?readonly=${!uiStore.isAdmin()}
${field(model.date)}
>
</vaadin-date-picker>
<live-currency-field
id="prospectvalue"
label=${lang.getText(uiStore.lang, "prospect-value")}
?readonly=${!uiStore.isAdmin()}
?disabled=${uiStore.offline}
${field(model.prospectValue)}
><vaadin-tooltip
slot="tooltip"
text=${lang.getText(uiStore.lang, "currency-field-tooltip")}
></vaadin-tooltip
>
</live-currency-field>
<div
class="buttons border-contrast-30 border-t mt-auto flex justify-between"
>
<vaadin-button
id="save"
theme="primary"
@click=${this.save}
?disabled=${!this.binder.dirty ||
this.binder.invalid ||
uiStore.offline ||
this.loading ||
!uiStore.isAdmin()}
>
<vaadin-icon icon="vaadin:disc"></vaadin-icon>
${this.binder.value.id
? lang.getText(uiStore.lang, "button-save")
: lang.getText(uiStore.lang, "button-create")}
</vaadin-button>
<vaadin-button
id="delete"
theme="error"
@click=${this.deleteClicked}
?disabled=${!this.binder.value.id ||
uiStore.offline ||
this.loading ||
!uiStore.isAdmin()}
>
<vaadin-icon icon="vaadin:trash"></vaadin-icon>
${lang.getText(uiStore.lang, "button-delete")}
</vaadin-button>
<vaadin-button
id="cancel"
theme="tertiary"
@click=${listViewStore.cancelEdit}
>
${lang.getText(uiStore.lang, "button-cancel")}
</vaadin-button>
</div>
<!-- confirm dialog is bound to confirm state variable -->
<!-- clicking delete will change it to true, which opens the dialog -->
<custom-confirm-dialog
header=${lang.getText(uiStore.lang, "contact-confirm-delete")}
cancel
cancel-text=${lang.getText(uiStore.lang, "button-cancel")}
confirm-text=${lang.getText(uiStore.lang, "button-confirm")}
@vaadin-overlay-closed=${this.overlayClosed}
@confirm=${this.delete}
@cancel=${this.cancelClicked}
.opened=${this.confirm}
><span class="whitespace-pre text-l">
<span class="text-secondary font-semibold text-s"
>${lang.getText(uiStore.lang, "contact")}:</span
>
${listViewStore.selectedContact?.firstName}
${listViewStore.selectedContact?.lastName}
</span></custom-confirm-dialog
>
`;
}
overlayClosed(e: OverlayClosedEvent) {
console.log("closed", e);
}
// Update the value to false after closing. Otherwise the dialog cannot
// be re-opened.
cancelClicked() {
this.confirm = false;
}
// Open the dialog by changing the value bound to opened property
deleteClicked() {
this.confirm = true;
}
async delete() {
await listViewStore.delete();
this.confirm = false;
this.submitDataChange();
}
async save() {
this.loading = true;
await this.binder.submitTo(listViewStore.save);
this.binder.clear();
this.loading = false;
this.submitDataChange();
}
// We dispatch a custom event so that ListView can observe changes
private submitDataChange() {
const event = new CustomEvent("data-change");
this.dispatchEvent(event);
}
}