in public/src/components/channelManagement/bannerTests/variantEditor.tsx [65:158]
marginLeft: spacing(1),
},
},
switchLabel: {
fontSize: '14px',
fontWeight: 500,
},
}));
const BODY_DEFAULT_HELPER_TEXT = 'Main banner message paragraph';
const HIGHTLIGHTED_TEXT_HELPER_TEXT = 'Final sentence of body copy';
const BODY_COPY_WITHOUT_SECONDARY_CTA_RECOMMENDED_LENGTH = 500;
const BODY_COPY_WITH_SECONDARY_CTA_RECOMMENDED_LENGTH = 500;
type DeviceType = 'ALL' | 'MOBILE' | 'NOT_MOBILE';
const getLabelSuffix = (deviceType: DeviceType): string => {
switch (deviceType) {
case 'MOBILE':
return ' (mobile only)';
case 'NOT_MOBILE':
return ' (tablet + desktop)';
default:
return ' (all devices)';
}
};
interface VariantContentEditorProps {
content: BannerContent;
template: BannerUi;
onChange: (updatedContent: BannerContent) => void;
onValidationChange: (isValid: boolean) => void;
editMode: boolean;
deviceType: DeviceType;
}
interface FormData {
heading?: string;
paragraphs: string[];
highlightedText?: string;
}
// Temporary, while we migrate from messageText to paragraphs
const getParagraphsOrMessageText = (
paras: string[] | undefined,
text: string | undefined,
): string[] => {
const bodyCopy = [];
if (paras != null) {
bodyCopy.push(...paras);
} else if (text != null) {
bodyCopy.push(text);
}
return bodyCopy;
};
const VariantContentEditor: React.FC<VariantContentEditorProps> = ({
content,
onChange,
onValidationChange,
editMode,
deviceType,
}: VariantContentEditorProps) => {
const classes = useStyles();
const templateValidator = templateValidatorForPlatform('DOTCOM');
const defaultValues: FormData = {
heading: content.heading || '',
paragraphs: getParagraphsOrMessageText(content.paragraphs, content.messageText),
highlightedText: content.highlightedText || '',
};
/**
* Only some fields are validated by the useForm here.
* Ideally we'd combine the validated fields with the rest of the variant fields in a callback (inside the RTE Controllers below).
* But the callback closes over the old state of `content`, causing it to overwrite changes to non-validated fields.
* So instead we write updates to the validated fields to the `validatedFields` state, and merge with the rest of
* `content` in a useEffect.
*/
const [validatedFields, setValidatedFields] = useState<FormData>(defaultValues);
const { handleSubmit, control, errors, trigger } = useForm<FormData>({
mode: 'onChange',
defaultValues,
});
useEffect(() => {
trigger();
}, []);
useEffect(() => {
onChange({