add image dropzone delete button
This commit is contained in:
parent
b6cd284972
commit
05764656d8
@ -22,7 +22,7 @@ import {
|
|||||||
useMediaQuery,
|
useMediaQuery,
|
||||||
useTheme,
|
useTheme,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { incrementCurrentStep, updateQuiz } from "@root/quizes/actions";
|
import { incrementCurrentStep, updateQuiz, uploadQuizImage } from "@root/quizes/actions";
|
||||||
import { useCurrentQuiz } from "@root/quizes/hooks";
|
import { useCurrentQuiz } from "@root/quizes/hooks";
|
||||||
import CustomCheckbox from "@ui_kit/CustomCheckbox";
|
import CustomCheckbox from "@ui_kit/CustomCheckbox";
|
||||||
import CustomTextField from "@ui_kit/CustomTextField";
|
import CustomTextField from "@ui_kit/CustomTextField";
|
||||||
@ -307,8 +307,15 @@ export default function StartPageSettings() {
|
|||||||
heightImg={"110px"}
|
heightImg={"110px"}
|
||||||
sx={{ maxWidth: "300px" }}
|
sx={{ maxWidth: "300px" }}
|
||||||
imageUrl={quiz.config.startpage.background.desktop}
|
imageUrl={quiz.config.startpage.background.desktop}
|
||||||
onImageUpload={(quiz, url) => {
|
onFileChange={file => {
|
||||||
|
uploadQuizImage(quiz.id, file, (quiz, url) => {
|
||||||
quiz.config.startpage.background.desktop = url;
|
quiz.config.startpage.background.desktop = url;
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onDeleteClick={() => {
|
||||||
|
updateQuiz(quiz.id, quiz => {
|
||||||
|
quiz.config.startpage.background.desktop = null;
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
@ -361,8 +368,15 @@ export default function StartPageSettings() {
|
|||||||
text={"5 MB максимум"}
|
text={"5 MB максимум"}
|
||||||
heightImg={"110px"}
|
heightImg={"110px"}
|
||||||
imageUrl={quiz.config.startpage.background.mobile}
|
imageUrl={quiz.config.startpage.background.mobile}
|
||||||
onImageUpload={(quiz, url) => {
|
onFileChange={file => {
|
||||||
|
uploadQuizImage(quiz.id, file, (quiz, url) => {
|
||||||
quiz.config.startpage.background.mobile = url;
|
quiz.config.startpage.background.mobile = url;
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onDeleteClick={() => {
|
||||||
|
updateQuiz(quiz.id, quiz => {
|
||||||
|
quiz.config.startpage.background.mobile = null;
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
@ -450,8 +464,15 @@ export default function StartPageSettings() {
|
|||||||
text={"5 MB максимум"}
|
text={"5 MB максимум"}
|
||||||
heightImg={"110px"}
|
heightImg={"110px"}
|
||||||
imageUrl={quiz.config.startpage.background.mobile}
|
imageUrl={quiz.config.startpage.background.mobile}
|
||||||
onImageUpload={(quiz, url) => {
|
onFileChange={file => {
|
||||||
|
uploadQuizImage(quiz.id, file, (quiz, url) => {
|
||||||
quiz.config.startpage.background.mobile = url;
|
quiz.config.startpage.background.mobile = url;
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onDeleteClick={() => {
|
||||||
|
updateQuiz(quiz.id, quiz => {
|
||||||
|
quiz.config.startpage.background.mobile = null;
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
@ -518,8 +539,15 @@ export default function StartPageSettings() {
|
|||||||
heightImg={"110px"}
|
heightImg={"110px"}
|
||||||
sx={{ maxWidth: "300px" }}
|
sx={{ maxWidth: "300px" }}
|
||||||
imageUrl={quiz.config.logo}
|
imageUrl={quiz.config.logo}
|
||||||
onImageUpload={(quiz, url) => {
|
onFileChange={file => {
|
||||||
|
uploadQuizImage(quiz.id, file, (quiz, url) => {
|
||||||
quiz.config.logo = url;
|
quiz.config.logo = url;
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onDeleteClick={() => {
|
||||||
|
updateQuiz(quiz.id, quiz => {
|
||||||
|
quiz.config.logo = null;
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
@ -592,8 +620,15 @@ export default function StartPageSettings() {
|
|||||||
heightImg={"110px"}
|
heightImg={"110px"}
|
||||||
sx={{ maxWidth: "300px" }}
|
sx={{ maxWidth: "300px" }}
|
||||||
imageUrl={quiz.config.logo}
|
imageUrl={quiz.config.logo}
|
||||||
onImageUpload={(quiz, url) => {
|
onFileChange={file => {
|
||||||
|
uploadQuizImage(quiz.id, file, (quiz, url) => {
|
||||||
quiz.config.logo = url;
|
quiz.config.logo = url;
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
onDeleteClick={() => {
|
||||||
|
updateQuiz(quiz.id, quiz => {
|
||||||
|
quiz.config.logo = null;
|
||||||
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
@ -704,7 +739,7 @@ export default function StartPageSettings() {
|
|||||||
})}
|
})}
|
||||||
/>
|
/>
|
||||||
<CustomCheckbox
|
<CustomCheckbox
|
||||||
sx={{margin: "10px 0"}}
|
sx={{ margin: "10px 0" }}
|
||||||
label="Кликабельный"
|
label="Кликабельный"
|
||||||
checked={quiz.config.info.clickable}
|
checked={quiz.config.info.clickable}
|
||||||
handleChange={e => updateQuiz(quiz.id, quiz => {
|
handleChange={e => updateQuiz(quiz.id, quiz => {
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
import UploadIcon from "@icons/UploadIcon";
|
import UploadIcon from "@icons/UploadIcon";
|
||||||
import { Quiz } from "@model/quiz/quiz";
|
import DeleteIcon from '@mui/icons-material/Delete';
|
||||||
import {
|
import {
|
||||||
Box,
|
Box,
|
||||||
ButtonBase,
|
ButtonBase,
|
||||||
|
IconButton,
|
||||||
SxProps,
|
SxProps,
|
||||||
Theme,
|
Theme,
|
||||||
Typography,
|
Typography,
|
||||||
useTheme,
|
useTheme,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { uploadQuizImage } from "@root/quizes/actions";
|
|
||||||
import { useCurrentQuiz } from "@root/quizes/hooks";
|
import { useCurrentQuiz } from "@root/quizes/hooks";
|
||||||
import { enqueueSnackbar } from "notistack";
|
import { enqueueSnackbar } from "notistack";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
@ -19,12 +19,13 @@ interface Props {
|
|||||||
sx?: SxProps<Theme>;
|
sx?: SxProps<Theme>;
|
||||||
heightImg: string;
|
heightImg: string;
|
||||||
widthImg?: string;
|
widthImg?: string;
|
||||||
onImageUpload: (quiz: Quiz, url: string) => void;
|
onFileChange?: (file: File) => void;
|
||||||
|
onDeleteClick?: () => void;
|
||||||
imageUrl: string | null;
|
imageUrl: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Научи функцию принимать данные для валидации
|
//Научи функцию принимать данные для валидации
|
||||||
export const DropZone = ({ text, sx, heightImg, widthImg, onImageUpload, imageUrl }: Props) => {
|
export const DropZone = ({ text, sx, heightImg, widthImg, onFileChange, onDeleteClick, imageUrl }: Props) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const quiz = useCurrentQuiz();
|
const quiz = useCurrentQuiz();
|
||||||
const [ready, setReady] = useState(false);
|
const [ready, setReady] = useState(false);
|
||||||
@ -39,7 +40,7 @@ export const DropZone = ({ text, sx, heightImg, widthImg, onImageUpload, imageUr
|
|||||||
if (!file) return;
|
if (!file) return;
|
||||||
if (file.size > 5 * 2 ** 20) return enqueueSnackbar("Размер картинки слишком велик");
|
if (file.size > 5 * 2 ** 20) return enqueueSnackbar("Размер картинки слишком велик");
|
||||||
|
|
||||||
uploadQuizImage(quiz.id, file, onImageUpload);
|
onFileChange?.(file);
|
||||||
};
|
};
|
||||||
|
|
||||||
const dragenterHC = () => {
|
const dragenterHC = () => {
|
||||||
@ -57,14 +58,57 @@ export const DropZone = ({ text, sx, heightImg, widthImg, onImageUpload, imageUr
|
|||||||
const file = event.dataTransfer.files[0];
|
const file = event.dataTransfer.files[0];
|
||||||
if (file.size < 5 * 2 ** 20) return enqueueSnackbar("Размер картинки слишком велик");
|
if (file.size < 5 * 2 ** 20) return enqueueSnackbar("Размер картинки слишком велик");
|
||||||
|
|
||||||
uploadQuizImage(quiz.id, file, onImageUpload);
|
onFileChange?.(file);
|
||||||
};
|
};
|
||||||
|
|
||||||
const dragOverHC = (event: React.DragEvent<HTMLDivElement>) => {
|
const dragOverHC = (event: React.DragEvent<HTMLDivElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return imageUrl ? (
|
||||||
|
<Box
|
||||||
|
onDragEnter={dragenterHC}
|
||||||
|
onDragExit={dragexitHC}
|
||||||
|
onDrop={dropHC}
|
||||||
|
onDragOver={dragOverHC}
|
||||||
|
sx={{
|
||||||
|
width: "100%",
|
||||||
|
height: "120px",
|
||||||
|
position: "relative",
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "center",
|
||||||
|
alignItems: "center",
|
||||||
|
backgroundColor: theme.palette.background.default,
|
||||||
|
border: `1px solid ${ready ? "red" : theme.palette.grey2.main}`,
|
||||||
|
borderRadius: "8px",
|
||||||
|
...sx,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
height={heightImg}
|
||||||
|
width={widthImg}
|
||||||
|
src={imageUrl}
|
||||||
|
style={{
|
||||||
|
objectFit: "scale-down",
|
||||||
|
}}
|
||||||
|
alt="img"
|
||||||
|
/>
|
||||||
|
<IconButton
|
||||||
|
onClick={onDeleteClick}
|
||||||
|
sx={{
|
||||||
|
position: "absolute",
|
||||||
|
right: 0,
|
||||||
|
top: 0,
|
||||||
|
color: theme.palette.orange.main,
|
||||||
|
fontSize: "16px",
|
||||||
|
lineHeight: "19px",
|
||||||
|
cursor: "pointer",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<DeleteIcon />
|
||||||
|
</IconButton>
|
||||||
|
</Box>
|
||||||
|
) : (
|
||||||
<ButtonBase component="label" sx={{ justifyContent: "flex-start" }}>
|
<ButtonBase component="label" sx={{ justifyContent: "flex-start" }}>
|
||||||
<input
|
<input
|
||||||
onChange={(event) => imgHC(event.target)}
|
onChange={(event) => imgHC(event.target)}
|
||||||
@ -106,19 +150,6 @@ export const DropZone = ({ text, sx, heightImg, widthImg, onImageUpload, imageUr
|
|||||||
>
|
>
|
||||||
{text}
|
{text}
|
||||||
</Typography>
|
</Typography>
|
||||||
{imageUrl && (
|
|
||||||
<img
|
|
||||||
height={heightImg}
|
|
||||||
width={widthImg}
|
|
||||||
src={imageUrl}
|
|
||||||
style={{
|
|
||||||
position: "absolute",
|
|
||||||
zIndex: "-1",
|
|
||||||
objectFit: "revert-layer",
|
|
||||||
}}
|
|
||||||
alt="img"
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</Box>
|
</Box>
|
||||||
</ButtonBase>
|
</ButtonBase>
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user