saleor-dashboard/src/components/SeoForm/SeoForm.tsx

299 lines
8.5 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-06-19 14:40:52 +00:00
import TextField from "@material-ui/core/TextField";
import Typography from "@material-ui/core/Typography";
2020-09-24 12:11:30 +00:00
import { PageErrorFragment } from "@saleor/fragments/types/PageErrorFragment";
import { ProductErrorFragment } from "@saleor/fragments/types/ProductErrorFragment";
import { PageErrorCode, ProductErrorCode } from "@saleor/types/globalTypes";
import { getProductErrorMessage } from "@saleor/utils/errors";
import getPageErrorMessage from "@saleor/utils/errors/page";
2019-08-09 11:14:35 +00:00
import classNames from "classnames";
2019-08-09 10:26:22 +00:00
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2020-09-18 14:40:48 +00:00
import slugify from "slugify";
2019-06-19 14:40:52 +00:00
import CardTitle from "../CardTitle";
import FormSpacer from "../FormSpacer";
2020-09-24 12:11:30 +00:00
enum SeoField {
slug = "slug",
title = "seoTitle",
description = "seoDescription"
}
const DEFAULT_INPUT_MESSAGE =
"If empty, the preview shows what will be autogenerated.";
interface Error {
__typename: "ProductError" | "PageError";
field: string | null;
code: ProductErrorCode | PageErrorCode;
}
const SLUG_REGEX = /^[a-z0-9]+(?:[\-a-z0-9]+)*$/;
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
addressBar: {
color: "#006621",
fontSize: "13px",
lineHeight: "16px",
marginBottom: "2px",
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap"
2019-06-19 14:40:52 +00:00
},
2019-12-03 15:28:40 +00:00
container: {
width: "100%"
},
descriptionBar: {
color: "#545454",
fontSize: "13px",
lineHeight: "18px",
overflowWrap: "break-word"
},
helperText: {
marginBottom: theme.spacing(3)
},
label: {
flex: 1
},
labelContainer: {
"& span": {
paddingRight: 30
},
display: "flex"
},
preview: {
minHeight: theme.spacing(10)
},
title: {
padding: 0
},
titleBar: {
color: "#1a0dab",
fontSize: "18px",
lineHeight: "21px",
overflowWrap: "break-word",
textDecoration: "none",
wordWrap: "break-word"
}
}),
{ name: "SeoForm" }
);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
interface SeoFormProps {
2019-06-19 14:40:52 +00:00
description?: string;
descriptionPlaceholder: string;
disabled?: boolean;
2020-09-24 12:11:30 +00:00
errors?: ProductErrorFragment[] | PageErrorFragment[];
2019-06-19 14:40:52 +00:00
loading?: boolean;
helperText?: string;
2020-09-24 12:11:30 +00:00
isCreating?: boolean;
2019-06-19 14:40:52 +00:00
title: string;
2020-09-18 14:40:48 +00:00
slug: string;
slugPlaceholder?: string;
2019-06-19 14:40:52 +00:00
titlePlaceholder: string;
onChange(event: any);
onClick?();
}
2019-10-30 14:34:24 +00:00
const SeoForm: React.FC<SeoFormProps> = props => {
const {
description,
2019-06-19 14:40:52 +00:00
descriptionPlaceholder,
disabled,
2020-09-24 12:11:30 +00:00
errors,
2019-06-19 14:40:52 +00:00
helperText,
2020-09-24 12:11:30 +00:00
isCreating = false,
2019-06-19 14:40:52 +00:00
loading,
title,
2020-09-17 14:06:13 +00:00
slug,
slugPlaceholder,
2019-06-19 14:40:52 +00:00
titlePlaceholder,
onChange
2019-10-30 14:34:24 +00:00
} = props;
const classes = useStyles(props);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
const intl = useIntl();
2019-10-30 14:34:24 +00:00
const [expanded, setExpansionStatus] = React.useState(false);
2019-10-30 14:34:24 +00:00
const toggleExpansion = () => setExpansionStatus(!expanded);
2020-09-22 11:10:53 +00:00
const shouldDisplayHelperText = helperText && !expanded;
2019-10-30 14:34:24 +00:00
2020-09-24 12:11:30 +00:00
const getFilteredErrors = () =>
(errors as Error[])?.filter(({ field }) =>
Object.keys(SeoField).includes(field)
) || [];
const getError = (fieldName: SeoField) =>
getFilteredErrors().find(({ field }) => fieldName === field);
const isError = (fieldName: SeoField) => !!getError(fieldName);
const getSlugHelperText = () => {
const error = isError(SeoField.slug);
if (isCreating && !error) {
return intl.formatMessage({
defaultMessage: DEFAULT_INPUT_MESSAGE
});
}
if (error) {
return getSlugErrorHelperText();
}
return "";
};
const getSlugErrorHelperText = () => {
const error = getError(SeoField.slug);
const { __typename: type } = error;
return type === "ProductError"
? getProductErrorMessage(error as ProductErrorFragment, intl)
: getPageErrorMessage(error as PageErrorFragment, intl);
};
const handleSlugChange = event => {
const { value } = event.target;
if (value === "" || !!value.match(SLUG_REGEX)) {
onChange(event);
}
};
2020-09-24 12:11:30 +00:00
2019-10-30 14:34:24 +00:00
return (
<Card>
<CardTitle
title={intl.formatMessage({
defaultMessage: "Search Engine Preview"
})}
toolbar={
<Button color="primary" variant="text" onClick={toggleExpansion}>
2020-09-21 14:30:18 +00:00
<FormattedMessage
defaultMessage="Edit website SEO"
description="button"
/>
2019-10-30 14:34:24 +00:00
</Button>
}
/>
<CardContent>
2020-09-22 11:10:53 +00:00
{shouldDisplayHelperText && (
2020-09-21 14:30:18 +00:00
<Typography
className={classNames({ [classes.helperText]: expanded })}
>
{helperText}
</Typography>
)}
2019-10-30 14:34:24 +00:00
{expanded && (
<div className={classes.container}>
2020-09-17 14:06:13 +00:00
<TextField
2020-09-24 12:11:30 +00:00
error={isError(SeoField.slug)}
name={SeoField.slug}
2020-09-17 14:06:13 +00:00
label={
<div className={classes.labelContainer}>
<div className={classes.label}>
<FormattedMessage defaultMessage="Slug" />
</div>
2020-09-18 14:40:48 +00:00
{slug?.length > 0 && (
2020-09-17 14:06:13 +00:00
<span>
<FormattedMessage
defaultMessage="{numberOfCharacters} of {maxCharacters} characters"
description="character limit"
values={{
maxCharacters: 70,
2020-09-18 14:40:48 +00:00
numberOfCharacters: slug?.length
2020-09-17 14:06:13 +00:00
}}
/>
</span>
)}
</div>
}
2020-09-24 12:11:30 +00:00
helperText={getSlugHelperText()}
value={slug?.slice(0, 69)}
2020-09-17 14:06:13 +00:00
disabled={loading || disabled}
2020-09-21 13:34:32 +00:00
placeholder={slug || slugify(slugPlaceholder, { lower: true })}
onChange={handleSlugChange}
2020-09-17 14:06:13 +00:00
fullWidth
/>
<FormSpacer />
2019-10-30 14:34:24 +00:00
<TextField
2020-09-24 12:11:30 +00:00
name={SeoField.title}
error={isError(SeoField.title)}
2019-10-30 14:34:24 +00:00
label={
<div className={classes.labelContainer}>
<div className={classes.label}>
<FormattedMessage defaultMessage="Search engine title" />
2019-06-19 14:40:52 +00:00
</div>
{title?.length > 0 && (
2019-10-30 14:34:24 +00:00
<span>
<FormattedMessage
defaultMessage="{numberOfCharacters} of {maxCharacters} characters"
description="character limit"
values={{
maxCharacters: 70,
numberOfCharacters: title.length
}}
/>
</span>
)}
</div>
}
helperText={intl.formatMessage({
2020-09-24 12:11:30 +00:00
defaultMessage: DEFAULT_INPUT_MESSAGE
2019-10-30 14:34:24 +00:00
})}
value={title?.slice(0, 69)}
2019-10-30 14:34:24 +00:00
disabled={loading || disabled}
placeholder={titlePlaceholder}
onChange={onChange}
fullWidth
/>
<FormSpacer />
<TextField
2020-09-24 12:11:30 +00:00
error={isError(SeoField.description)}
name={SeoField.description}
2019-10-30 14:34:24 +00:00
label={
<div className={classes.labelContainer}>
<div className={classes.label}>
<FormattedMessage defaultMessage="Search engine description" />
2019-06-19 14:40:52 +00:00
</div>
{description?.length > 0 && (
2019-10-30 14:34:24 +00:00
<span>
<FormattedMessage
defaultMessage="{numberOfCharacters} of {maxCharacters} characters"
description="character limit"
values={{
maxCharacters: 300,
numberOfCharacters: description.length
}}
/>
</span>
)}
</div>
}
helperText={intl.formatMessage({
2020-09-24 12:11:30 +00:00
defaultMessage: DEFAULT_INPUT_MESSAGE
2019-10-30 14:34:24 +00:00
})}
value={description?.slice(0, 299)}
2019-10-30 14:34:24 +00:00
onChange={onChange}
disabled={loading || disabled}
fullWidth
multiline
placeholder={descriptionPlaceholder}
rows={10}
/>
</div>
)}
</CardContent>
</Card>
);
};
2019-06-19 14:40:52 +00:00
SeoForm.displayName = "SeoForm";
export default SeoForm;