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";
|
2019-08-26 21:54:03 +00:00
|
|
|
import { useIntl } from "react-intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
import CardTitle from "@saleor/components/CardTitle";
|
|
|
|
import { FormSpacer } from "@saleor/components/FormSpacer";
|
2019-09-09 14:07:09 +00:00
|
|
|
import RadioGroupField from "@saleor/components/RadioGroupField";
|
2019-06-19 14:40:52 +00:00
|
|
|
import useDateLocalize from "@saleor/hooks/useDateLocalize";
|
|
|
|
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
|
|
|
},
|
|
|
|
expandedSwitchContainer: {
|
2019-09-09 14:07:09 +00:00
|
|
|
"& > div": {
|
|
|
|
padding: 0
|
|
|
|
},
|
2019-06-19 14:40:52 +00:00
|
|
|
marginBottom: 0
|
|
|
|
},
|
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;
|
|
|
|
onChange(event: any);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const VisibilityCard = withStyles(styles, {
|
|
|
|
name: "VisibilityCard"
|
|
|
|
})(
|
|
|
|
({
|
|
|
|
children,
|
|
|
|
classes,
|
|
|
|
data: { isPublished, publicationDate },
|
|
|
|
errors,
|
|
|
|
disabled,
|
|
|
|
onChange
|
|
|
|
}: VisibilityCardProps) => {
|
2019-08-26 21:54:03 +00:00
|
|
|
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 localizeDate = useDateLocalize();
|
|
|
|
const dateNow = React.useContext(DateContext);
|
2019-09-09 14:07:09 +00:00
|
|
|
const isPublishedRadio =
|
|
|
|
typeof isPublished !== "boolean"
|
|
|
|
? isPublished
|
|
|
|
: isPublished
|
|
|
|
? "true"
|
|
|
|
: "false";
|
|
|
|
const visibleSecondLabel = publicationDate
|
|
|
|
? isPublished
|
|
|
|
? intl.formatMessage(
|
|
|
|
{
|
|
|
|
defaultMessage: "since {date}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
date: localizeDate(publicationDate)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
: null;
|
|
|
|
const hiddenSecondLabel = publicationDate
|
|
|
|
? isPublished
|
|
|
|
? null
|
|
|
|
: Date.parse(publicationDate) > dateNow
|
|
|
|
? intl.formatMessage(
|
|
|
|
{
|
|
|
|
defaultMessage: "will be visible from {date}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
date: localizeDate(publicationDate)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
: null
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const visiblilityPickerChoices = [
|
|
|
|
{
|
|
|
|
label: intl.formatMessage({
|
|
|
|
defaultMessage: "Visible"
|
|
|
|
}),
|
|
|
|
secondLabel: visibleSecondLabel,
|
|
|
|
value: "true"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: intl.formatMessage({
|
|
|
|
defaultMessage: "Hidden"
|
|
|
|
}),
|
|
|
|
secondLabel: hiddenSecondLabel,
|
|
|
|
value: "false"
|
|
|
|
}
|
|
|
|
];
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
|
|
|
<Card>
|
2019-08-26 21:54:03 +00:00
|
|
|
<CardTitle
|
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Visibility",
|
|
|
|
description: "section header"
|
|
|
|
})}
|
|
|
|
/>
|
2019-06-19 14:40:52 +00:00
|
|
|
<CardContent>
|
|
|
|
<div
|
|
|
|
className={
|
|
|
|
isPublished
|
|
|
|
? classes.expandedSwitchContainer
|
|
|
|
: classes.switchContainer
|
|
|
|
}
|
|
|
|
>
|
2019-09-09 14:07:09 +00:00
|
|
|
<RadioGroupField
|
|
|
|
choices={visiblilityPickerChoices}
|
2019-06-19 14:40:52 +00:00
|
|
|
disabled={disabled}
|
2019-09-09 14:07:09 +00:00
|
|
|
name={"isPublished" as keyof FormData}
|
|
|
|
value={isPublishedRadio}
|
|
|
|
onChange={onChange}
|
2019-06-19 14:40:52 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2019-09-09 14:07:09 +00:00
|
|
|
{isPublishedRadio === "false" && (
|
2019-06-19 14:40:52 +00:00
|
|
|
<>
|
2019-09-09 14:07:09 +00:00
|
|
|
{!isPublicationDate && (
|
|
|
|
<Typography
|
|
|
|
className={classes.setPublicationDate}
|
|
|
|
onClick={() => setPublicationDate(!isPublicationDate)}
|
|
|
|
>
|
2019-09-11 14:24:24 +00:00
|
|
|
{intl.formatMessage({
|
|
|
|
defaultMessage: "Set publication date",
|
|
|
|
description: "set publication date"
|
|
|
|
})}
|
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
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<FormSpacer />
|
|
|
|
{children}
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
VisibilityCard.displayName = "VisibilityCard";
|
|
|
|
export default VisibilityCard;
|