Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1339: Save Button Disabled During API Call, Disllowing Multiple Requests #1345

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions frontend/src/components/patient/CreatePatientForm.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import React, { useState, useRef, useEffect, useContext } from "react";
import { FormattedMessage, injectIntl, useIntl } from "react-intl";
import "../Style.css";
import {
getFromOpenElisServer,
getFromOpenElisServerSync,
postToOpenElisServer,
} from "../utils/Utils";
import { getFromOpenElisServer, postToOpenElisServer } from "../utils/Utils";
import { nationalityList } from "../data/countries";
import format from "date-fns/format";
import {
Expand Down Expand Up @@ -66,6 +62,9 @@ function CreatePatientForm(props) {
const [subjectNo, setSubjectNo] = useState(
props.selectedPatient.subjectNumber,
);

const [isSubmitting, setIsSubmitting] = useState(false);

const handleNationalIdChange = (event) => {
const newValue = event.target.value;
setNationalId(newValue);
Expand Down Expand Up @@ -308,6 +307,13 @@ function CreatePatientForm(props) {
};

const handleSubmit = async (values, { resetForm }) => {
// Prevent multiple submissions.
if (isSubmitting) {
return;
}

setIsSubmitting(true);

if ("years" in values) {
delete values.years;
}
Expand All @@ -330,7 +336,9 @@ function CreatePatientForm(props) {
days: "",
});
},
);
).then(() => {
setIsSubmitting(false);
Copy link
Collaborator

@mozzy11 mozzy11 Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can instead set this directly in the call back here ,when the call returns the Response , like we do for the other post-Submit actions

});
};

const handlePost = (status) => {
Expand Down Expand Up @@ -1007,14 +1015,15 @@ function CreatePatientForm(props) {
{props.showActionsButton && (
<>
<Column lg={4} md={4} sm={4}>
<Button type="submit" id="submit">
<Button type="submit" id="submit" disabled={isSubmitting}>
<FormattedMessage id="label.button.save" />
</Button>
</Column>
<Column lg={4} md={4} sm={4}>
<Button
id="clear"
kind="danger"
disabled={isSubmitting}
onClick={() => {
resetForm({ values: CreatePatientFormValues });
setHealthDistricts([]);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/utils/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const postToOpenElisServer = (
callback,
extraParams,
) => {
fetch(
return fetch(
Copy link
Collaborator

@mozzy11 mozzy11 Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aayush6194 , we already pass a call back to the fetch function when it returns the Response.

you can revert this change here so that we are consistent across the API call utilities

config.serverBaseUrl + endPoint,

{
Expand Down