progressBar.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. document.querySelector('.bg-gradient-to-r.from-amber-50').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';
  22. } else if (currentProgress >= 75) {
  23. 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';
  24. }
  25. }
  26. export async function claimReward(token, userTable) {
  27. const userData = await getUserData(token);
  28. const progress = userData.rewardProgress || 0;
  29. if (currentProgress >= progress) {
  30. const response = await freeBeer(token, userTable);
  31. if (!response || response.error) {
  32. alert("Error al reclamar la cerveza gratis. Por favor, inténtalo de nuevo más tarde.");
  33. return;
  34. }
  35. progressBar.style.width = '0%';
  36. 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';
  37. progressText.textContent = '0%';
  38. progressText.className = 'text-sm font-bold text-amber-700';
  39. // Desactivar botón
  40. rewardBtn.disabled = true;
  41. 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';
  42. // Restaurar colores originales
  43. 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';
  44. document.querySelector("#successRewardModal").classList.remove("hidden");
  45. }else {
  46. alert("Te pedimos disculpas, aún no has alcanzado el progreso necesario para reclamar tu recompensa. Notifica este error al administrador del sistema.");
  47. }
  48. }