Skip to content

Commit 7b49e3f

Browse files
authored
Merge pull request #1215 from lvdwijngaart/edit-exercise-image
Improve image edit form #1209
2 parents 2a518eb + 10daf4d commit 7b49e3f

12 files changed

Lines changed: 538 additions & 141 deletions

File tree

src/components/Exercises/Add/Step5Images.tsx

Lines changed: 19 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,38 @@
11
import CameraAltIcon from '@mui/icons-material/CameraAlt';
22
import CollectionsIcon from '@mui/icons-material/Collections';
33
import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';
4-
import InfoIcon from '@mui/icons-material/Info';
54
import {
6-
Alert,
75
Box,
86
Button,
97
IconButton,
108
ImageListItem,
119
ImageListItemBar,
12-
Modal,
1310
Stack,
1411
Typography
1512
} from "@mui/material";
1613
import Grid from '@mui/material/Grid';
1714
import ImageList from '@mui/material/ImageList';
18-
import { LicenseAuthor } from "components/Common/forms/LicenseAuthor";
19-
import { LicenseAuthorUrl } from "components/Common/forms/LicenseAuthorUrl";
20-
import { LicenseDerivativeSourceUrl } from "components/Common/forms/LicenseDerivativeSourceUrl";
21-
import { LicenseObjectUrl } from "components/Common/forms/LicenseObjectUrl";
22-
import { LicenseTitle } from "components/Common/forms/LicenseTitle";
2315
import { StepProps } from "components/Exercises/Add/AddExerciseStepper";
24-
import { ImageStyleToggle } from "components/Exercises/forms/ImageStyle";
16+
import { ImageFormModal } from "components/Exercises/forms/ImageModal";
2517
import { ImageFormData } from "components/Exercises/models/exercise";
2618
import { ImageStyle } from "components/Exercises/models/image";
27-
import { useProfileQuery } from "components/User/queries/profile";
28-
import { Form, Formik } from "formik";
2919
import React, { useEffect, useState } from "react";
3020
import { useTranslation } from "react-i18next";
3121
import { useExerciseSubmissionStateValue } from "state";
3222
import { setImages } from "state/exerciseSubmissionReducer";
3323

