Debian Codegraph @

addons/portal_rating/static/src/interactions/portal_rating_composer.js

raw
1 import { Interaction } from "@web/public/interaction";
2 import { registry } from "@web/core/registry";
3 import { PortalComposer } from "@portal/interactions/portal_composer";
4 import { _t } from "@web/core/l10n/translation";
5 import { user } from "@web/core/user";
6
7 /**
8 * RatingPopupComposer
9 *
10 * Display the rating average with a static star widget, and open
11 * a popup with the portal composer when clicking on it.
12 **/
13 export class RatingPopupComposer extends Interaction {
14 static selector = ".o_rating_popup_composer";
15
16 setup() {
17 const options = this.el.dataset;
18 this.rating_avg = Math.round(options["rating_avg"] * 100) / 100 || 0.0;
19 this.rating_count = options["rating_count"] || 0.0;
20
21 this.options = Object.assign({
22 "token": false,
23 "res_model": false,
24 "res_id": false,
25 "pid": 0,
26 "display_rating": true,
27 "csrf_token": odoo.csrf_token,
28 "user_id": user.userId,
29 "reloadRatingPopupComposer": this.onReloadRatingPopupComposer.bind(this),
30 }, options, {});
31 this.env.bus.addEventListener("reload_rating_popup_composer", (ev) =>
32 this.onReloadRatingPopupComposer(ev.detail)
33 );
34 }
35
36 start() {
37 this.reloadRatingPopupComposer();
38 }
39
40 /**
41 * Destroy existing ratingPopup and insert new ratingPopup widget
42 */
43 reloadRatingPopupComposer() {
44 const starsEl = this.el.querySelector(".o_rating_popup_composer_stars");
45 if (this.options.hide_rating_avg) {
46 starsEl.replaceChildren();
47 } else {
48 starsEl.replaceChildren();
49 this.renderAt(
50 "portal_rating.rating_stars_static", {
51 inline_mode: true,
52 widget: this,
53 val: this.rating_avg,
54 }, starsEl);
55 }
56
57 // Append the modal
58 const modalEl = this.el.querySelector(".o_rating_popup_composer_modal");
59 modalEl.replaceChildren();
60 this.renderAt(
61 "portal_rating.PopupComposer", {
62 inline_mode: true,
63 widget: this,
64 val: this.rating_avg,
65 }, modalEl);
66
67 if (this.composerEl) {
68 this.services["public.interactions"].stopInteractions(this.composerEl);
69 }
70
71 // Instantiate the "Portal Composer" widget and insert it into the modal
72 // TODO Exchange options through another mean ?
73 const options = PortalComposer.prepareOptions(this.options);
74 // Change the text of send button
75 options.send_button_label = options.default_message_id ? _t("Update review") : _t("Post review");
76 this.env.portalComposerOptions = options;
77 const locationEl = this.el.querySelector(".o_rating_popup_composer_modal .o_portal_chatter_composer");
78 // TODO maybe always put in this.options - and prepare in setup ???
79 if (!locationEl) {
80 return;
81 }
82 this.composerEl = this.renderAt("portal.Composer", { widget: {options: this.env.portalComposerOptions }}, locationEl, "afterend")[0];
83 delete this.env.portalComposerOptions;
84 locationEl.remove();
85 // Change the text of the button
86 this.el.querySelector(".o_rating_popup_composer_text").textContent =
87 options.is_fullscreen
88 ? _t("Review")
89 : options.default_message_id
90 ? _t("Edit Review")
91 : _t("Add Review");
92 }
93
94 /**
95 * @param {Object} data
96 */
97 onReloadRatingPopupComposer(data) {
98 // Refresh the internal state of the widget
99 this.rating_avg = data["mail.thread"][0].rating_avg;
100 this.rating_count = data["mail.thread"][0].rating_count;
101 this.rating_value = data["rating.rating"]?.[0].rating;
102 this.updateOptions(data);
103 this.reloadRatingPopupComposer();
104 }
105
106 /**
107 * @param {Object} data
108 */
109 updateOptions(data) {
110 const message = data["mail.message"][0];
111 if (!message.author_id || message.author_id !== this.options.partner_id) {
112 return;
113 }
114 const body = message.body[1]?.replace(/<[^>]+>/g, "");
115 Object.assign(this.options, {
116 default_message: body,
117 default_message_id:
118 body || message.attachment_ids?.length || message.rating_id
119 ? message.id
120 : undefined,
121 default_attachment_ids: data["ir.attachment"],
122 default_rating_value: this.rating_value || 4,
123 });
124 }
125 }
126
127
128 registry
129 .category("public.interactions")
130 .add("portal_rating.rating_popup_composer", RatingPopupComposer);