Debian Codegraph @

addons/web/static/src/views/calendar/quick_create/calendar_quick_create.js

raw
1 import { useAutofocus, useService } from "@web/core/utils/hooks";
2 import { Dialog } from "@web/core/dialog/dialog";
3 import { _t } from "@web/core/l10n/translation";
4
5 import { Component } from "@odoo/owl";
6
7 export class CalendarQuickCreate extends Component {
8 static template = "web.CalendarQuickCreate";
9 static components = {
10 Dialog,
11 };
12 static props = {
13 title: { type: String, optional: true },
14 close: Function,
15 record: Object,
16 model: Object,
17 editRecord: Function,
18 };
19
20 setup() {
21 this.titleRef = useAutofocus({ refName: "title" });
22 this.notification = useService("notification");
23 this.creatingRecord = false;
24 }
25
26 get dialogTitle() {
27 return _t("New Event");
28 }
29
30 get recordTitle() {
31 return this.titleRef.el.value.trim();
32 }
33 get record() {
34 return {
35 ...this.props.record,
36 title: this.recordTitle,
37 };
38 }
39
40 editRecord() {
41 this.props.editRecord(this.record);
42 this.props.close();
43 }
44 async createRecord() {
45 if (this.creatingRecord) {
46 return;
47 }
48
49 if (this.recordTitle) {
50 try {
51 this.creatingRecord = true;
52 await this.props.model.createRecord(this.record);
53 this.props.close();
54 } catch {
55 this.editRecord();
56 }
57 } else {
58 this.titleRef.el.classList.add("o_field_invalid");
59 this.notification.add(_t("Meeting Subject"), {
60 title: _t("Invalid fields"),
61 type: "danger",
62 });
63 }
64 }
65
66 onInputKeyup(ev) {
67 switch (ev.key) {
68 case "Enter":
69 this.createRecord();
70 break;
71 case "Escape":
72 this.props.close();
73 break;
74 }
75 }
76 onCreateBtnClick() {
77 this.createRecord();
78 }
79 onEditBtnClick() {
80 this.editRecord();
81 }
82 onCancelBtnClick() {
83 this.props.close();
84 }
85 }