import { CollectionErrorFragment, PageErrorFragment, ProductErrorFragment, } from "@dashboard/graphql"; import { getFieldError, getProductErrorMessage } from "@dashboard/utils/errors"; import getPageErrorMessage from "@dashboard/utils/errors/page"; import { TextField } from "@material-ui/core"; import { makeStyles } from "@saleor/macaw-ui"; import { Box, Button, Input, Text } from "@saleor/macaw-ui/next"; import React from "react"; import { FormattedMessage, useIntl } from "react-intl"; import { DashboardCard } from "../Card"; enum SeoField { slug = "slug", title = "seoTitle", description = "seoDescription", } const SLUG_REGEX = /^[a-zA-Z0-9\-\_]+$/; const maxSlugLength = 255; const maxTitleLength = 70; const maxDescriptionLength = 300; const useStyles = makeStyles( { label: { flex: 1, }, labelContainer: { "& span": { paddingRight: 30, }, display: "flex", }, }, { name: "SeoForm" }, ); interface SeoFormProps { description?: string | null; descriptionPlaceholder: string; disabled?: boolean; errors?: Array< PageErrorFragment | ProductErrorFragment | CollectionErrorFragment >; loading?: boolean; helperText?: string; allowEmptySlug?: boolean; title: string | null; slug: string; slugPlaceholder?: string; titlePlaceholder: string; onChange(event: any); onClick?(); } export const SeoForm: React.FC = props => { const { description, descriptionPlaceholder, disabled, errors = [], helperText, loading, title, slug, slugPlaceholder, titlePlaceholder, onChange, } = props; const classes = useStyles(props); const intl = useIntl(); const [expanded, setExpansionStatus] = React.useState(false); const toggleExpansion = () => setExpansionStatus(!expanded); const shouldDisplayHelperText = helperText && !expanded; const getSlugHelperMessage = () => { const error = !!getError(SeoField.slug); return error ? getSlugErrorMessage() : ""; }; const getSlugErrorMessage = () => { 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: React.ChangeEvent) => { const { value } = event.target; if (value === "" || SLUG_REGEX.test(value)) { onChange(event); } }; const getError = (fieldName: SeoField) => getFieldError(errors, fieldName); return ( {shouldDisplayHelperText && {helperText}} {expanded && ( maxSlugLength} name={SeoField.slug} label={ {slug?.length > 0 && ( )} } helperText={getSlugHelperMessage()} size="small" value={slug} onChange={handleSlugChange} disabled={loading || disabled} maxLength={maxSlugLength} placeholder={slugPlaceholder} /> maxTitleLength} name={SeoField.title} value={title ?? ""} disabled={loading || disabled} onChange={onChange} maxLength={maxTitleLength} placeholder={titlePlaceholder} label={ {title?.length > 0 && ( )} } /> maxDescriptionLength} name={SeoField.description} label={
{description?.length > 0 && ( )}
} InputProps={{ inputProps: { maxLength: maxDescriptionLength, }, }} value={description ?? ""} onChange={onChange} disabled={loading || disabled} fullWidth multiline placeholder={descriptionPlaceholder} rows={10} />
)}
); };