import {
drawerInputClass,
Field,
ImagePickerField,
MarkdownBodyField,
Section,
Segmented,
useScopedFormContext
} from '@components/common/page-builder/index.js';
import { CollectionPicker } from '@components/common/page-builder/pickers/CollectionPicker.js';
import { LinkPicker } from '@components/common/page-builder/pickers/LinkPicker.js';
import { _ } from '@evershop/evershop/lib/locale/translate/_';
import React from 'react';
interface CollectionSpotlightSettingProps {
collectionSpotlightWidget?: {
collection?: string | null;
image?: string | null;
imageAlt?: string;
imagePosition?: 'left' | 'right';
imageWidth?: number | null;
imageHeight?: number | null;
eyebrow?: string | null;
heading?: string;
body?: string | null;
previewCount?: 2 | 4;
collectionName?: string | null;
viewAllLink?: string | null;
viewAllLabel?: string | null;
};
}
export default function CollectionSpotlightSetting({
collectionSpotlightWidget
}: CollectionSpotlightSettingProps) {
const {
collection,
image,
imageAlt,
imagePosition,
imageWidth,
imageHeight,
eyebrow,
heading,
body,
previewCount,
collectionName,
viewAllLink,
viewAllLabel
} = collectionSpotlightWidget ?? {};
const { register, setValue, watch } = useScopedFormContext();
const collectionV =
(watch('settings.collection') as string) ?? collection ?? '';
const imageV = (watch('settings.image') as string) ?? image ?? '';
const imageAltV = (watch('settings.imageAlt') as string) ?? imageAlt ?? '';
const imagePositionV =
((watch('settings.imagePosition') as string) ??
imagePosition ??
'left') as 'left' | 'right';
const eyebrowV =
(watch('settings.eyebrow') as string) ?? eyebrow ?? 'COLLECTION';
const headingV = (watch('settings.heading') as string) ?? heading ?? '';
const bodyV = (watch('settings.body') as string) ?? body ?? '';
const previewCountV =
((watch('settings.previewCount') as number) ?? previewCount ?? 4) as 2 | 4;
const viewAllLinkV =
(watch('settings.viewAllLink') as string) ?? viewAllLink ?? '';
const viewAllLabelV =
(watch('settings.viewAllLabel') as string) ?? viewAllLabel ?? '';
// The picker callback updates `_pickedName` so we can show the
// collection name as a placeholder for the heading override.
const pickedName = (watch('_pickedName') as string) ?? collectionName ?? '';
return (
{
setValue('settings.collection', code, { shouldDirty: true });
setValue('_pickedName', name);
// Seed heading with the collection name when the merchant
// hasn't typed one — keeps the spotlight useful out of the box.
if (!headingV) {
setValue('settings.heading', name, { shouldDirty: true });
}
}}
/>
{
setValue('settings.image', v || null, { shouldDirty: true });
if (!v) {
setValue('settings.imageWidth', null, { shouldDirty: true });
setValue('settings.imageHeight', null, { shouldDirty: true });
}
}}
onLoadDimensions={({ width: w, height: h }) => {
setValue('settings.imageWidth', w, { shouldDirty: true });
setValue('settings.imageHeight', h, { shouldDirty: true });
}}
/>
setValue('settings.imageAlt', e.target.value, {
shouldDirty: true
})
}
placeholder={_('Describe the image')}
className={drawerInputClass}
/>
value={imagePositionV}
options={[
{ value: 'left', label: _('Image left') },
{ value: 'right', label: _('Image right') }
]}
onChange={(v) =>
setValue('settings.imagePosition', v, { shouldDirty: true })
}
/>
value={previewCountV}
options={[
{ value: 2, label: _('2 (1×2)') },
{ value: 4, label: _('4 (2×2)') }
]}
onChange={(v) =>
setValue('settings.previewCount', v, { shouldDirty: true })
}
/>
);
}
export const query = `
query Query(
$collection: String
$image: String
$imageAlt: String
$imagePosition: String
$imageWidth: Float
$imageHeight: Float
$eyebrow: String
$heading: String
$body: String
$previewCount: Int
) {
collectionSpotlightWidget(
collection: $collection
image: $image
imageAlt: $imageAlt
imagePosition: $imagePosition
imageWidth: $imageWidth
imageHeight: $imageHeight
eyebrow: $eyebrow
heading: $heading
body: $body
previewCount: $previewCount
) {
collection
image
imageAlt
imagePosition
imageWidth
imageHeight
eyebrow
heading
body
previewCount
collectionName
}
}
`;
export const variables = `{
collection: getWidgetSetting("collection"),
image: getWidgetSetting("image"),
imageAlt: getWidgetSetting("imageAlt"),
imagePosition: getWidgetSetting("imagePosition", "left"),
imageWidth: getWidgetSetting("imageWidth"),
imageHeight: getWidgetSetting("imageHeight"),
eyebrow: getWidgetSetting("eyebrow", "COLLECTION"),
heading: getWidgetSetting("heading"),
body: getWidgetSetting("body"),
previewCount: getWidgetSetting("previewCount", 4)
}`;