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

176 lines
4.7 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
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";
2019-09-09 14:07:09 +00:00
import Typography from "@material-ui/core/Typography";
2019-08-09 10:26:22 +00:00
import React from "react";
import { useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import CardTitle from "@saleor/components/CardTitle";
2019-09-16 01:00:38 +00:00
import RadioSwitchField from "@saleor/components/RadioSwitchField";
2020-01-31 13:48:39 +00:00
import { FormErrors } from "@saleor/types";
2019-06-19 14:40:52 +00:00
import { DateContext } from "../Date/DateContext";
2020-01-31 13:48:39 +00:00
import FormSpacer from "../FormSpacer";
2019-06-19 14:40:52 +00:00
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
children: {
"& button": {
margin: "0 9px"
},
"& label": {
marginTop: theme.spacing(2.5)
}
2019-09-16 12:03:37 +00:00
},
2019-12-03 15:28:40 +00:00
date: {
"& svg": {
fill: theme.palette.primary.main
},
marginTop: theme.spacing(3)
},
label: {
lineHeight: 1,
margin: 0
2019-10-30 14:34:24 +00:00
},
2019-12-03 15:28:40 +00:00
secondLabel: {
fontSize: 12
},
setPublicationDate: {
color: theme.palette.primary.main,
cursor: "pointer",
fontSize: "14px",
paddingTop: "15px",
textDecoration: "underline"
}
}),
{ name: "VisibilityCard" }
);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
interface VisibilityCardProps {
2019-06-19 14:40:52 +00:00
children?: React.ReactNode | React.ReactNodeArray;
data: {
isPublished: boolean;
publicationDate: string;
};
2020-01-31 13:48:39 +00:00
errors: FormErrors<"isPublished" | "publicationDate">;
2019-06-19 14:40:52 +00:00
disabled?: boolean;
hiddenMessage: string;
2019-09-16 01:00:38 +00:00
onChange: (event: React.ChangeEvent<any>) => void;
visibleMessage: string;
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
export const VisibilityCard: React.FC<VisibilityCardProps> = props => {
const {
2019-06-19 14:40:52 +00:00
children,
2019-10-30 14:34:24 +00:00
2019-06-19 14:40:52 +00:00
data: { isPublished, publicationDate },
errors,
disabled,
hiddenMessage,
onChange,
visibleMessage
2019-10-30 14:34:24 +00:00
} = props;
const classes = useStyles(props);
2019-09-09 14:07:09 +00:00
2019-10-30 14:34:24 +00:00
const intl = useIntl();
const [isPublicationDate, setPublicationDate] = React.useState(
publicationDate === null ? true : false
);
const dateNow = React.useContext(DateContext);
const visibleSecondLabel = publicationDate
? isPublished
? visibleMessage
: null
: null;
const hiddenSecondLabel = publicationDate
? isPublished
? null
: Date.parse(publicationDate) > dateNow
? hiddenMessage
: null
: null;
return (
<Card>
<CardTitle
title={intl.formatMessage({
defaultMessage: "Visibility",
description: "section header"
})}
/>
<CardContent>
<RadioSwitchField
disabled={disabled}
2020-01-31 13:48:39 +00:00
error={!!errors.isPublished}
2019-10-30 14:34:24 +00:00
firstOptionLabel={
2019-06-19 14:40:52 +00:00
<>
2019-10-30 14:34:24 +00:00
<p className={classes.label}>
{intl.formatMessage({
defaultMessage: "Visible"
})}
</p>
<span className={classes.secondLabel}>{visibleSecondLabel}</span>
2019-06-19 14:40:52 +00:00
</>
2019-10-30 14:34:24 +00:00
}
name={"isPublished" as keyof FormData}
secondOptionLabel={
<>
<p className={classes.label}>
{intl.formatMessage({
defaultMessage: "Hidden"
})}
</p>
<span className={classes.secondLabel}>{hiddenSecondLabel}</span>
</>
}
value={isPublished}
onChange={onChange}
/>
{!isPublished && (
<>
{!isPublished && (
<Typography
className={classes.setPublicationDate}
onClick={() => setPublicationDate(!isPublicationDate)}
>
{intl.formatMessage({
defaultMessage: "Set publication date"
})}
</Typography>
)}
{isPublicationDate && (
<TextField
error={!!errors.publicationDate}
disabled={disabled}
label={intl.formatMessage({
defaultMessage: "Publish on",
description: "publish on date"
})}
name="publicationDate"
type="date"
fullWidth={true}
helperText={errors.publicationDate}
value={publicationDate ? publicationDate : ""}
onChange={onChange}
className={classes.date}
InputLabelProps={{
shrink: true
}}
/>
)}
</>
)}
2020-01-31 13:48:39 +00:00
{errors.isPublished && (
<>
<FormSpacer />
<Typography color="error">{errors.isPublished}</Typography>
</>
)}
2019-10-30 14:34:24 +00:00
<div className={classes.children}>{children}</div>
</CardContent>
</Card>
);
};
2019-06-19 14:40:52 +00:00
VisibilityCard.displayName = "VisibilityCard";
export default VisibilityCard;