Enhance photo upload functionality in ArticleQuestionForm and ArticleRatingForm: Added reset method to PhotoUpload component and integrated it into both forms to clear uploaded files upon submission. Improved user experience by ensuring the photo upload state resets after form submission.

This commit is contained in:
sebseb7
2025-07-18 12:13:01 +02:00
parent 47a882b667
commit 67f0126343
3 changed files with 69 additions and 35 deletions

View File

@@ -23,6 +23,7 @@ class ArticleQuestionForm extends Component {
success: false, success: false,
error: null error: null
}; };
this.photoUploadRef = React.createRef();
} }
handleInputChange = (field) => (event) => { handleInputChange = (field) => (event) => {
@@ -90,6 +91,10 @@ class ArticleQuestionForm extends Component {
question: '', question: '',
photos: [] photos: []
}); });
// Reset the photo upload component
if (this.photoUploadRef.current) {
this.photoUploadRef.current.reset();
}
} else { } else {
this.setState({ this.setState({
loading: false, loading: false,
@@ -110,24 +115,28 @@ class ArticleQuestionForm extends Component {
}); });
} }
// Fallback timeout in case backend doesn't respond // Fallback timeout in case backend doesn't respond
setTimeout(() => {
if (this.state.loading) {
this.setState({
loading: false,
success: true,
name: '',
email: '',
question: '',
photos: []
});
// Clear success message after 3 seconds
setTimeout(() => { setTimeout(() => {
this.setState({ success: false }); if (this.state.loading) {
}, 3000); this.setState({
} loading: false,
}, 5000); success: true,
name: '',
email: '',
question: '',
photos: []
});
// Reset the photo upload component
if (this.photoUploadRef.current) {
this.photoUploadRef.current.reset();
}
// Clear success message after 3 seconds
setTimeout(() => {
this.setState({ success: false });
}, 3000);
}
}, 5000);
}; };
render() { render() {
@@ -190,6 +199,7 @@ class ArticleQuestionForm extends Component {
/> />
<PhotoUpload <PhotoUpload
ref={this.photoUploadRef}
onChange={this.handlePhotosChange} onChange={this.handlePhotosChange}
disabled={loading} disabled={loading}
maxFiles={3} maxFiles={3}

View File

@@ -26,6 +26,7 @@ class ArticleRatingForm extends Component {
success: false, success: false,
error: null error: null
}; };
this.photoUploadRef = React.createRef();
} }
handleInputChange = (field) => (event) => { handleInputChange = (field) => (event) => {
@@ -99,6 +100,10 @@ class ArticleRatingForm extends Component {
review: '', review: '',
photos: [] photos: []
}); });
// Reset the photo upload component
if (this.photoUploadRef.current) {
this.photoUploadRef.current.reset();
}
} else { } else {
this.setState({ this.setState({
loading: false, loading: false,
@@ -119,25 +124,29 @@ class ArticleRatingForm extends Component {
}); });
} }
// Fallback timeout in case backend doesn't respond // Fallback timeout in case backend doesn't respond
setTimeout(() => {
if (this.state.loading) {
this.setState({
loading: false,
success: true,
name: '',
email: '',
rating: 0,
review: '',
photos: []
});
// Clear success message after 3 seconds
setTimeout(() => { setTimeout(() => {
this.setState({ success: false }); if (this.state.loading) {
}, 3000); this.setState({
} loading: false,
}, 5000); success: true,
name: '',
email: '',
rating: 0,
review: '',
photos: []
});
// Reset the photo upload component
if (this.photoUploadRef.current) {
this.photoUploadRef.current.reset();
}
// Clear success message after 3 seconds
setTimeout(() => {
this.setState({ success: false });
}, 3000);
}
}, 5000);
}; };
render() { render() {
@@ -219,6 +228,7 @@ class ArticleRatingForm extends Component {
/> />
<PhotoUpload <PhotoUpload
ref={this.photoUploadRef}
onChange={this.handlePhotosChange} onChange={this.handlePhotosChange}
disabled={loading} disabled={loading}
maxFiles={5} maxFiles={5}

View File

@@ -153,6 +153,20 @@ class PhotoUpload extends Component {
img.src = dataURL; img.src = dataURL;
}; };
// Method to reset the component
reset = () => {
this.setState({
files: [],
previews: [],
error: null
});
// Also reset the file input
if (this.fileInputRef.current) {
this.fileInputRef.current.value = '';
}
};
render() { render() {
const { files, previews, error } = this.state; const { files, previews, error } = this.state;
const { disabled, label } = this.props; const { disabled, label } = this.props;