import Card from "@material-ui/core/Card"; import CardContent from "@material-ui/core/CardContent"; import { makeStyles } from "@material-ui/core/styles"; import TextField from "@material-ui/core/TextField"; import Typography from "@material-ui/core/Typography"; import CardTitle from "@saleor/components/CardTitle"; import ControlledCheckbox from "@saleor/components/ControlledCheckbox"; import Hr from "@saleor/components/Hr"; import RadioSwitchField from "@saleor/components/RadioSwitchField"; import useDateLocalize from "@saleor/hooks/useDateLocalize"; import { ChangeEvent } from "@saleor/hooks/useForm"; import { UserError } from "@saleor/types"; import { getFieldError } from "@saleor/utils/errors"; import classNames from "classnames"; import React, { useState } from "react"; import { useIntl } from "react-intl"; import { DateContext } from "../Date/DateContext"; import FormSpacer from "../FormSpacer"; const useStyles = makeStyles( theme => ({ checkbox: { alignItems: "flex-start", marginTop: 10 }, children: { "& button": { margin: "0 9px" }, "& label": { marginTop: theme.spacing(2.5) } }, date: { "& svg": { fill: theme.palette.primary.main }, marginTop: theme.spacing(3) }, label: { lineHeight: 1.2, marginBottom: 5, marginTop: 0 }, listingLabel: { marginTop: 9 }, secondLabel: { color: theme.palette.text.hint, fontSize: 12 }, setPublicationDate: { color: theme.palette.primary.main, cursor: "pointer", fontSize: 14, paddingBottom: 10, paddingTop: 0 } }), { name: "VisibilityCard" } ); interface Message { visibleLabel: string; hiddenLabel: string; visibleSecondLabel?: string; hiddenSecondLabel: string; availableLabel?: string; unavailableLabel?: string; availableSecondLabel?: string; setAvailabilityDateLabel?: string; } export interface VisibilityCardProps { children?: React.ReactNode | React.ReactNodeArray; data: { availableForPurchase?: string; isAvailableForPurchase?: boolean; isPublished: boolean; publicationDate: string; visibleInListings?: boolean; }; errors: UserError[]; disabled?: boolean; messages: Message; onChange: (event: ChangeEvent) => void; } export const VisibilityCard: React.FC = props => { const { children, data: { availableForPurchase, isAvailableForPurchase: isAvailable, isPublished, publicationDate, visibleInListings }, errors, disabled, messages, onChange } = props; const classes = useStyles(props); const intl = useIntl(); const localizeDate = useDateLocalize(); const dateNow = React.useContext(DateContext); const hasAvailableProps = isAvailable !== undefined && availableForPurchase !== undefined; const [isPublicationDate, setPublicationDate] = useState( publicationDate === null ? true : false ); const [isAvailableDate, setAvailableDate] = useState( availableForPurchase === null ? true : false ); const visibleMessage = (date: string) => intl.formatMessage( { defaultMessage: "since {date}", description: "date" }, { date: localizeDate(date, "L") } ); return (

{messages.visibleLabel}

{isPublished && publicationDate && Date.parse(publicationDate) < dateNow && ( {messages.visibleSecondLabel || visibleMessage(publicationDate)} )} } name={"isPublished" as keyof FormData} secondOptionLabel={ <>

{messages.hiddenLabel}

{publicationDate && !isPublished && Date.parse(publicationDate) >= dateNow && ( {messages.hiddenSecondLabel} )} } value={isPublished} onChange={onChange} /> {!isPublished && ( <> setPublicationDate(!isPublicationDate)} > {intl.formatMessage({ defaultMessage: "Set publication date" })} {isPublicationDate && ( )} )} {getFieldError(errors, "isPublished") && ( <> {getFieldError(errors, "isPublished")?.message} )} {hasAvailableProps && ( <>

{messages.availableLabel}

{isAvailable && availableForPurchase && Date.parse(availableForPurchase) < dateNow && ( {visibleMessage(availableForPurchase)} )} } name={"isAvailableForPurchase" as keyof FormData} secondOptionLabel={ <>

{messages.unavailableLabel}

{availableForPurchase && !isAvailable && ( {messages.availableSecondLabel} )} } value={isAvailable} onChange={e => { const { value } = e.target; if (!value) { onChange({ target: { name: "availableForPurchase", value: null } }); } return onChange(e); }} /> {!isAvailable && ( <> setAvailableDate(!isAvailable)} > {messages.setAvailabilityDateLabel} {isAvailableDate && ( )} )} {getFieldError(errors, "isAvailableForPurchase") && ( <> {getFieldError(errors, "isAvailableForPurchase")?.message} )} )} {visibleInListings !== undefined && ( <>

{intl.formatMessage({ defaultMessage: "Show in product listings" })}

{intl.formatMessage({ defaultMessage: "Disabling this checkbox will remove product from search and category pages. It will be available on collection pages." })} } onChange={onChange} /> )}
{children}
); }; VisibilityCard.displayName = "VisibilityCard"; export default VisibilityCard;