progressBar.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { freeBeer } from "../service/product.js";
  2. import { getUserData } from "../service/user.js";
  3. const progressBar = document.getElementById('progressBar');
  4. const progressText = document.getElementById('progressText');
  5. let currentProgress = 0;
  6. // Función para actualizar la barra de progreso
  7. export function updateProgress(value) {
  8. const currentProgress = Math.min(value, 100);
  9. // Actualizar visualmente
  10. progressBar.style.width = currentProgress + '%';
  11. progressText.textContent = currentProgress + '%';
  12. // Cambiar colores según el progreso
  13. if (currentProgress >= 100) {
  14. progressBar.className = 'h-full bg-gradient-to-r from-green-400 to-emerald-500 rounded-full transition-all duration-500 ease-out shadow-sm relative overflow-hidden';
  15. progressText.textContent = '¡100% - Cerveza lista!';
  16. progressText.className = 'text-sm font-bold text-green-700';
  17. // Activar botón de recompensa
  18. rewardBtn.disabled = false;
  19. rewardBtn.className = 'text-xs bg-green-500 hover:bg-green-600 text-white px-3 py-1 rounded-full font-medium transition-all duration-200 animate-bounce';
  20. // Efecto de confeti (simulado con animación)
  21. const confetti = document.querySelector('.bg-gradient-to-r.from-amber-50')
  22. if (confetti) {
  23. confetti.className = 'mx-4 mb-6 p-4 bg-gradient-to-r from-green-50 to-emerald-50 rounded-xl border border-green-200 animate-pulse';
  24. }
  25. } else if (currentProgress >= 75) {
  26. progressBar.className = 'h-full bg-gradient-to-r from-orange-400 to-red-500 rounded-full transition-all duration-500 ease-out shadow-sm relative overflow-hidden';
  27. }
  28. }
  29. export function getProgress() {
  30. return currentProgress;
  31. }
  32. export async function claimReward(token, userTable) {
  33. const userData = await getUserData(token);
  34. const progress = userData.rewardProgress || 0;
  35. if (currentProgress >= progress) {
  36. const response = await freeBeer(token, userTable);
  37. if (!response || response.error) {
  38. alert("Error al reclamar la cerveza gratis. Por favor, inténtalo de nuevo más tarde.");
  39. return;
  40. }
  41. progressBar.style.width = '0%';
  42. progressBar.className = 'h-full bg-gradient-to-r from-amber-400 to-orange-500 rounded-full transition-all duration-500 ease-out shadow-sm relative overflow-hidden';
  43. progressText.textContent = '0%';
  44. progressText.className = 'text-sm font-bold text-amber-700';
  45. // Desactivar botón
  46. rewardBtn.disabled = true;
  47. rewardBtn.className = 'text-xs bg-amber-500 hover:bg-amber-600 text-white px-3 py-1 rounded-full font-medium transition-colors duration-200 opacity-50 cursor-not-allowed';
  48. // Restaurar colores originales
  49. document.querySelector('.bg-gradient-to-r.from-green-50').className = 'mx-4 mb-6 p-4 bg-gradient-to-r from-amber-50 to-orange-50 rounded-xl border border-amber-200';
  50. document.querySelector("#successRewardModal").classList.remove("hidden");
  51. }else {
  52. alert("Te pedimos disculpas, aún no has alcanzado el progreso necesario para reclamar tu recompensa. Notifica este error al administrador del sistema.");
  53. }
  54. }