Skip to content

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

Add confirm button

dominik-zeglenb6a200688b82
diff --git a/saleor/static/dashboard-next/components/ConfirmButton/ConfirmButton.tsx b/saleor/static/dashboard-next/components/ConfirmButton/ConfirmButton.tsx
new file mode 100644
index 0000000000..57c11a55cb
--- /dev/null
+++ b/saleor/static/dashboard-next/components/ConfirmButton/ConfirmButton.tsx
@@ -0,0 +1,160 @@
+import Button, { ButtonProps } from "@material-ui/core/Button";
+import CircularProgress from "@material-ui/core/CircularProgress";
+import { withStyles, WithStyles } from "@material-ui/core/styles";
+import CheckIcon from "@material-ui/icons/Check";
+import classNames from "classnames";
+import * as React from "react";
+
+import i18n from "../../i18n";
+
+export type ConfirmButtonTransitionState =
+ | "loading"
+ | "success"
+ | "error"
+ | "default";
+
+export interface ConfirmButtonProps extends ButtonProps {
+ children: string;
+ transitionState: ConfirmButtonTransitionState;
+}
+interface ConfirmButtonState {
+ transitionState: ConfirmButtonTransitionState;
+ timeout: any | null;
+}
+
+const decorate = withStyles(theme => ({
+ error: {
+ "&:hover": {
+ backgroundColor: theme.palette.error.main
+ },
+ backgroundColor: theme.palette.error.main,
+ color: theme.palette.error.contrastText
+ },
+ icon: {
+ marginLeft: "0 !important",
+ position: "absolute" as "absolute",
+ transition: theme.transitions.duration.standard + "ms"
+ },
+ invisible: {
+ opacity: 0
+ },
+ label: {
+ transition: theme.transitions.duration.standard + "ms"
+ },
+ progress: {
+ "& svg": {
+ color: theme.palette.common.white,
+ margin: 0
+ },
+ position: "absolute" as "absolute",
+ transition: theme.transitions.duration.standard + "ms"
+ },
+ success: {
+ "&:hover": {
+ backgroundColor: theme.palette.primary.main
+ },
+ backgroundColor: theme.palette.primary.main,
+ color: theme.palette.primary.contrastText
+ }
+}));
+const ConfirmButton = decorate<ConfirmButtonProps>(
+ class ConfirmButtonComponent extends React.Component<
+ ConfirmButtonProps &
+ WithStyles<
+ "error" | "icon" | "invisible" | "label" | "progress" | "success"
+ >,
+ ConfirmButtonState
+ > {
+ state: ConfirmButtonState = {
+ timeout: null,
+ transitionState: "default"
+ };
+
+ componentDidUpdate(prevProps: ConfirmButtonProps) {
+ const { transitionState } = this.props;
+ if (prevProps.transitionState !== transitionState) {
+ if (["default", "loading"].includes(transitionState)) {
+ if (transitionState === "loading") {
+ clearTimeout(this.state.timeout);
+ }
+ this.setState({
+ timeout: null,
+ transitionState
+ });
+ } else {
+ const setStateToDefault = setTimeout(
+ () =>
+ this.setState({
+ timeout: null,
+ transitionState: "default"
+ }),
+ 2000
+ );
+ this.setState({
+ timeout: setStateToDefault,
+ transitionState
+ });
+ }
+ }
+ }
+
+ render() {
+ const {
+ children,
+ classes,
+ className,
+ transitionState: transitionStateProp,
+ onClick,
+ ...props
+ } = this.props;
+ const { transitionState } = this.state;
+
+ return (
+ <Button
+ variant="contained"
+ onClick={transitionState === "loading" ? undefined : onClick}
+ color="secondary"
+ className={classNames({
+ [classes.error]: transitionState === "error",
+ [classes.success]: transitionState === "success",
+ [className]: true
+ })}
+ {...props}
+ >
+ <CircularProgress
+ size={24}
+ color="inherit"
+ className={classNames({
+ [classes.progress]: true,
+ [classes.invisible]: transitionState !== "loading"
+ })}
+ />
+ <CheckIcon
+ className={classNames({
+ [classes.icon]: true,
+ [classes.invisible]: transitionState !== "success"
+ })}
+ />
+ <span
+ className={classNames({
+ [classes.label]: true,
+ [classes.invisible]:
+ transitionState === "loading" || transitionState === "success"
+ })}
+ >
+ {transitionState === "error"
+ ? i18n.t("Error", {
+ context: "button"
+ })
+ : children ||
+ i18n.t("Confirm", {
+ context: "button"
+ })}
+ </span>
+ </Button>
+ );
+ }
+ }
+);
+ConfirmButton.displayName = "ConfirmButton";
+export default ConfirmButton;
diff --git a/saleor/static/dashboard-next/components/ConfirmButton/index.ts b/saleor/static/dashboard-next/components/ConfirmButton/index.ts
new file mode 100644
index 0000000000..4fe09023d2
--- /dev/null
+++ b/saleor/static/dashboard-next/components/ConfirmButton/index.ts
@@ -0,0 +1,2 @@
+export { default } from "./ConfirmButton";
+export * from "./ConfirmButton";
diff --git a/saleor/static/dashboard-next/components/SaveButtonBar/SaveButtonBar.tsx b/saleor/static/dashboard-next/components/SaveButtonBar/SaveButtonBar.tsx
index 2c62fbb99c..371ba5a9d3 100644
--- a/saleor/static/dashboard-next/components/SaveButtonBar/SaveButtonBar.tsx
+++ b/saleor/static/dashboard-next/components/SaveButtonBar/SaveButtonBar.tsx
@@ -1,22 +1,17 @@
import Button from "@material-ui/core/Button";
-import CircularProgress from "@material-ui/core/CircularProgress";
import gray from "@material-ui/core/colors/grey";
import { withStyles } from "@material-ui/core/styles";
-import CheckIcon from "@material-ui/icons/Check";
-import classNames from "classnames";
import * as React from "react";
import i18n from "../../i18n";
+import { maybe } from "../../misc";
+import ConfirmButton, {
+ ConfirmButtonTransitionState
+} from "../ConfirmButton/ConfirmButton";
-export type SaveButtonBarState =
- | "loading"
- | "success"
- | "error"
- | "default"
- | string;
interface SaveButtonBarProps {
- disabled?: boolean;
- state?: SaveButtonBarState;
+ disabled: boolean;
+ state: ConfirmButtonTransitionState;
labels?: {
cancel?: string;
delete?: string;
@@ -31,15 +26,6 @@ const decorate = withStyles(theme => ({
button: {
marginRight: theme.spacing.unit
},
- buttonProgress: {
- "& svg": {
- color: theme.palette.common.white,
- margin: 0
- },
- opacity: 0,
- position: "absolute" as "absolute",
- transition: theme.transitions.duration.standard + "ms"
- },
cancelButton: {
marginRight: theme.spacing.unit * 2
},
@@ -50,25 +36,6 @@ const decorate = withStyles(theme => ({
backgroundColor: theme.palette.error.main,
color: theme.palette.error.contrastText
},
- error: {
- "&:hover": {
- backgroundColor: theme.palette.error.main
- },
- backgroundColor: theme.palette.error.main,
- color: theme.palette.error.contrastText
- },
- icon: {
- marginLeft: "0 !important",
- opacity: 0,
- position: "absolute" as "absolute",
- transition: theme.transitions.duration.standard + "ms"
- },
- label: {
- transition: theme.transitions.duration.standard + "ms"
- },
- labelInvisible: {
- opacity: 0
- },
root: {
borderTop: `1px ${gray[300]} solid`,
display: "flex",
@@ -81,16 +48,6 @@ const decorate = withStyles(theme => ({
},
spacer: {
flex: "1"
- },
- success: {
- "&:hover": {
- backgroundColor: theme.palette.primary.main
- },
- backgroundColor: theme.palette.primary.main,
- color: theme.palette.primary.contrastText
- },
- visible: {
- opacity: 1
}
}));
export const SaveButtonBar = decorate<SaveButtonBarProps>(
@@ -104,26 +61,6 @@ export const SaveButtonBar = decorate<SaveButtonBarProps>(
onSave,
...props
}) => {
- let saveButtonClassName;
- let buttonLabel;
- switch (state) {
- case "success":
- saveButtonClassName = classes.success;
- buttonLabel = i18n.t("Save");
- break;
- case "error":
- saveButtonClassName = classes.error;
- buttonLabel = i18n.t("Error");
- break;
- case "loading":
- saveButtonClassName = "";
- buttonLabel = i18n.t("Save");
- break;
- default:
- saveButtonClassName = "";
- buttonLabel = labels && labels.save ? labels.save : i18n.t("Save");
- break;
- }
return (
<div className={classes.root} {...props}>
{!!onDelete && (
@@ -143,37 +80,18 @@ export const SaveButtonBar = decorate<SaveButtonBarProps>(
>
{labels && labels.cancel ? labels.cancel : i18n.t("Cancel")}
</Button>
- <Button
- variant="contained"
- onClick={state === "loading" ? undefined : onSave}
- color="secondary"
+ <ConfirmButton
disabled={disabled}
- className={saveButtonClassName}
+ onClick={onSave}
+ transitionState={state}
>
- <CircularProgress
- size={24}
- color="inherit"
- className={classNames({
- [classes.buttonProgress]: true,
- [classes.visible]: state === "loading"
- })}
- />
- <CheckIcon
- className={classNames({
- [classes.icon]: true,
- [classes.visible]: state === "success"
- })}
- />
- <span
- className={classNames({
- [classes.label]: true,
- [classes.labelInvisible]:
- state === "loading" || state === "success"
- })}
- >
- {buttonLabel}
- </span>
- </Button>
+ {maybe(
+ () => labels.save,
+ i18n.t("Save", {
+ context: "button"
+ })
+ )}
+ </ConfirmButton>
</div>
);
}