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

165 lines
4.5 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";
import {
createStyles,
Theme,
withStyles,
WithStyles
} from "@material-ui/core/styles";
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";
2019-06-19 14:40:52 +00:00
import { DateContext } from "../Date/DateContext";
const styles = (theme: Theme) =>
createStyles({
date: {
"& svg": {
fill: theme.palette.primary.main
},
2019-09-09 14:07:09 +00:00
marginTop: theme.spacing.unit * 3
2019-06-19 14:40:52 +00:00
},
2019-09-16 01:00:38 +00:00
label: {
lineHeight: 1,
margin: 0
},
secondLabel: {
fontSize: 12
},
2019-09-09 14:07:09 +00:00
setPublicationDate: {
color: theme.palette.primary.main,
cursor: "pointer",
fontSize: "14px",
paddingTop: "15px",
textDecoration: "underline"
2019-06-19 14:40:52 +00:00
}
});
interface VisibilityCardProps extends WithStyles<typeof styles> {
children?: React.ReactNode | React.ReactNodeArray;
data: {
isPublished: boolean;
publicationDate: string;
};
errors: { [key: string]: string };
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
}
export const VisibilityCard = withStyles(styles, {
name: "VisibilityCard"
})(
({
children,
classes,
data: { isPublished, publicationDate },
errors,
disabled,
hiddenMessage,
onChange,
visibleMessage
2019-06-19 14:40:52 +00:00
}: VisibilityCardProps) => {
const intl = useIntl();
2019-09-09 14:07:09 +00:00
const [isPublicationDate, setPublicationDate] = React.useState(
publicationDate === null ? true : false
);
2019-06-19 14:40:52 +00:00
const dateNow = React.useContext(DateContext);
2019-09-09 14:07:09 +00:00
const visibleSecondLabel = publicationDate
? isPublished
? visibleMessage
2019-09-09 14:07:09 +00:00
: null
: null;
const hiddenSecondLabel = publicationDate
? isPublished
? null
: Date.parse(publicationDate) > dateNow
? hiddenMessage
2019-09-09 14:07:09 +00:00
: null
: null;
2019-06-19 14:40:52 +00:00
return (
<Card>
<CardTitle
title={intl.formatMessage({
defaultMessage: "Visibility",
description: "section header"
})}
/>
2019-06-19 14:40:52 +00:00
<CardContent>
2019-09-16 01:00:38 +00:00
<RadioSwitchField
2019-09-12 08:46:46 +00:00
disabled={disabled}
2019-09-16 01:00:38 +00:00
firstOptionLabel={
<>
<p className={classes.label}>
{intl.formatMessage({
defaultMessage: "Visible"
})}
</p>
<span className={classes.secondLabel}>
{visibleSecondLabel}
</span>
</>
}
2019-09-12 08:46:46 +00:00
name={"isPublished" as keyof FormData}
2019-09-16 01:00:38 +00:00
secondOptionLabel={
<>
<p className={classes.label}>
{intl.formatMessage({
defaultMessage: "Hidden"
})}
</p>
<span className={classes.secondLabel}>{hiddenSecondLabel}</span>
</>
}
value={isPublished}
2019-09-12 08:46:46 +00:00
onChange={onChange}
/>
2019-09-16 01:00:38 +00:00
{!isPublished && (
2019-06-19 14:40:52 +00:00
<>
2019-09-16 01:00:38 +00:00
{!isPublished && (
2019-09-09 14:07:09 +00:00
<Typography
className={classes.setPublicationDate}
onClick={() => setPublicationDate(!isPublicationDate)}
>
2019-09-11 14:24:24 +00:00
{intl.formatMessage({
2019-09-12 08:46:46 +00:00
defaultMessage: "Set publication date"
2019-09-11 14:24:24 +00:00
})}
2019-09-09 14:07:09 +00:00
</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
}}
/>
)}
2019-06-19 14:40:52 +00:00
</>
)}
{children}
</CardContent>
</Card>
);
}
);
VisibilityCard.displayName = "VisibilityCard";
export default VisibilityCard;