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

Merged
merged 15 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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);
mozzy11 marked this conversation as resolved.
Show resolved Hide resolved
});
};

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(
mozzy11 marked this conversation as resolved.
Show resolved Hide resolved
config.serverBaseUrl + endPoint,

{
Expand Down
Loading