saleor-dashboard/src/collections/components/CollectionImage/CollectionImage.tsx

137 lines
3.7 KiB
TypeScript
Raw Normal View History

import { Button, Card, CardContent, TextField } from "@material-ui/core";
2019-06-19 14:40:52 +00:00
import CardTitle from "@saleor/components/CardTitle";
import Hr from "@saleor/components/Hr";
import ImageUpload from "@saleor/components/ImageUpload";
import MediaTile from "@saleor/components/MediaTile";
2019-06-19 14:40:52 +00:00
import Skeleton from "@saleor/components/Skeleton";
import { commonMessages } from "@saleor/intl";
Use MacawUI (#1229) * Replace withStyleswith useStyles (#1100) * Replace withStyleswith useStyles * Update messages * Use rem as a spacing unit (#1101) * Use rems as spacing units * Fix visual bugs * Update stories * Use macaw-ui as theme provider (#1108) * Use macaw ui as a theme provider * Add react-dom to aliases * Fix jest module resolution * Update useTheme hook usage * Fix test wrapper * Use macaw from git repo * Fix CI * Update stories * Fix aliasing * Extract savebar to macaw ui (#1146) * wip * Use savebar from macaw * Use confirm button from macaw * Improve file structure * Use sidebar context from macaw * Update macaw * Update macaw version * Remove savebar from storybook * Update stories * Use alerts and notifications from macaw (#1166) * Use alerts from macaw * Add notifications from macaw * Update stories * Pin macaw version * Encapsulate limit reached in one component * Remove unused imports * Use backlinks from macaw (#1183) * Use backlink from macaw * Update macaw version * Use macaw sidebar (#1148) * Use sidebar from macaw * Use shipped logo * Use lowercase * Update stories * Use user chip from macaw (#1191) * Use user chip from macaw * Use dedicated components for menu items * Simplify code * Bump version and fix types (#1210) * Rename onBack to onClick * Rename UserChip to UserChipMenu * Rename IMenuItem to SidebarMenuItem * Update macaw version * Fix tables after changes in macaw (#1220) * Update macaw version * Update changelog * Update stories * Fix after rebase * Update to macaw 0.2.0 * Lint files * Update macaw to 0.2.2
2021-07-21 08:59:52 +00:00
import { makeStyles } from "@saleor/macaw-ui";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import { CollectionDetails_collection_backgroundImage } from "../../types/CollectionDetails";
2019-10-30 14:34:24 +00:00
const useStyles = makeStyles(
theme => ({
2019-06-19 14:40:52 +00:00
PhotosIcon: {
height: "64px",
margin: "0 auto",
width: "64px"
},
PhotosIconContainer: {
2019-10-28 16:16:49 +00:00
margin: theme.spacing(5, 0),
2019-06-19 14:40:52 +00:00
textAlign: "center"
},
fileField: {
display: "none"
},
image: {
height: "100%",
objectFit: "contain",
userSelect: "none",
width: "100%"
},
imageContainer: {
background: "#ffffff",
border: "1px solid #eaeaea",
2019-10-28 16:16:49 +00:00
borderRadius: theme.spacing(),
2019-06-19 14:40:52 +00:00
height: 148,
justifySelf: "start",
overflow: "hidden",
2019-10-28 16:16:49 +00:00
padding: theme.spacing(2),
2019-06-19 14:40:52 +00:00
position: "relative",
width: 148
}
2019-10-30 14:34:24 +00:00
}),
{
name: "CollectionImage"
}
);
2019-06-19 14:40:52 +00:00
export interface CollectionImageProps {
2019-06-19 14:40:52 +00:00
data: {
backgroundImageAlt: string;
};
image: CollectionDetails_collection_backgroundImage;
onChange: (event: React.ChangeEvent<any>) => void;
onImageDelete: () => void;
onImageUpload: (file: File) => void;
}
2019-10-30 14:34:24 +00:00
export const CollectionImage: React.FC<CollectionImageProps> = props => {
const { data, onImageUpload, image, onChange, onImageDelete } = props;
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
const anchor = React.useRef<HTMLInputElement>();
const classes = useStyles(props);
const intl = useIntl();
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
const handleImageUploadButtonClick = () => anchor.current.click();
return (
<Card>
<CardTitle
title={intl.formatMessage({
defaultMessage: "Background Image (optional)",
description: "section header"
})}
toolbar={
<>
<Button
variant="text"
color="primary"
onClick={handleImageUploadButtonClick}
>
<FormattedMessage {...commonMessages.uploadImage} />
</Button>
<input
className={classes.fileField}
id="fileUpload"
onChange={event => onImageUpload(event.target.files[0])}
type="file"
ref={anchor}
2019-11-28 15:19:25 +00:00
accept="image/*"
2019-10-30 14:34:24 +00:00
/>
</>
}
/>
{image === undefined ? (
<CardContent>
<div>
<div className={classes.imageContainer}>
<Skeleton />
</div>
2019-10-30 14:34:24 +00:00
</div>
</CardContent>
) : image === null ? (
2019-11-28 15:19:25 +00:00
<ImageUpload onImageUpload={files => onImageUpload(files[0])} />
2019-10-30 14:34:24 +00:00
) : (
<CardContent>
<MediaTile media={image} onDelete={onImageDelete} />
2019-10-30 14:34:24 +00:00
</CardContent>
)}
{image && (
<>
<Hr />
<CardContent>
2019-10-30 14:34:24 +00:00
<TextField
name="backgroundImageAlt"
label={intl.formatMessage(commonMessages.description)}
helperText={intl.formatMessage({
defaultMessage: "(Optional)",
description: "field is optional"
})}
value={data.backgroundImageAlt}
onChange={onChange}
fullWidth
multiline
/>
</CardContent>
2019-10-30 14:34:24 +00:00
</>
)}
</Card>
);
};
2019-06-19 14:40:52 +00:00
CollectionImage.displayName = "CollectionImage";
export default CollectionImage;