Skip to content

Commit

Permalink
1339: Used 'isSubmitting' to track the state of the API call
Browse files Browse the repository at this point in the history
  • Loading branch information
aayush6194 committed Dec 10, 2024
1 parent c34303b commit de5d9df
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
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);
});
};

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(
config.serverBaseUrl + endPoint,

{
Expand Down

0 comments on commit de5d9df

Please sign in to comment.