Debian Codegraph @

addons/point_of_sale/static/src/app/screens/feedback_screen/feedback_screen.js

raw
1 import { registry } from "@web/core/registry";
2 import { Component, useRef, onMounted, useEffect, useState, onWillUnmount } from "@odoo/owl";
3 import { usePos } from "@point_of_sale/app/hooks/pos_hook";
4 import { PriceFormatter } from "@point_of_sale/app/components/price_formatter/price_formatter";
5 import { _t } from "@web/core/l10n/translation";
6 import { useService } from "@web/core/utils/hooks";
7
8 export class FeedbackScreen extends Component {
9 static template = "point_of_sale.FeedbackScreen";
10 static storeOnOrder = false;
11 static components = { PriceFormatter };
12 static props = {
13 orderUuid: String,
14 waitFor: { type: Object, optional: true },
15 paymentMethodId: { type: Number, optional: true, default: null },
16 };
17
18 setup() {
19 super.setup();
20 this.pos = usePos();
21 this.notification = useService("notification");
22 this.containerRef = useRef("feedback-screen");
23 this.amountRef = useRef("amount");
24 this.state = useState({
25 loading: true,
26 });
27
28 onMounted(() => {
29 this.scaleText();
30 });
31
32 useEffect(
33 () => {
34 const waiter = async () => {
35 try {
36 if (this.props.waitFor) {
37 await this.props.waitFor;
38 }
39 } finally {
40 this.state.loading = false;
41 this.timeout = setTimeout(() => {
42 this.goNext();
43 }, 5000);
44 }
45 };
46
47 waiter();
48 },
49 () => []
50 );
51
52 onWillUnmount(() => {
53 clearTimeout(this.timeout);
54 });
55 }
56
57 scaleText() {
58 const containerWidth = this.containerRef.el.offsetWidth * 0.8; // 80% of the container width to have some space on the sides
59 const textWidth = this.amountRef.el.scrollWidth;
60
61 const scale = Math.min(1, containerWidth / textWidth);
62 this.amountRef.el.style.transform = `scale(${scale})`;
63 }
64
65 get currentOrder() {
66 return this.pos.models["pos.order"].getBy("uuid", this.props.orderUuid);
67 }
68
69 onClick() {
70 if (this.state.loading) {
71 this.notification.add(
72 _t("A request is still being processed in the background. Please wait."),
73 {
74 type: "warning",
75 }
76 );
77 return;
78 }
79 clearTimeout(this.timeout);
80 this.goNext();
81 }
82
83 goNext() {
84 this.pos.orderDone(this.currentOrder);
85 }
86 }
87
88 registry.category("pos_pages").add("FeedbackScreen", {
89 name: "FeedbackScreen",
90 component: FeedbackScreen,
91 route: `/pos/ui/${odoo.pos_config_id}/resume/{string:orderUuid}`,
92 params: {},
93 });