Fix visibility card component

This commit is contained in:
Krzysztof Bialoglowicz 2019-09-16 03:00:38 +02:00
parent b6886d8fc8
commit 6d0aacd7ce
10 changed files with 144 additions and 71 deletions

View file

@ -63,11 +63,7 @@ export const CollectionCreate: React.FC = () => {
backgroundImage: formData.backgroundImage.value, backgroundImage: formData.backgroundImage.value,
backgroundImageAlt: formData.backgroundImageAlt, backgroundImageAlt: formData.backgroundImageAlt,
descriptionJson: JSON.stringify(formData.description), descriptionJson: JSON.stringify(formData.description),
isPublished: isPublished: formData.isPublished,
typeof formData.isPublished !== "boolean" &&
formData.isPublished === "true"
? true
: false,
name: formData.name, name: formData.name,
seo: { seo: {
description: formData.seoDescription, description: formData.seoDescription,

View file

@ -148,11 +148,7 @@ export const CollectionDetails: React.StatelessComponent<
const input: CollectionInput = { const input: CollectionInput = {
backgroundImageAlt: formData.backgroundImageAlt, backgroundImageAlt: formData.backgroundImageAlt,
descriptionJson: JSON.stringify(formData.description), descriptionJson: JSON.stringify(formData.description),
isPublished: isPublished: formData.isPublished,
typeof formData.isPublished !== "boolean" &&
formData.isPublished === "true"
? true
: false,
name: formData.name, name: formData.name,
publicationDate: formData.publicationDate, publicationDate: formData.publicationDate,
seo: { seo: {

View file

@ -30,11 +30,10 @@ const styles = createStyles({
} }
}); });
interface RadioGroupFieldProps extends WithStyles<typeof styles> { interface RadioGroupFieldProps {
choices: Array<{ choices: Array<{
value: string; value: string;
label: string | React.ReactNode; label: React.ReactNode;
secondLabel?: string | React.ReactNode;
}>; }>;
className?: string; className?: string;
disabled?: boolean; disabled?: boolean;
@ -60,7 +59,7 @@ export const RadioGroupField = withStyles(styles, {
onChange, onChange,
name, name,
hint hint
}: RadioGroupFieldProps) => { }: RadioGroupFieldProps & WithStyles<typeof styles>) => {
return ( return (
<FormControl <FormControl
className={classNames(classes.formControl, className)} className={classNames(classes.formControl, className)}
@ -82,14 +81,7 @@ export const RadioGroupField = withStyles(styles, {
value={choice.value} value={choice.value}
className={classes.radioLabel} className={classes.radioLabel}
control={<Radio color="primary" />} control={<Radio color="primary" />}
label={ label={choice.label}
<>
{choice.label}
<span className={classes.secondLabel}>
{choice.secondLabel}
</span>
</>
}
key={choice.value} key={choice.value}
/> />
)) ))

View file

@ -0,0 +1,97 @@
import FormControl from "@material-ui/core/FormControl";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Radio from "@material-ui/core/Radio";
import RadioGroup from "@material-ui/core/RadioGroup";
import { createStyles, withStyles, WithStyles } from "@material-ui/core/styles";
import classNames from "classnames";
import React from "react";
const styles = createStyles({
formControl: {
padding: 0,
width: "100%"
},
formLabel: {
marginLeft: "-5px",
paddingBottom: "10px"
},
radioLabel: {
"& > span": {
padding: "10px 6px"
}
},
secondLabel: {
display: "block",
fontSize: "12px"
}
});
interface RadioSwitchFieldProps {
className?: string;
disabled?: boolean;
error?: boolean;
firstOptionLabel: React.ReactNode;
name?: string;
secondOptionLabel: React.ReactNode;
value?: boolean;
onChange: (event: React.ChangeEvent<any>) => void;
}
export const RadioSwitchField = withStyles(styles, {
name: "RadioSwitchField"
})(
({
className,
classes,
disabled,
error,
firstOptionLabel,
onChange,
name,
secondOptionLabel,
value
}: RadioSwitchFieldProps & WithStyles<typeof styles>) => {
const initialValue = value ? "visible" : "hidden";
const change = event => {
onChange({
target: {
name: event.target.name,
value: event.target.value === "visible" ? true : false
}
} as any);
};
return (
<FormControl
className={classNames(classes.formControl, className)}
error={error}
disabled={disabled}
>
<RadioGroup
aria-label={name}
name={name}
value={initialValue}
onChange={event => change(event)}
>
<FormControlLabel
value="visible"
className={classes.radioLabel}
control={<Radio color="primary" />}
label={firstOptionLabel}
name={name}
/>
<FormControlLabel
value="hidden"
className={classes.radioLabel}
control={<Radio color="primary" />}
label={secondOptionLabel}
name={name}
/>
</RadioGroup>
</FormControl>
);
}
);
RadioSwitchField.displayName = "RadioSwitchField";
export default RadioSwitchField;

View file

@ -0,0 +1,2 @@
export { default } from "./RadioSwitchField";
export * from "./RadioSwitchField";

View file

@ -12,8 +12,7 @@ import React from "react";
import { useIntl } from "react-intl"; import { useIntl } from "react-intl";
import CardTitle from "@saleor/components/CardTitle"; import CardTitle from "@saleor/components/CardTitle";
import { FormSpacer } from "@saleor/components/FormSpacer"; import RadioSwitchField from "@saleor/components/RadioSwitchField";
import RadioGroupField from "@saleor/components/RadioGroupField";
import useDateLocalize from "@saleor/hooks/useDateLocalize"; import useDateLocalize from "@saleor/hooks/useDateLocalize";
import { DateContext } from "../Date/DateContext"; import { DateContext } from "../Date/DateContext";
@ -25,6 +24,13 @@ const styles = (theme: Theme) =>
}, },
marginTop: theme.spacing.unit * 3 marginTop: theme.spacing.unit * 3
}, },
label: {
lineHeight: 1,
margin: 0
},
secondLabel: {
fontSize: 12
},
setPublicationDate: { setPublicationDate: {
color: theme.palette.primary.main, color: theme.palette.primary.main,
cursor: "pointer", cursor: "pointer",
@ -42,7 +48,7 @@ interface VisibilityCardProps extends WithStyles<typeof styles> {
}; };
errors: { [key: string]: string }; errors: { [key: string]: string };
disabled?: boolean; disabled?: boolean;
onChange(event: any); onChange: (event: React.ChangeEvent<any>) => void;
} }
export const VisibilityCard = withStyles(styles, { export const VisibilityCard = withStyles(styles, {
@ -62,12 +68,6 @@ export const VisibilityCard = withStyles(styles, {
); );
const localizeDate = useDateLocalize(); const localizeDate = useDateLocalize();
const dateNow = React.useContext(DateContext); const dateNow = React.useContext(DateContext);
const isPublishedRadio =
typeof isPublished !== "boolean"
? isPublished
: isPublished
? "true"
: "false";
const visibleSecondLabel = publicationDate const visibleSecondLabel = publicationDate
? isPublished ? isPublished
? intl.formatMessage( ? intl.formatMessage(
@ -95,22 +95,6 @@ export const VisibilityCard = withStyles(styles, {
: null : null
: null; : null;
const visiblilityPickerChoices = [
{
label: intl.formatMessage({
defaultMessage: "Visible"
}),
secondLabel: visibleSecondLabel,
value: "true"
},
{
label: intl.formatMessage({
defaultMessage: "Hidden"
}),
secondLabel: hiddenSecondLabel,
value: "false"
}
];
return ( return (
<Card> <Card>
<CardTitle <CardTitle
@ -120,16 +104,37 @@ export const VisibilityCard = withStyles(styles, {
})} })}
/> />
<CardContent> <CardContent>
<RadioGroupField <RadioSwitchField
choices={visiblilityPickerChoices}
disabled={disabled} disabled={disabled}
firstOptionLabel={
<>
<p className={classes.label}>
{intl.formatMessage({
defaultMessage: "Visible"
})}
</p>
<span className={classes.secondLabel}>
{visibleSecondLabel}
</span>
</>
}
name={"isPublished" as keyof FormData} name={"isPublished" as keyof FormData}
value={isPublishedRadio} secondOptionLabel={
<>
<p className={classes.label}>
{intl.formatMessage({
defaultMessage: "Hidden"
})}
</p>
<span className={classes.secondLabel}>{hiddenSecondLabel}</span>
</>
}
value={isPublished}
onChange={onChange} onChange={onChange}
/> />
{isPublishedRadio === "false" && ( {!isPublished && (
<> <>
{!isPublicationDate && ( {!isPublished && (
<Typography <Typography
className={classes.setPublicationDate} className={classes.setPublicationDate}
onClick={() => setPublicationDate(!isPublicationDate)} onClick={() => setPublicationDate(!isPublicationDate)}
@ -161,7 +166,6 @@ export const VisibilityCard = withStyles(styles, {
)} )}
</> </>
)} )}
<FormSpacer />
{children} {children}
</CardContent> </CardContent>
</Card> </Card>

View file

@ -59,11 +59,7 @@ export const PageCreate: React.StatelessComponent<PageCreateProps> = () => {
variables: { variables: {
input: { input: {
contentJson: JSON.stringify(formData.content), contentJson: JSON.stringify(formData.content),
isPublished: isPublished: formData.isPublished,
typeof formData.isPublished !== "boolean" &&
formData.isPublished === "true"
? true
: false,
publicationDate: formData.isPublished publicationDate: formData.isPublished
? null ? null
: formData.publicationDate === "" : formData.publicationDate === ""

View file

@ -22,10 +22,7 @@ export interface PageDetailsProps {
const createPageInput = (data: FormData): PageInput => { const createPageInput = (data: FormData): PageInput => {
return { return {
contentJson: JSON.stringify(data.content), contentJson: JSON.stringify(data.content),
isPublished: isPublished: data.isPublished,
typeof data.isPublished !== "boolean" && data.isPublished === "true"
? true
: false,
publicationDate: data.isPublished publicationDate: data.isPublished
? null ? null
: data.publicationDate === "" : data.publicationDate === ""

View file

@ -83,11 +83,7 @@ export const ProductUpdate: React.StatelessComponent<
descriptionJson: JSON.stringify( descriptionJson: JSON.stringify(
formData.description formData.description
), ),
isPublished: isPublished: formData.isPublished,
typeof formData.isPublished !== "boolean" &&
formData.isPublished === "true"
? true
: false,
name: formData.name, name: formData.name,
productType: formData.productType, productType: formData.productType,
publicationDate: publicationDate:

View file

@ -25,10 +25,7 @@ export function createUpdateHandler(
collections: data.collections, collections: data.collections,
descriptionJson: JSON.stringify(data.description), descriptionJson: JSON.stringify(data.description),
id: product.id, id: product.id,
isPublished: isPublished: data.isPublished,
typeof data.isPublished !== "boolean" && data.isPublished === "true"
? true
: false,
name: data.name, name: data.name,
publicationDate: publicationDate:
data.publicationDate !== "" ? data.publicationDate : null, data.publicationDate !== "" ? data.publicationDate : null,