박민정

[feat] Delete product image

Showing 33 changed files with 54 additions and 7 deletions
1 import React from 'react' 1 import React from 'react'
2 import Dropzone from 'react-dropzone' 2 import Dropzone from 'react-dropzone'
3 import axios from 'axios'; 3 import axios from 'axios';
4 +import { useState } from 'react';
4 5
5 -function ImageUpload() { 6 +function ImageUpload(props) {
6 7
8 + const [Images, setImages] = useState([]) // 이미지를 여러장 들어가게 하기 위해서
9 +
10 + // 이미지 서버에 저장
7 const imageDropEvent = (files) => { 11 const imageDropEvent = (files) => {
8 let imageData = new FormData(); 12 let imageData = new FormData();
9 13
...@@ -16,7 +20,10 @@ function ImageUpload() { ...@@ -16,7 +20,10 @@ function ImageUpload() {
16 axios.post('/api/product/image', imageData, config) 20 axios.post('/api/product/image', imageData, config)
17 .then(response => { 21 .then(response => {
18 if (response.data.success) { 22 if (response.data.success) {
19 - console.log(response.data) 23 + //console.log(response.data)
24 + setImages([...Images, response.data.filePath])
25 + props.refreshFunction([...Images, response.data.filePath])
26 + // 이 props (refreshFunction)은 UploadPage에 정의되어 있음
20 } 27 }
21 else { 28 else {
22 alert('파일 저장을 실패했습니다.') 29 alert('파일 저장을 실패했습니다.')
...@@ -24,6 +31,21 @@ function ImageUpload() { ...@@ -24,6 +31,21 @@ function ImageUpload() {
24 }) 31 })
25 32
26 } 33 }
34 +
35 + // 이미지 삭제 위한 deleteEvent
36 + const deleteEvent = (image) => {
37 + const currentIndex = Images.indexOf(image);
38 +
39 + //console.log(currentIndex);
40 +
41 + let updateImages = [...Images];
42 + updateImages.splice(currentIndex, 1); // currentIndex부터 1개의 사진을 지움
43 +
44 + setImages(updateImages);
45 + props.refreshFunction(updateImages);
46 + }
47 +
48 +
27 return ( 49 return (
28 <div style={ {display:'flex', justifyContent:'space-between'}}> 50 <div style={ {display:'flex', justifyContent:'space-between'}}>
29 <Dropzone onDrop={imageDropEvent}> 51 <Dropzone onDrop={imageDropEvent}>
...@@ -39,7 +61,26 @@ function ImageUpload() { ...@@ -39,7 +61,26 @@ function ImageUpload() {
39 </div> 61 </div>
40 </section> 62 </section>
41 )} 63 )}
42 -</Dropzone> 64 + </Dropzone>
65 +
66 +
67 + {/* 파일 업로드하면 옆에 나오게 하도록 */}
68 + <div style={{
69 + width: '350px', height: '200px', borderRadius: '1em'
70 + , overflowX: 'scroll'
71 + }}>
72 + {Images.map((image, index) => (
73 +
74 + <div onClick={ () => deleteEvent(image) }
75 + key={index}>
76 + <img style={{
77 + width: '300px', height: '200px', border: '1px solid lightgray',
78 + borderRadius: '1em', display: 'flex'}}
79 + src={`http://localhost:5000/${image}`} />
80 + </div>
81 + ))}
82 + </div>
83 +
43 </div> 84 </div>
44 ) 85 )
45 } 86 }
......
...@@ -43,6 +43,10 @@ function UploadPage() { ...@@ -43,6 +43,10 @@ function UploadPage() {
43 setImage(event.currentTarget.value); 43 setImage(event.currentTarget.value);
44 } 44 }
45 45
46 + const updateImages = ( newImages ) => {
47 + setImage(newImages);
48 + }
49 +
46 50
47 return ( 51 return (
48 <div style={{ maxWidth: '700px', margin: '2rem auto' }}> 52 <div style={{ maxWidth: '700px', margin: '2rem auto' }}>
...@@ -53,8 +57,8 @@ function UploadPage() { ...@@ -53,8 +57,8 @@ function UploadPage() {
53 </div> 57 </div>
54 58
55 <Form> 59 <Form>
56 - {/* 파일업로드 부분은 코드가 길어서 따로 컴포넌트로 만들었습니다. */} 60 + {/* 파일업로드 부분은 코드가 길어서 따로 컴포넌트로 만들어버리기~! */}
57 - <ImageUpload /> 61 + <ImageUpload refreshFunction={updateImages}/>
58 <br /> 62 <br />
59 <br /> 63 <br />
60 <label>이름</label> 64 <label>이름</label>
......
...@@ -20,8 +20,10 @@ router.post('/image', (req, res) => { ...@@ -20,8 +20,10 @@ router.post('/image', (req, res) => {
20 20
21 // 클라이언트로부터 받은 이미지 저장 21 // 클라이언트로부터 받은 이미지 저장
22 upload(req, res, (err) => { 22 upload(req, res, (err) => {
23 - if (err) return req.json({ success: false, err }) 23 + if (err) {
24 - return res.json({success: true, filePath: res.req.file.path, fileName: res.req.file.filename}) 24 + return req.json({ success: false, err })
25 + }
26 + return res.json({ success: true, filePath: res.req.file.path, fileName: res.req.file.filename })
25 } 27 }
26 ) 28 )
27 29
......