|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState, useEffect } from 'react'; |
| 4 | + |
| 5 | +interface ExpiryDatePickerProps { |
| 6 | + value: string; |
| 7 | + onChange: (iso: string) => void; |
| 8 | + onTimezoneChange?: (tz: string) => void; |
| 9 | + timezone?: string; |
| 10 | + error?: string; |
| 11 | +} |
| 12 | + |
| 13 | +export default function ExpiryDatePicker({ |
| 14 | + value, |
| 15 | + onChange, |
| 16 | + onTimezoneChange, |
| 17 | + timezone: externalTimezone, |
| 18 | + error, |
| 19 | +}: ExpiryDatePickerProps) { |
| 20 | + const [timezone, setTimezone] = useState<string>(() => { |
| 21 | + if (externalTimezone) return externalTimezone; |
| 22 | + return Intl.DateTimeFormat().resolvedOptions().timeZone; |
| 23 | + }); |
| 24 | + |
| 25 | + const [timezones, setTimezones] = useState<string[]>([]); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + try { |
| 29 | + const supported = Intl.supportedValuesOf('timeZone') as string[]; |
| 30 | + setTimezones(supported); |
| 31 | + } catch { |
| 32 | + setTimezones([]); |
| 33 | + } |
| 34 | + }, []); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (externalTimezone) { |
| 38 | + setTimezone(externalTimezone); |
| 39 | + } |
| 40 | + }, [externalTimezone]); |
| 41 | + |
| 42 | + const handleTimezoneChange = (tz: string) => { |
| 43 | + setTimezone(tz); |
| 44 | + if (onTimezoneChange) { |
| 45 | + onTimezoneChange(tz); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + const isExpired = value && new Date(value) < new Date(); |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="space-y-3"> |
| 53 | + <div className="grid grid-cols-2 gap-3"> |
| 54 | + <div> |
| 55 | + <label htmlFor="expiry-date" className="block text-sm font-medium text-gray-300 mb-1"> |
| 56 | + Expiry Date & Time |
| 57 | + </label> |
| 58 | + <input |
| 59 | + id="expiry-date" |
| 60 | + type="datetime-local" |
| 61 | + value={value} |
| 62 | + onChange={(e) => onChange(e.target.value)} |
| 63 | + aria-invalid={isExpired || !!error} |
| 64 | + aria-describedby={error ? 'expiry-error' : undefined} |
| 65 | + className="w-full bg-gray-900 border border-gray-700 rounded-lg px-3 py-2 text-sm text-gray-100 focus:outline-none focus:ring-2 focus:ring-indigo-500 aria-invalid:border-red-600 aria-invalid:ring-red-600" |
| 66 | + /> |
| 67 | + </div> |
| 68 | + |
| 69 | + <div> |
| 70 | + <label htmlFor="timezone-select" className="block text-sm font-medium text-gray-300 mb-1"> |
| 71 | + Timezone |
| 72 | + </label> |
| 73 | + <select |
| 74 | + id="timezone-select" |
| 75 | + value={timezone} |
| 76 | + onChange={(e) => handleTimezoneChange(e.target.value)} |
| 77 | + className="w-full bg-gray-900 border border-gray-700 rounded-lg px-3 py-2 text-sm text-gray-100 focus:outline-none focus:ring-2 focus:ring-indigo-500" |
| 78 | + > |
| 79 | + {timezones.map((tz) => ( |
| 80 | + <option key={tz} value={tz}> |
| 81 | + {tz} |
| 82 | + </option> |
| 83 | + ))} |
| 84 | + </select> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + |
| 88 | + {(error || isExpired) && ( |
| 89 | + <div |
| 90 | + id="expiry-error" |
| 91 | + role="alert" |
| 92 | + className="text-sm text-red-400 bg-red-950/40 border border-red-800 rounded-lg px-3 py-2" |
| 93 | + > |
| 94 | + {error || 'Expiry date cannot be in the past'} |
| 95 | + </div> |
| 96 | + )} |
| 97 | + </div> |
| 98 | + ); |
| 99 | +} |
0 commit comments