Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add VoucherDates component

Krzysztof Bialoglowiczdb9fe8713ddf
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherDates/VoucherDates.tsx b/saleor/static/dashboard-next/discounts/components/VoucherDates/VoucherDates.tsx
new file mode 100644
index 0000000000..73c8a7c7ad
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/components/VoucherDates/VoucherDates.tsx
@@ -0,0 +1,123 @@
+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";
+import * as moment from "moment-timezone";
+import * as React from "react";
+
+import CardTitle from "@saleor/components/CardTitle";
+import { ControlledCheckbox } from "@saleor/components/ControlledCheckbox";
+import i18n from "../../../i18n";
+import { FormErrors } from "../../../types";
+import { FormData } from "../VoucherDetailsPage";
+
+interface VoucherDatesProps {
+ data: FormData;
+ defaultCurrency: string;
+ disabled: boolean;
+ errors: FormErrors<
+ | "discountType"
+ | "discountValue"
+ | "endDate"
+ | "minAmountSpent"
+ | "startDate"
+ | "usageLimit"
+ >;
+ onChange: (event: React.ChangeEvent<any>) => void;
+}
+
+const styles = (theme: Theme) =>
+ createStyles({
+ root: {
+ display: "grid",
+ gridColumnGap: theme.spacing.unit * 2 + "px",
+ gridTemplateColumns: "1fr 1fr"
+ }
+ });
+
+const VoucherDates = withStyles(styles, {
+ name: "VoucherDates"
+})(
+ ({
+ classes,
+ data,
+ disabled,
+ errors,
+ onChange
+ }: VoucherDatesProps & WithStyles<typeof styles>) => {
+ const [showEndDate, setshowEndDate] = React.useState(false);
+ const handleOnChange = () => setshowEndDate(!showEndDate);
+
+ console.log(data);
+
+ return (
+ <Card>
+ <CardTitle title={i18n.t("Active Dates")} />
+ <CardContent>
+ <div className={classes.root}>
+ <TextField
+ disabled={disabled}
+ error={!!errors.startDate}
+ helperText={errors.startDate}
+ name={"startDate" as keyof FormData}
+ onChange={onChange}
+ label={i18n.t("Start Date")}
+ value={data.startDate}
+ type="date"
+ fullWidth
+ />
+ <TextField
+ disabled={disabled}
+ error={!!errors.startDate}
+ helperText={errors.startDate}
+ name={"startTime" as keyof FormData}
+ onChange={onChange}
+ label={i18n.t("Start Hour")}
+ value={data.startTime}
+ type="time"
+ fullWidth
+ />
+ </div>
+ <ControlledCheckbox
+ checked={showEndDate}
+ label={i18n.t("Set end date")}
+ name="setEndDate"
+ onChange={handleOnChange}
+ />
+ {showEndDate ? (
+ <div className={classes.root}>
+ <TextField
+ disabled={disabled}
+ error={!!errors.endDate}
+ helperText={errors.endDate}
+ name={"endDate" as keyof FormData}
+ onChange={onChange}
+ label={i18n.t("End Date")}
+ value={data.endDate}
+ type="date"
+ fullWidth
+ />
+ <TextField
+ disabled={disabled}
+ error={!!errors.endDate}
+ helperText={errors.endDate}
+ name={"endTime" as keyof FormData}
+ onChange={onChange}
+ label={i18n.t("End Hour")}
+ value={data.endTime}
+ type="time"
+ fullWidth
+ />
+ </div>
+ ) : null}
+ </CardContent>
+ </Card>
+ );
+ }
+);
+export default VoucherDates;
diff --git a/saleor/static/dashboard-next/discounts/components/VoucherDates/index.ts b/saleor/static/dashboard-next/discounts/components/VoucherDates/index.ts
new file mode 100644
index 0000000000..da24480b60
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/components/VoucherDates/index.ts
@@ -0,0 +1,2 @@
+export { default } from "./VoucherDates";
+export * from "./VoucherDates";
diff --git a/saleor/static/dashboard-next/misc.ts b/saleor/static/dashboard-next/misc.ts
index 83f9e9aecb..f997adc60f 100644
--- a/saleor/static/dashboard-next/misc.ts
+++ b/saleor/static/dashboard-next/misc.ts
@@ -1,5 +1,5 @@
+import moment from "moment-timezone";
import urlJoin from "url-join";
-
import { MutationFn, MutationResult } from "react-apollo";
import { ConfirmButtonTransitionState } from "./components/ConfirmButton/ConfirmButton";
import { APP_MOUNT_URI } from "./config";
@@ -236,3 +236,25 @@ export function stopPropagation(cb: () => void) {
cb();
};
}
+
+export function connectDateTime(date: string, time?: string) {
+ if (!date) return null;
+ const setTime = time || "00:00";
+ const dateTime = moment(date + " " + setTime).format();
+ return dateTime;
+}
+
+export function splitDateTime(dateTime: string) {
+ if (!dateTime)
+ return {
+ date: "",
+ time: ""
+ };
+ const splitDateTime = moment(dateTime)
+ .format("YYYY-MM-DD HH:mm")
+ .split(" ");
+ return {
+ date: splitDateTime[0],
+ time: splitDateTime[1]
+ };
+}