Debian Codegraph @

addons/portal_rating/static/src/chatter/frontend/composer_patch.js

raw
1 import { Composer } from "@mail/core/common/composer";
2
3 import { patch } from "@web/core/utils/patch";
4 import { rpc } from "@web/core/network/rpc";
5 import { useState } from "@odoo/owl";
6 import { isMobileOS } from "@web/core/browser/feature_detection";
7
8 const MAX_STAR_RATING = 5;
9 const DEFAULT_STAR_RATING = 4;
10
11 patch(Composer.prototype, {
12 setup() {
13 super.setup(...arguments);
14 this.MAX_STAR_RATING = MAX_STAR_RATING;
15 this.portalState = useState({
16 hoveredRatingValue: undefined,
17 ratingValue: DEFAULT_STAR_RATING,
18 /** @deprecated: use 'hoveredRatingValue' instead */
19 get starValue() {
20 return this.hoveredRatingValue;
21 },
22 /** @deprecated: use 'hoveredRatingValue' instead */
23 set starValue(val) {
24 this.hoveredRatingValue = val;
25 },
26 });
27 },
28
29 get allowUpload() {
30 return super.allowUpload && !this.props.composer.portalComment;
31 },
32
33 async editMessage() {
34 if (this.props.composer.portalComment) {
35 await this.savePublisherComment();
36 return;
37 }
38 await super.editMessage();
39 },
40
41 async savePublisherComment() {
42 if (!this.state.active) {
43 return;
44 }
45 this.state.active = false;
46 const data = await rpc("/website/rating/comment", {
47 rating_id: this.message.rating_id.id,
48 publisher_comment: this.props.composer.composerText.trim(),
49 });
50 this.message.rating_id = data;
51 this.props.onPostCallback();
52 },
53
54 get canProcessMessage() {
55 return super.canProcessMessage || (this.message && this.message.rating_value);
56 },
57
58 get askDeleteFromEdit() {
59 return super.askDeleteFromEdit && !this.message.rating_value;
60 },
61
62 get visibleRatingValue() {
63 return this.portalState.hoveredRatingValue ?? this.portalState.ratingValue;
64 },
65
66 onMoveStar(ev) {
67 this.handleStar(ev, { hovered: !isMobileOS() });
68 },
69
70 handleStar(ev, { hovered } = {}) {
71 const index = parseInt(ev.currentTarget.getAttribute("index"));
72 if (Number.isNaN(index) || index < 0 || index > MAX_STAR_RATING - 1) {
73 if (hovered) {
74 this.portalState.hoveredRatingValue = undefined;
75 }
76 return;
77 }
78 if (hovered) {
79 this.portalState.hoveredRatingValue = index + 1;
80 } else {
81 this.portalState.ratingValue = index + 1;
82 }
83 },
84
85 onClickStar(ev) {
86 this.handleStar(ev);
87 },
88
89 onMouseLeaveStar(ev) {
90 if (!isMobileOS()) {
91 this.handleStar(ev, { hovered: true });
92 }
93 },
94
95 get postData() {
96 const postData = super.postData;
97 if (this.env.displayRating && !this.message) {
98 postData.rating_value = this.portalState.ratingValue;
99 }
100 this.portalState.ratingValue = DEFAULT_STAR_RATING;
101 return postData;
102 },
103 });