import {
drawerInputClass,
drawerTextareaClass,
Field,
Section,
Toggle,
useScopedFormContext
} from '@components/common/page-builder/index.js';
import {
Alert,
AlertDescription,
AlertTitle
} from '@components/common/ui/Alert.js';
import { _ } from '@evershop/evershop/lib/locale/translate/_';
import { Mail, TriangleAlert } from 'lucide-react';
import React from 'react';
import { useQuery } from 'urql';
const StoreEmailQuery = `
query StoreEmail {
setting {
storeEmail
}
storeSettingUrl: url(routeId: "storeSetting")
}
`;
/**
* Where the messages go.
*
* The recipient is the store email setting and nothing else — never a
* per-widget address, which would be scraped straight off the page source. That
* is invisible from inside the drawer, so state it either way rather than only
* when something is wrong: a merchant configuring this form should not have to
* guess where a customer's message will land.
*
* Both branches also say that submissions are stored regardless, because the
* email is a notification, not the record.
*/
function StoreEmailNote() {
const [{ data, fetching }] = useQuery({ query: StoreEmailQuery });
if (fetching) {
return null;
}
const storeEmail = data?.setting?.storeEmail;
const settingsUrl = data?.storeSettingUrl;
if (!storeEmail) {
return (
{_('Store email is not set')}
{_(
'Messages will still be saved and visible under CMS → Contact messages, but no notification email will be sent.'
)}{' '}
{settingsUrl && (
{_('Add a store email')}
)}
);
}
// Quiet by design — this is reassurance on every open, not an alert.
return (
{_(
'Messages are sent to ${email} and saved under CMS → Contact messages.',
{
email: storeEmail
}
)}{' '}
{settingsUrl && (
{_('Change')}
)}
);
}
interface ContactFormSettingProps {
contactFormWidget?: {
title?: string | null;
subtitle?: string | null;
submitLabel?: string | null;
successMessage?: string | null;
showPhone?: boolean | null;
showSubject?: boolean | null;
consentEnabled?: boolean | null;
consentText?: string | null;
};
}
export default function ContactFormSetting({
contactFormWidget
}: ContactFormSettingProps) {
// Optional-chained: in the page-builder drawer this mounts via
// with no GraphQL props — the page-level form
// already holds the values.
const {
title = '',
subtitle = '',
submitLabel = '',
successMessage = '',
showPhone,
showSubject,
consentEnabled,
consentText = ''
} = contactFormWidget ?? {};
const { setValue, watch } = useScopedFormContext();
const phoneOn =
(watch('settings.showPhone') as boolean | null) ?? showPhone ?? false;
const subjectOn =
(watch('settings.showSubject') as boolean | null) ?? showSubject ?? false;
const consentOn =
(watch('settings.consentEnabled') as boolean | null) ??
consentEnabled ??
false;
return (
// The drawer's `space-y-6` applies to the wrapper, not to what the
// setting component renders inside it — so the spacing between sections is
// ours to supply, like every other widget setting component.
setValue('settings.showPhone', v, { shouldDirty: true })
}
/>
setValue('settings.showSubject', v, { shouldDirty: true })
}
/>
setValue('settings.consentEnabled', v, { shouldDirty: true })
}
/>
{consentOn && (
)}
);
}
export const query = `
query Query(
$title: String
$subtitle: String
$submitLabel: String
$successMessage: String
$showPhone: Boolean
$showSubject: Boolean
$consentEnabled: Boolean
$consentText: String
) {
contactFormWidget(
title: $title
subtitle: $subtitle
submitLabel: $submitLabel
successMessage: $successMessage
showPhone: $showPhone
showSubject: $showSubject
consentEnabled: $consentEnabled
consentText: $consentText
) {
title
subtitle
submitLabel
successMessage
showPhone
showSubject
consentEnabled
consentText
}
}
`;
export const variables = `{
title: getWidgetSetting("title"),
subtitle: getWidgetSetting("subtitle"),
submitLabel: getWidgetSetting("submitLabel"),
successMessage: getWidgetSetting("successMessage"),
showPhone: getWidgetSetting("showPhone"),
showSubject: getWidgetSetting("showSubject"),
consentEnabled: getWidgetSetting("consentEnabled"),
consentText: getWidgetSetting("consentText")
}`;