import React from 'react'; import './Coupon.css'; // Import a CSS file for styling const Coupon = ({ code, value, period, type, expiration }) => { // Format the value based on type const formattedValue = type == 'fixed' ? `Diskon Rp${value}` : value != 0 ? `Diskon${value}%` : 'Voucher berlangganan'; // Function to convert expiration to Indonesian date format (dd MMMM yyyy) const formatExpirationDate = (dateString) => { const date = new Date(dateString); // Options for Indonesian date format const options = { weekday: 'long', // 'Monday' year: 'numeric', // '2025' month: 'long', // 'Januari' day: 'numeric' // '11' }; // Format the date to Indonesian locale (ID) return date.toLocaleDateString('id-ID', options); }; // Function to calculate the difference in days between expiration and current date (adjusted to UTC) const calculateDaysLeft = (expirationDate) => { const currentDate = new Date(); const expiration = new Date(expirationDate); // Convert both dates to UTC const utcCurrentDate = new Date(currentDate.toISOString()); // Ensure it's in UTC const utcExpirationDate = new Date(expiration.toISOString()); // Ensure it's in UTC // Calculate the time difference in milliseconds const timeDifference = utcExpirationDate - utcCurrentDate; const daysLeft = Math.ceil(timeDifference / (1000 * 3600 * 24)); // Convert to days return daysLeft; }; const daysLeft = expiration ? calculateDaysLeft(expiration) : null; return (
{(expiration == null && code != null) || (expiration != null && code != null && daysLeft < 1) && (

Voucher berakhir

)}
{code == null ? '404' : code}
{/*
*/}

{code == null ? 'Voucher tidak ditemukan' : formattedValue}

{type && {type}} {/* Display type if provided */}

{code == null ? '-' : value === 0 ? `Masa berlangganan ${period} minggu` : `Masa Voucher ${period} minggu`}

{expiration == null ? (code == null ? '-' : 'Tanpa kadaluarsa') : daysLeft < 0 ? `Berakhir ${Math.abs(daysLeft)} hari lalu` : daysLeft <= 7 ? `Berlaku hingga ${daysLeft} hari lagi` : `Berlaku hingga: ${formatExpirationDate(expiration)}`}

); }; export default Coupon;