Skip to content

Commit

Permalink
prevent multiple submits
Browse files Browse the repository at this point in the history
  • Loading branch information
kongtiaowang committed Sep 25, 2024
1 parent 01d093a commit 859d708
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions modules/acknowledgements/jsx/acknowledgementsIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class AcknowledgementsIndex extends Component {
this.state = {
data: {},
formData: {},
submitting: false, // track if form is being submitted
error: false,
isLoaded: false,
affiliationsOptions: {
Expand Down Expand Up @@ -146,7 +147,13 @@ class AcknowledgementsIndex extends Component {
* @param {event} e - event of the form
*/
handleSubmit(e) {
const formData = Object.assign({}, this.state.formData);
e.preventDefault(); // prevent default form submission
const { formData, submitting } = this.state;

if (submitting) return; // prevent multiple submits

this.setState({ submitting: true }); // set submitting to true

let formObject = new FormData();
for (let key in formData) {
if (formData[key] !== '') {
Expand All @@ -163,16 +170,14 @@ class AcknowledgementsIndex extends Component {
})
.then((resp) => {
if (resp.ok && resp.status === 200) {
swal.fire(
'Success!',
'Acknowledgement added.',
'success'
).then((result) => {
if (result.value) {
this.closeModalForm();
this.fetchData();
}
});
swal
.fire('Success!', 'Acknowledgement added.', 'success')
.then((result) => {
if (result.value) {
this.closeModalForm();
this.fetchData();
}
});
} else {
resp.text().then((message) => {
swal.fire('Error!', message, 'error');
Expand All @@ -181,6 +186,9 @@ class AcknowledgementsIndex extends Component {
})
.catch((error) => {
console.error(error);
})
.finally(() => {
this.setState({ submitting: false }); // reset submitting state
});
}

Expand Down Expand Up @@ -286,7 +294,7 @@ class AcknowledgementsIndex extends Component {
Module='acknowledgements'
name='addAcknowledgement'
id='addAcknowledgementForm'
onSubmit={this.handleSubmit}
onSubmit={(e) => this.handleSubmit(e)}
method='POST'
>
<TextboxElement
Expand Down Expand Up @@ -371,6 +379,7 @@ class AcknowledgementsIndex extends Component {
label='Save'
type='submit'
buttonClass='btn btn-sm btn-primary'
disabled={this.state.submitting}
/>
</div>
</FormElement>
Expand Down

0 comments on commit 859d708

Please sign in to comment.