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

142 lines
3.8 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Button from "@material-ui/core/Button";
2020-09-03 17:01:32 +00:00
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
2019-06-19 14:40:52 +00:00
import Portal from "@material-ui/core/Portal";
import useWindowScroll from "@saleor/hooks/useWindowScroll";
import { buttonMessages } from "@saleor/intl";
import { makeStyles } from "@saleor/theme";
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
2020-10-14 08:19:30 +00:00
import { useAppAction } from "../AppLayout/AppActionContext";
2019-06-19 14:40:52 +00:00
import ConfirmButton, {
ConfirmButtonTransitionState
} from "../ConfirmButton/ConfirmButton";
import Container from "../Container";
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
button: {
marginRight: theme.spacing(1)
},
cancelButton: {
marginRight: theme.spacing(2)
},
2020-09-03 17:01:32 +00:00
content: {
"&:last-child": {
paddingBottom: theme.spacing(2)
},
2019-12-03 15:28:40 +00:00
display: "flex",
paddingBottom: theme.spacing(2),
paddingTop: theme.spacing(2),
[theme.breakpoints.down("sm")]: {
marginTop: theme.spacing(1)
}
},
deleteButton: {
"&:hover": {
backgroundColor: theme.palette.error.dark
},
backgroundColor: theme.palette.error.main,
color: theme.palette.error.contrastText
},
2020-10-14 08:17:23 +00:00
paper: {
borderBottomLeftRadius: 0,
borderBottomRightRadius: 0
},
2019-12-03 15:28:40 +00:00
root: {
2020-10-14 08:17:23 +00:00
height: 70
2019-12-03 15:28:40 +00:00
},
spacer: {
flex: "1"
2019-06-19 14:40:52 +00:00
}
2019-12-03 15:28:40 +00:00
}),
{ name: "SaveButtonBar" }
);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
interface SaveButtonBarProps {
2019-06-19 14:40:52 +00:00
disabled: boolean;
state: ConfirmButtonTransitionState;
labels?: {
cancel?: string;
delete?: string;
save?: string;
};
onCancel: () => void;
onDelete?: () => void;
onSave(event: any);
}
2019-10-30 14:34:24 +00:00
export const SaveButtonBar: React.FC<SaveButtonBarProps> = props => {
const {
2019-06-19 14:40:52 +00:00
disabled,
labels,
state,
onCancel,
onDelete,
onSave,
2019-10-30 14:34:24 +00:00
...rest
} = props;
const classes = useStyles(props);
2019-06-19 14:40:52 +00:00
2020-10-14 08:17:23 +00:00
const appAction = useAppAction();
2019-10-30 14:34:24 +00:00
const intl = useIntl();
const scrollPosition = useWindowScroll();
2020-10-14 08:17:23 +00:00
React.useEffect(() => {
2020-10-15 08:00:31 +00:00
if (!disabled && state !== "loading") {
appAction.setDocked(false);
}
2020-10-14 08:17:23 +00:00
}, [disabled]);
2020-10-15 08:00:31 +00:00
React.useEffect(() => () => appAction.setDocked(true), []);
2020-10-14 08:17:23 +00:00
2019-10-30 14:34:24 +00:00
const scrolledToBottom =
scrollPosition.y + window.innerHeight >= document.body.scrollHeight;
2019-10-30 14:34:24 +00:00
2020-10-14 08:17:23 +00:00
return appAction.anchor ? (
<Portal container={appAction.anchor.current}>
<div className={classes.root} {...rest}>
<Container>
<Card
className={classes.paper}
elevation={!(appAction.docked || scrolledToBottom) ? 16 : 0}
>
<CardContent className={classes.content}>
{!!onDelete && (
<Button
variant="contained"
onClick={onDelete}
className={classes.deleteButton}
data-test="button-bar-delete"
>
2020-10-15 08:00:31 +00:00
{labels?.delete || intl.formatMessage(buttonMessages.delete)}
2020-10-14 08:17:23 +00:00
</Button>
)}
<div className={classes.spacer} />
<Button
className={classes.cancelButton}
variant="text"
onClick={onCancel}
data-test="button-bar-cancel"
>
2020-10-15 08:00:31 +00:00
{labels?.cancel || intl.formatMessage(buttonMessages.back)}
2020-10-14 08:17:23 +00:00
</Button>
<ConfirmButton
disabled={disabled}
onClick={onSave}
transitionState={state}
data-test="button-bar-confirm"
2020-10-15 08:00:31 +00:00
onTransitionToDefault={() => appAction.setDocked(true)}
2020-10-14 08:17:23 +00:00
>
2020-10-15 08:00:31 +00:00
{labels?.save || intl.formatMessage(buttonMessages.save)}
2020-10-14 08:17:23 +00:00
</ConfirmButton>
</CardContent>
</Card>
</Container>
</div>
</Portal>
) : null;
2019-10-30 14:34:24 +00:00
};
2019-06-19 14:40:52 +00:00
SaveButtonBar.displayName = "SaveButtonBar";
export default SaveButtonBar;