3424
export const Step5Images = ({ onContinue, onBack }: StepProps) => {
3525
const [t] = useTranslation();
36-
const profileQuery = useProfileQuery();
3726

3827
const [state, dispatch] = useExerciseSubmissionStateValue();
3928
const [localImages, setLocalImages] = useState<ImageFormData[]>(state.images);
40-
const [popupImage, setPopupImage] = useState<ImageFormData | undefined>(undefined);
29+
const [selectedImage, setSelectedImage] = useState<ImageFormData | null>(null);
4130

42-
const [openModal, setOpenModal] = React.useState(false);
43-
const handleCloseModal = () => setOpenModal(false);
31+
const [openModal, setOpenModal] = useState(false);
32+
const handleCloseModal = () => {
33+
setOpenModal(false);
34+
setSelectedImage(null);
35+
};
4436

4537
useEffect(() => {
4638
dispatch(setImages(localImages));
@@ -50,43 +42,25 @@ export const Step5Images = ({ onContinue, onBack }: StepProps) => {
5042
if (!e.target.files?.length) {
5143
return;
5244
}
45+
5346
const [uploadedFile] = e.target.files;
5447
const objectURL = URL.createObjectURL(uploadedFile);
5548

56-
setOpenModal(true);
57-
58-
setPopupImage({
49+
setSelectedImage({
5950
url: objectURL,
6051
file: uploadedFile,
61-
6252
author: "",
6353
authorUrl: "",
6454
title: "",
6555
derivativeSourceUrl: "",
6656
objectUrl: "",
67-
style: ImageStyle.PHOTO.toString()
57+
style: ImageStyle.PHOTO
6858
});
59+
setOpenModal(true);
6960
};
7061

71-
const handleAddFullImage = (data: {
72-
title: string,
73-
objectUrl: string,
74-
author: string,
75-
authorUrl: string,
76-
derivativeSourceUrl: string,
77-
imageStyle: number
78-
}) => {
79-
setLocalImages(localImages.concat({
80-
url: popupImage?.url!,
81-
file: popupImage?.file!,
82-
83-
author: data.author,
84-
authorUrl: data.authorUrl,
85-
title: data.title,
86-
derivativeSourceUrl: data.derivativeSourceUrl,
87-
objectUrl: data.objectUrl,
88-
style: data.imageStyle.toString()
89-
}));
62+
const handleAddFullImage = (data: ImageFormData) => {
63+
setLocalImages(prevImages => prevImages.concat(data));
9064
handleCloseModal();
9165
};
9266

@@ -99,89 +73,15 @@ export const Step5Images = ({ onContinue, onBack }: StepProps) => {
9973
onContinue!();
10074
};
10175

102-
const style = {
103-
position: 'absolute' as const,
104-
top: '50%',
105-
left: '50%',
106-
transform: 'translate(-50%, -50%)',
107-
width: 600,
108-
bgcolor: 'background.paper',
109-
//border: '2px solid #000',
110-
boxShadow: 24,
111-
p: 4,
112-
};
113-
11476
return (
11577
(<div>
116-
<Modal
78+
<ImageFormModal
11779
open={openModal}
11880
onClose={handleCloseModal}
119-
aria-labelledby="modal-modal-title"
120-
aria-describedby="modal-modal-description"
121-
>
122-
123-
<Box sx={style}>
124-
<Typography id="modal-modal-title" variant="h6" component="h2">
125-
{t('exercises.imageDetails')}
126-
</Typography>
127-
128-
<Grid container spacing={2}>
129-
<Grid size={4}>
130-
{popupImage && <img
131-
style={{ width: "100%", }}
132-
src={popupImage.url}
133-
alt=""
134-
loading="lazy"
135-
/>}
136-
</Grid>
137-
<Grid size={8}>
138-
<Formik
139-
initialValues={{
140-
title: '',
141-
objectUrl: '',
142-
author: profileQuery.isSuccess ? profileQuery.data!.username : '',
143-
authorUrl: '',
144-
derivativeSourceUrl: '',
145-
imageStyle: ImageStyle.PHOTO,
146-
}}
147-
onSubmit={values => {
148-
console.log(values);
149-
handleAddFullImage(values);
150-
}}
151-
>
152-
{formik => {
153-
return (<Form>
154-
<Stack spacing={2}>
155-
<LicenseTitle fieldName={'title'} />
156-
<LicenseObjectUrl fieldName={'objectUrl'} />
157-
<LicenseAuthor fieldName={'author'} />
158-
<LicenseAuthorUrl fieldName={'authorUrl'} />
159-
<LicenseDerivativeSourceUrl fieldName={'derivativeSourceUrl'} />
160-
<ImageStyleToggle fieldName={'imageStyle'} />
161-
<Alert icon={<InfoIcon fontSize="inherit" />} severity="info">
162-
By submitting this image, you agree to release it under the <a
163-
href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank"
164-
rel="noreferrer">CC
165-
BY-SA 4.0</a> license. The image must be either your own work or the
166-
author must have released in under
167-
a license compatible with CC BY-SA 4.0.
168-
</Alert>
169-
</Stack>
170-
<Stack direction="row" justifyContent="end"
171-
sx={{ mt: 2 }}>
172-
173-
<Button color="primary" variant="contained" type="submit" sx={{ mt: 2 }}>
174-
{t('add')}
175-
</Button>
176-
177-
</Stack>
178-
</Form>);
179-
}}
180-
</Formik>
181-
</Grid>
182-
</Grid>
183-
</Box>
184-
</Modal>
81+
image={selectedImage}
82+
onSubmit={handleAddFullImage}
83+
submitLabel={t('add')}
84+
/>
18585
<Typography>
18686
{t("exercises.compatibleImagesCC")}
18787

@@ -216,7 +116,7 @@ export const Step5Images = ({ onContinue, onBack }: StepProps) => {
216116
</Stack>
217117
<ImageList
218118
cols={3}
219-
style={{ maxHeight: "400px", }}>
119+
style={{ maxHeight: "400px" }}>
220120
{localImages.map(imageEntry => (
221121
<ImageListItem key={imageEntry.url}>
222122
<img

0 commit comments

Comments
 (0)