-
Notifications
You must be signed in to change notification settings - Fork 2
/
AnswerModal.jsx
114 lines (106 loc) · 3.48 KB
/
AnswerModal.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import './styles.css';
function AnswerModal({
isOpen, onRequestClose, productName, questionBody,
selectedQuestion, refreshQuestions,
}) {
const [answerInput, setAnswerInput] = useState('');
const [nicknameInput, setNicknameInput] = useState('');
const [emailInput, setEmailInput] = useState('');
const [photos, setPhotos] = useState([]);
useEffect(() => {
document.body.style.overflow = isOpen ? 'hidden' : 'unset';
return () => {
document.body.style.overflow = 'unset'; // Reset on unmount
};
}, [isOpen]);
const handleFileChange = async (event) => {
// Limit to 3 photos
const files = Array.from(event.target.files).slice(0, 3);
const formData = new FormData();
files.forEach((file) => {
formData.append('images', file);
});
try {
const response = await axios.post('/upload', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
});
const uploadedUrls = response.data.urls;
setPhotos(uploadedUrls);
} catch (error) {
console.error('Error uploading files:', error);
}
};
const submitAnswer = async (event) => {
event.preventDefault();
const answerDetails = {
body: answerInput,
name: nicknameInput,
email: emailInput,
photos,
};
await axios.post(`/qa/questions/${selectedQuestion.question_id}/answers`, answerDetails);
refreshQuestions(); // Refresh the questions list to include the new answer
onRequestClose(); // Close the modal after submission
};
return isOpen ? (
<div className="modal-overlay" onClick={onRequestClose}>
<div className="form-popup" onClick={(e) => e.stopPropagation()}>
<h2>Submit your Answer</h2>
<h3>
{productName}
:
{questionBody}
</h3>
<form onSubmit={submitAnswer} className="form-container">
<textarea
id="answer-input"
aria-label="Your Answer"
placeholder="Your Answer *"
value={answerInput}
onChange={(e) => setAnswerInput(e.target.value)}
required
maxLength="1000"
/>
<input
type="text"
id="nickname-input"
aria-label="Your Nickname"
placeholder="What is your nickname *"
value={nicknameInput}
onChange={(e) => setNicknameInput(e.target.value)}
required
maxLength="60"
/>
<input
type="email"
id="email-input"
aria-label="Your Email"
placeholder="Your email *"
value={emailInput}
onChange={(e) => setEmailInput(e.target.value)}
required
maxLength="60"
/>
{/* File input for photos */}
<input
type="file"
accept="image/*"
multiple
onChange={handleFileChange}
/>
{/* Preview uploaded photos */}
<div className="photo-previews">
{photos.map((photo, index) => (
<img key={index} src={photo} alt="Preview" style={{ width: '100px', height: '100px' }} />
))}
</div>
<button type="submit" className="btn">Submit answer</button>
<button type="button" className="btn cancel" onClick={onRequestClose}>Close</button>
</form>
</div>
</div>
) : null;
}
export default AnswerModal;