import { Card, CardContent, TextField, Typography } from "@material-ui/core"; 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 { makeStyles } from "@saleor/theme"; import { UserError } from "@saleor/types"; import { getFieldError } from "@saleor/utils/errors"; import classNames from "classnames"; import React from "react"; import { useIntl } from "react-intl"; import { DateContext } from "../Date/DateContext"; import FormSpacer from "../FormSpacer"; import DateVisibilitySelector from "./DateVisibilitySelector"; 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(1) }, label: { lineHeight: 1.2, marginBottom: 5, marginTop: 0 }, listingLabel: { marginTop: 9 }, secondLabel: { color: theme.palette.text.hint, fontSize: 12, marginBottom: theme.spacing(2) }, switchField: { marginTop: theme.spacing(1) } }), { name: "VisibilityCard" } ); interface Message { visibleLabel: string; hiddenLabel: string; visibleSecondLabel?: string; hiddenSecondLabel: string; availableLabel?: string; unavailableLabel?: string; availableSecondLabel?: string; setAvailabilityDateLabel?: string; } export interface DateFields { publicationDate: string; availableForPurchase?: string; } export interface VisibilityCardProps { children?: React.ReactNode | React.ReactNodeArray; data: DateFields & { isAvailableForPurchase?: boolean; isPublished: boolean; 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 visibleMessage = (date: string) => intl.formatMessage( { defaultMessage: "since {date}", description: "date" }, { date: localizeDate(date, "L") } ); const handleRadioFieldChange = (type: keyof DateFields) => ( e: ChangeEvent ) => { const { value } = e.target; if (!value) { onChange({ target: { name: type, value: null } }); } return onChange(e); }; 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={handleRadioFieldChange("publicationDate")} /> {!isPublished && ( onChange({ target: { name: "publicationDate", value: null } }) } > )} {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={handleRadioFieldChange("availableForPurchase")} /> {!isAvailable && ( onChange({ target: { name: "availableForPurchase", value: null } }) } > )} {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;