File size: 3,015 Bytes
9c3ea7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const images = [
  "/static/img/imagem-1.png",
  "/static/img/imagem-2.png",
  "/static/img/imagem-3.png"
];

let current = 0;
const bg1 = document.getElementById("background1");
const bg2 = document.getElementById("background2");

let activeLayer = bg1;
let inactiveLayer = bg2;

// FunΓ§Γ£o para alternar entre camadas com fade para ficar bem babadeiro
function changeBackground() {
  if (!images.length || !bg1 || !bg2) return;

  inactiveLayer.style.backgroundImage = `url(${images[current]})`;
  inactiveLayer.style.opacity = 1;
  activeLayer.style.opacity = 0;

  [activeLayer, inactiveLayer] = [inactiveLayer, activeLayer];
  current = (current + 1) % images.length;
}

window.addEventListener("DOMContentLoaded", () => {
  if (!bg1 || !bg2) return;

  // Primeira imagem
  bg1.style.backgroundImage = `url(${images[current]})`;
  bg1.style.opacity = 1;
  bg2.style.opacity = 0;
  current++;

  setInterval(changeBackground, 10000);

  const form = document.getElementById("reviewForm");
  const result = document.getElementById("result");
  const ratingDisplay = document.getElementById("ratingDisplay");
  const reviewInput = document.getElementById("reviewInput");
  const restartBtn = document.getElementById("restartBtn");

  if (form) {
    form.addEventListener("submit", async (e) => {
      e.preventDefault();
      const text = reviewInput.value.trim();

      if (!text) return;

      ratingDisplay.innerHTML = "Analyzing...";
      result.classList.remove("hidden");
      ratingDisplay.classList.remove("hidden");

      try {
        const response = await fetch("/predict", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ text })
        });

        const data = await response.json();

        if (data.rating) {
          const stars = "⭐".repeat(data.rating);
          const labels = [
            "one star πŸ˜•",
            "two stars 😐",
            "three stars πŸ™‚",
            "four stars πŸ˜„",
            "five stars! πŸŽ‰"
          ];

          const label = labels[data.rating - 1] || "unrated πŸ€”";
          ratingDisplay.innerHTML = `${stars}, ${label}`;
          ratingDisplay.style.color = "white";
        } else {
          ratingDisplay.innerHTML = "Could not rate your review.";
          ratingDisplay.style.color = "white";

        }

        restartBtn.classList.remove("hidden");
      } catch (error) {
        console.error("Error:", error);
        ratingDisplay.innerHTML = "Something went wrong πŸ˜“";
        ratingDisplay.style.color = "white";
      }
    });
  }

  if (restartBtn) {
    restartBtn.addEventListener("click", () => {
      reviewInput.value = "";
      ratingDisplay.innerHTML = "";
      result.classList.add("hidden");
      ratingDisplay.classList.add("hidden");
      restartBtn.classList.add("hidden");
      reviewInput.focus();
    });
  }
});