Skip to content

Commit

Permalink
Merge pull request #109 from kshitij01042002/img-preview
Browse files Browse the repository at this point in the history
  • Loading branch information
narayan954 authored Dec 7, 2022
2 parents a9c82d8 + a4aa30b commit dd010f8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ function App() {
</div>
)}
</div>
<Dialog open={openNewUpload} onClose={() => setOpenNewUpload(false)}>
<Dialog
open={openNewUpload}
onClose={() => setOpenNewUpload(false)}
>
<DialogTitle>New Upload</DialogTitle>
<DialogContent>
{!loadingPosts &&
Expand Down Expand Up @@ -317,8 +320,7 @@ function App() {
onChange={(e) => setPassword(e.target.value)}
/>
<label for="file">Choose your profile pic</label>
<Input type="file" id="file" onChange={handleChange} />

<Input type="file" id="file" onChange={handleChange}/>
<AnimatedButton
type="submit"
variant="contained"
Expand Down
30 changes: 27 additions & 3 deletions src/components/ImgUpload.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@ import firebase from "firebase/compat/app";
import AnimatedButton from "./AnimatedButton";
import { LinearProgress, TextField } from "@mui/material";
import { useSnackbar } from "notistack";
import "./imgPreview.css";


function ImgUpload(props) {
const [image, setImage] = useState(null);
const [caption, setCaption] = useState("");
const [progress, setProgress] = useState(0);
const [uploadingPost, setUploadingPost] = useState(false);
const imgInput = useRef(null);
const [imagePreviews, setImagePreviews] = useState([]);

const handleChange = (e) => {
let images = [];

if (e.target.files?.length) {
setImage(Array.from(e.target.files));
setImagePreviews(image);
}
for (let i = 0; i < e.target.files.length; i++) {
images.push(URL.createObjectURL(e.target.files[i]));
}
};

setImagePreviews(images);
};

const { enqueueSnackbar } = useSnackbar();

Expand Down Expand Up @@ -121,7 +131,21 @@ function ImgUpload(props) {
ref={imgInput}
disabled={uploadingPost}
/>
)}
)}
{imagePreviews && (
<div>
{imagePreviews.map((img, i) => {
return (
<img
id="imgPreview"
className="preview"
src={img}
alt={`image-${i}`}
key={i} />
);
})}
</div>
)}
<TextField
onChange={(e) => setCaption(e.target.value)}
value={caption}
Expand All @@ -131,7 +155,7 @@ function ImgUpload(props) {
rows={4}
disabled={uploadingPost}
/>
<AnimatedButton onClick={handleUpload} loading={uploadingPost}>
<AnimatedButton onClick={handleUpload} loading={uploadingPost}>
Upload
</AnimatedButton>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/components/imgPreview.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.preview{
width: 150px;
height: auto;
padding: 50px;
}
2 changes: 2 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,5 @@ div#picker {
left: -7px;
transform: scale(0.9);
}


0 comments on commit dd010f8

Please sign in to comment.