fix seo form to work with null values from some props

This commit is contained in:
Magdalena Markusik 2020-09-21 14:45:57 +02:00
parent 37e0ecce26
commit 77063deb4f
2 changed files with 42 additions and 9 deletions

View file

@ -103,7 +103,7 @@ const CollectionDetailsPage: React.FC<CollectionDetailsPageProps> = ({
publicationDate: maybe(() => collection.publicationDate, ""), publicationDate: maybe(() => collection.publicationDate, ""),
seoDescription: maybe(() => collection.seoDescription, ""), seoDescription: maybe(() => collection.seoDescription, ""),
seoTitle: maybe(() => collection.seoTitle, ""), seoTitle: maybe(() => collection.seoTitle, ""),
slug: collection.slug || "" slug: collection?.slug || ""
}} }}
onSubmit={handleSubmit} onSubmit={handleSubmit}
confirmLeave confirmLeave

View file

@ -78,14 +78,14 @@ interface SeoFormProps {
const SeoForm: React.FC<SeoFormProps> = props => { const SeoForm: React.FC<SeoFormProps> = props => {
const { const {
description = "", description,
descriptionPlaceholder, descriptionPlaceholder,
disabled, disabled,
helperText, helperText,
loading, loading,
title = "", title,
slug, slug,
slugPlaceholder = "", slugPlaceholder,
titlePlaceholder, titlePlaceholder,
onChange onChange
} = props; } = props;
@ -95,6 +95,39 @@ const SeoForm: React.FC<SeoFormProps> = props => {
const [expanded, setExpansionStatus] = React.useState(false); const [expanded, setExpansionStatus] = React.useState(false);
const toggleExpansion = () => setExpansionStatus(!expanded); const toggleExpansion = () => setExpansionStatus(!expanded);
const shouldDisplayHelperText = () => helperText && !expanded; const shouldDisplayHelperText = () => helperText && !expanded;
// const getParsedProps = () => {
// const keysToParse: Array<keyof SeoFormProps> = [
// "slug",
// "slugPlaceholder",
// "description",
// "descriptionPlaceholder",
// "title",
// "titlePlaceholder"
// ];
// return reduce(
// props,
// (result, value, key: keyof SeoFormProps) => {
// console.log({ result, value, key });
// if (keysToParse.includes(key)) {
// return { ...result, [key]: !!value ? "" : value };
// }
// return result;
// },
// {} as Partial<SeoFormProps>
// );
// };
// const {
// title,
// titlePlaceholder,
// slug,
// slugPlaceholder,
// description,
// descriptionPlaceholder
// } = getParsedProps();
// console.log({ ...getParsedProps() });
return ( return (
<Card> <Card>
@ -146,7 +179,7 @@ const SeoForm: React.FC<SeoFormProps> = props => {
defaultMessage: defaultMessage:
"If empty, the preview shows what will be autogenerated." "If empty, the preview shows what will be autogenerated."
})} })}
value={slug.slice(0, 69)} value={slug?.slice(0, 69)}
disabled={loading || disabled} disabled={loading || disabled}
placeholder={slug || slugify(slugPlaceholder)} placeholder={slug || slugify(slugPlaceholder)}
onChange={onChange} onChange={onChange}
@ -160,7 +193,7 @@ const SeoForm: React.FC<SeoFormProps> = props => {
<div className={classes.label}> <div className={classes.label}>
<FormattedMessage defaultMessage="Search engine title" /> <FormattedMessage defaultMessage="Search engine title" />
</div> </div>
{title.length > 0 && ( {title?.length > 0 && (
<span> <span>
<FormattedMessage <FormattedMessage
defaultMessage="{numberOfCharacters} of {maxCharacters} characters" defaultMessage="{numberOfCharacters} of {maxCharacters} characters"
@ -178,7 +211,7 @@ const SeoForm: React.FC<SeoFormProps> = props => {
defaultMessage: defaultMessage:
"If empty, the preview shows what will be autogenerated." "If empty, the preview shows what will be autogenerated."
})} })}
value={title.slice(0, 69)} value={title?.slice(0, 69)}
disabled={loading || disabled} disabled={loading || disabled}
placeholder={titlePlaceholder} placeholder={titlePlaceholder}
onChange={onChange} onChange={onChange}
@ -192,7 +225,7 @@ const SeoForm: React.FC<SeoFormProps> = props => {
<div className={classes.label}> <div className={classes.label}>
<FormattedMessage defaultMessage="Search engine description" /> <FormattedMessage defaultMessage="Search engine description" />
</div> </div>
{description.length > 0 && ( {description?.length > 0 && (
<span> <span>
<FormattedMessage <FormattedMessage
defaultMessage="{numberOfCharacters} of {maxCharacters} characters" defaultMessage="{numberOfCharacters} of {maxCharacters} characters"
@ -210,7 +243,7 @@ const SeoForm: React.FC<SeoFormProps> = props => {
defaultMessage: defaultMessage:
"If empty, the preview shows what will be autogenerated." "If empty, the preview shows what will be autogenerated."
})} })}
value={description ? description.slice(0, 299) : undefined} value={description?.slice(0, 299)}
onChange={onChange} onChange={onChange}
disabled={loading || disabled} disabled={loading || disabled}
fullWidth fullWidth