ActivityModal.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import React, { useState, useEffect } from "react";
  2. import { X } from "lucide-react";
  3. const ActivityModal = () => {
  4. const [isOpen, setIsOpen] = useState(false);
  5. const [activityContent, setActivityContent] = useState(null);
  6. const [isLoading, setIsLoading] = useState(true);
  7. useEffect(() => {
  8. const fetchActivityContent = async () => {
  9. try {
  10. setIsLoading(true);
  11. const response = await fetch("/api/activity", {
  12. headers: {
  13. "x-from-frontend": "true",
  14. },
  15. });
  16. if (!response.ok) {
  17. throw new Error("Failed to fetch activity content");
  18. }
  19. const data = await response.json();
  20. console.log("data", data);
  21. if (data.success && data.data) {
  22. setActivityContent(data.data[0]);
  23. setIsOpen(true);
  24. } else {
  25. console.log(data.message || "No active activity");
  26. setActivityContent(null);
  27. }
  28. } catch (error) {
  29. console.error("Error fetching activity content:", error);
  30. setActivityContent(null);
  31. } finally {
  32. setIsLoading(false);
  33. }
  34. };
  35. fetchActivityContent();
  36. }, []);
  37. if (!isOpen || !activityContent || isLoading) return null;
  38. console.log("activityContent", activityContent);
  39. const hasBackgroundImage = !!activityContent?.backgroundImage;
  40. return (
  41. <>
  42. {hasBackgroundImage ? (
  43. <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
  44. <div className="relative">
  45. <button
  46. onClick={() => setIsOpen(false)}
  47. className="absolute -top-16 right-3 text-white hover:text-gray-300 z-20 p-2 rounded-full bg-gray-500 hover:bg-opacity-70 transition-all duration-300"
  48. >
  49. <X size={24} />
  50. </button>
  51. <div
  52. className="bg-cover bg-center rounded-lg px-6 pb-6 pt-16 max-w-sm w-full mx-4 overflow-hidden"
  53. style={{
  54. backgroundImage: `url('${activityContent.backgroundImage}')`,
  55. width: "80vw",
  56. minHeight: "50vh",
  57. backgroundPosition: "top center",
  58. }}
  59. >
  60. <div className="relative z-10 text-white">
  61. <div className="flex flex-col items-center mb-4 relative w-full">
  62. <div
  63. className="text-xl font-bold w-full text-center"
  64. dangerouslySetInnerHTML={{ __html: activityContent.title }}
  65. ></div>
  66. </div>
  67. <div
  68. className="overflow-y-auto"
  69. style={{ maxHeight: "calc(50vh - 100px)" }}
  70. dangerouslySetInnerHTML={{ __html: activityContent.content }}
  71. />
  72. </div>
  73. </div>
  74. </div>
  75. </div>
  76. ) : (
  77. <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
  78. <div className="bg-white rounded-lg p-6 max-w-sm w-full mx-4">
  79. <div className="flex flex-col items-center mb-4 relative w-full">
  80. <div
  81. className="text-xl font-bold text-black w-full"
  82. dangerouslySetInnerHTML={{ __html: activityContent.title }}
  83. ></div>
  84. <button
  85. onClick={() => setIsOpen(false)}
  86. className="text-gray-500 hover:text-gray-700 absolute right-0 top-0"
  87. >
  88. <X size={24} />
  89. </button>
  90. </div>
  91. <div
  92. className="text-gray-700"
  93. dangerouslySetInnerHTML={{ __html: activityContent.content }}
  94. />
  95. </div>
  96. </div>
  97. )}
  98. </>
  99. );
  100. };
  101. export default ActivityModal;