import { ColorSwatchField, drawerInputClass, drawerTextareaClass, Field, Section, Segmented, Toggle, useScopedFormContext } from '@components/common/page-builder/index.js'; import { LinkPicker } from '@components/common/page-builder/pickers/LinkPicker.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React from 'react'; import type { CouponBorderStyle } from './CouponBlock.js'; interface CouponBlockSettingProps { couponBlockWidget?: { eyebrow?: string | null; heading?: string; body?: string | null; code?: string; ctaLabel?: string | null; ctaLink?: string; ctaNewTab?: boolean | null; expires?: string | null; borderStyle?: CouponBorderStyle | null; backgroundColor?: string | null; }; } const BORDER_OPTIONS: ReadonlyArray<{ value: CouponBorderStyle; label: string; }> = [ { value: 'solid', label: _('Solid') }, { value: 'dashed', label: _('Dashed') }, { value: 'none', label: _('None') } ]; function formatCountdown(iso: string): string { const t = Date.parse(iso); if (!Number.isFinite(t)) return ''; const diff = t - Date.now(); if (diff <= 0) return _('Already expired'); const days = Math.floor(diff / (1000 * 60 * 60 * 24)); const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); if (days >= 1) return _('Expires in ${days}d ${hours}h', { days: String(days), hours: String(hours) }); const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); return _('Expires in ${hours}h ${minutes}m', { hours: String(hours), minutes: String(minutes) }); } export default function CouponBlockSetting({ couponBlockWidget }: CouponBlockSettingProps) { const { eyebrow, heading, body, code, ctaLabel, ctaLink, ctaNewTab, expires, borderStyle, backgroundColor } = couponBlockWidget ?? {}; const { register, setValue, watch } = useScopedFormContext(); const eyebrowV = (watch('settings.eyebrow') as string) ?? eyebrow ?? ''; const headingV = (watch('settings.heading') as string) ?? heading ?? ''; const bodyV = (watch('settings.body') as string) ?? body ?? ''; const codeV = (watch('settings.code') as string) ?? code ?? ''; const ctaLabelV = (watch('settings.ctaLabel') as string) ?? ctaLabel ?? ''; const ctaLinkV = (watch('settings.ctaLink') as string) ?? ctaLink ?? ''; const ctaNewTabV = (watch('settings.ctaNewTab') as boolean | null) ?? ctaNewTab ?? false; const expiresV = (watch('settings.expires') as string) ?? expires ?? ''; const borderStyleV = ((watch('settings.borderStyle') as string) ?? borderStyle ?? 'dashed') as CouponBorderStyle; const backgroundColorV = (watch('settings.backgroundColor') as string) ?? backgroundColor ?? ''; // datetime-local accepts `YYYY-MM-DDTHH:MM`; storage is ISO. Convert both // ways so the input mirrors what's saved. const localExpiresInput = expiresV ? (() => { const d = new Date(expiresV); if (Number.isNaN(d.getTime())) return ''; const pad = (n: number) => String(n).padStart(2, '0'); return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad( d.getDate() )}T${pad(d.getHours())}:${pad(d.getMinutes())}`; })() : ''; return (
setValue('settings.eyebrow', e.target.value, { shouldDirty: true }) } placeholder={_('Limited · Ends Sunday')} className={drawerInputClass} /> setValue('settings.heading', e.target.value, { shouldDirty: true }) } placeholder={_('Take 20% off your order')} className={drawerInputClass} /> setValue('settings.body', e.target.value, { shouldDirty: true }) } placeholder={_('Use code at checkout')} className={drawerInputClass} /> setValue('settings.code', e.target.value, { shouldDirty: true }) } placeholder="SUMMER20" className={`${drawerInputClass} uppercase font-mono`} /> { const v = e.target.value; const iso = v ? new Date(v).toISOString() : ''; setValue('settings.expires', iso || null, { shouldDirty: true }); }} className={drawerInputClass} />
setValue('settings.ctaLabel', e.target.value, { shouldDirty: true }) } placeholder={_('Shop now →')} className={drawerInputClass} /> setValue('settings.ctaLink', url, { shouldDirty: true }) } /> setValue('settings.ctaNewTab', v, { shouldDirty: true }) } />
value={borderStyleV} options={BORDER_OPTIONS} onChange={(v) => setValue('settings.borderStyle', v, { shouldDirty: true }) } /> setValue('settings.backgroundColor', v || null, { shouldDirty: true }) } />
{/* Hidden mirrors for legacy widgetEdit form submission. */}
); } export const query = ` query Query( $eyebrow: String $heading: String $body: String $code: String $ctaLabel: String $ctaLink: String $ctaNewTab: Boolean $expires: String $borderStyle: String $backgroundColor: String ) { couponBlockWidget( eyebrow: $eyebrow heading: $heading body: $body code: $code ctaLabel: $ctaLabel ctaLink: $ctaLink ctaNewTab: $ctaNewTab expires: $expires borderStyle: $borderStyle backgroundColor: $backgroundColor ) { eyebrow heading body code ctaLabel ctaLink ctaNewTab expires borderStyle backgroundColor } } `; export const variables = `{ eyebrow: getWidgetSetting("eyebrow"), heading: getWidgetSetting("heading"), body: getWidgetSetting("body"), code: getWidgetSetting("code"), ctaLabel: getWidgetSetting("ctaLabel"), ctaLink: getWidgetSetting("ctaLink"), ctaNewTab: getWidgetSetting("ctaNewTab", false), expires: getWidgetSetting("expires"), borderStyle: getWidgetSetting("borderStyle", "dashed"), backgroundColor: getWidgetSetting("backgroundColor") }`;