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

107 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-11-28 15:17:39 +00:00
import CircularProgress from "@material-ui/core/CircularProgress";
2019-06-19 14:40:52 +00:00
import IconButton from "@material-ui/core/IconButton";
import DeleteIcon from "@material-ui/icons/Delete";
import EditIcon from "@material-ui/icons/Edit";
import { makeStyles } from "@saleor/theme";
2019-11-28 15:17:39 +00:00
import classNames from "classnames";
import React from "react";
2019-06-19 14:40:52 +00:00
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => ({
media: {
2019-12-03 15:28:40 +00:00
height: "100%",
objectFit: "contain",
userSelect: "none",
width: "100%"
},
mediaContainer: {
2019-12-03 15:28:40 +00:00
"&:hover, &.dragged": {
"& $mediaOverlay": {
2019-12-03 15:28:40 +00:00
display: "block"
}
},
background: theme.palette.background.paper,
border: `1px solid ${theme.palette.divider}`,
borderRadius: theme.spacing(),
height: 148,
overflow: "hidden",
padding: theme.spacing(2),
position: "relative",
width: 148
},
mediaOverlay: {
2019-12-03 15:28:40 +00:00
background: "rgba(0, 0, 0, 0.6)",
cursor: "move",
display: "none",
height: 148,
left: 0,
position: "absolute",
top: 0,
width: 148
},
mediaOverlayShadow: {
"&mediaOverlay": {
2019-12-03 15:28:40 +00:00
alignItems: "center",
display: "flex",
justifyContent: "center"
2019-10-30 14:34:24 +00:00
}
2019-06-19 14:40:52 +00:00
},
mediaOverlayToolbar: {
2019-11-28 15:17:39 +00:00
display: "flex",
2019-12-03 15:28:40 +00:00
justifyContent: "flex-end"
2019-11-28 15:17:39 +00:00
}
2019-12-03 15:28:40 +00:00
}),
{ name: "MediaTile" }
2019-12-03 15:28:40 +00:00
);
2019-06-19 14:40:52 +00:00
interface MediaTileProps {
media: {
alt: string;
2019-06-19 14:40:52 +00:00
url: string;
type?: string;
oembedData?: string;
2019-06-19 14:40:52 +00:00
};
2019-11-28 15:17:39 +00:00
loading?: boolean;
onDelete?: () => void;
onEdit?: (event: React.ChangeEvent<any>) => void;
2019-06-19 14:40:52 +00:00
}
const MediaTile: React.FC<MediaTileProps> = props => {
const { loading, onDelete, onEdit, media } = props;
2019-10-30 14:34:24 +00:00
const classes = useStyles(props);
const parsedMediaOembedData = media?.oembedData
? JSON.parse(media.oembedData)
: null;
const mediaUrl = parsedMediaOembedData?.thumbnail_url || media.url;
2019-10-30 14:34:24 +00:00
return (
<div className={classes.mediaContainer} data-test="product-image">
2019-11-28 15:17:39 +00:00
<div
className={classNames(classes.mediaOverlay, {
[classes.mediaOverlayShadow]: loading
2019-11-28 15:17:39 +00:00
})}
>
{loading ? (
<CircularProgress size={32} />
) : (
<div className={classes.mediaOverlayToolbar}>
{onEdit && (
<IconButton color="primary" onClick={onEdit}>
2019-11-28 15:17:39 +00:00
<EditIcon />
</IconButton>
)}
{onDelete && (
<IconButton color="primary" onClick={onDelete}>
2019-11-28 15:17:39 +00:00
<DeleteIcon />
</IconButton>
)}
</div>
)}
2019-06-19 14:40:52 +00:00
</div>
<img className={classes.media} src={mediaUrl} alt={media.alt} />
2019-06-19 14:40:52 +00:00
</div>
2019-10-30 14:34:24 +00:00
);
};
MediaTile.displayName = "MediaTile";
export default MediaTile;