Skip to content

Commit

Permalink
Refactor duplicated code in import modal
Browse files Browse the repository at this point in the history
  • Loading branch information
Lehats committed Dec 19, 2024
1 parent b3dd351 commit 4a81b32
Showing 1 changed file with 31 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,70 +28,48 @@ const ImportModal = ({
const { t } = useTranslation();
const [fileType, setFileType] = useState<string | null>(null); // Track file type

const handleImportResponse = async (response: Response) => {
setCreating(false);
setModal(false);
setUpload(false);

if (response.ok) {
showAlert(`${await response.text()} ${t("boreholesImported")}.`, "success");
refresh();
} else {
const responseBody = await response.json();
if (response.status === 400) {
if (responseBody.errors) {
// If response is of type ValidationProblemDetails, open validation error modal.
setErrorsResponse(responseBody);
setValidationErrorModal(true);
refresh();
} else {
// If response is of type ProblemDetails, show error message.
showAlert(responseBody.detail, "error");
}
} else if (response.status === 504) {
showAlert(t("boreholesImportLongRunning"), "error");
} else {
showAlert(t("boreholesImportError"), "error");
}
}
};

const handleBoreholeImport = () => {
const combinedFormData = new FormData();
if (selectedFile !== null) {
selectedFile.forEach((file: string | Blob) => {
combinedFormData.append("boreholesFile", file);
});
}
if (fileType == "csv") {
if (fileType === "csv") {
importBoreholesCsv(workgroup, combinedFormData).then(response => {
setCreating(false);
setModal(false);
setUpload(false);
(async () => {
if (response.ok) {
showAlert(`${await response.text()} ${t("boreholesImported")}.`, "success");
refresh();
} else {
const responseBody = await response.json();
if (response.status === 400) {
if (responseBody.errors) {
// If response is of type ValidationProblemDetails, open validation error modal.
setErrorsResponse(responseBody);
setValidationErrorModal(true);
refresh();
} else {
// If response is of type ProblemDetails, show error message.
showAlert(responseBody.detail, "error");
}
} else if (response.status === 504) {
showAlert(t("boreholesImportLongRunning"), "error");
} else {
showAlert(t("boreholesImportError"), "error");
}
}
})();
handleImportResponse(response);
});
} else {
importBoreholesJson(workgroup, combinedFormData).then(response => {
setCreating(false);
setModal(false);
setUpload(false);
(async () => {
if (response.ok) {
showAlert(`${await response.text()} ${t("boreholesImported")}.`, "success");
refresh();
} else {
const responseBody = await response.json();
if (response.status === 400) {
if (responseBody.errors) {
// If response is of type ValidationProblemDetails, open validation error modal.
setErrorsResponse(responseBody);
setValidationErrorModal(true);
refresh();
} else {
// If response is of type ProblemDetails, show error message.
showAlert(responseBody.detail, "error");
}
} else if (response.status === 504) {
showAlert(t("boreholesImportLongRunning"), "error");
} else {
showAlert(t("boreholesImportError"), "error");
}
}
})();
handleImportResponse(response);
});
}
};
Expand Down

0 comments on commit 4a81b32

Please sign in to comment.