Refactor caseControl's duplication
This commit is contained in:
@@ -16,15 +16,13 @@ import { priorities, testTypes, templates } from "@/config/selection";
|
||||
import CaseStepsEditor from "./CaseStepsEditor";
|
||||
import CaseAttachmentsEditor from "./CaseAttachmentsEditor";
|
||||
import { CaseType, AttachmentType } from "@/types/case";
|
||||
import { fetchCase, updateCase } from "../caseControl";
|
||||
import { fetchCreateStep, fetchDeleteStep } from "./stepControl";
|
||||
import {
|
||||
fetchCase,
|
||||
fetchCreateStep,
|
||||
fetchDeleteStep,
|
||||
updateCase,
|
||||
fetchCreateAttachments,
|
||||
fetchDownloadAttachment,
|
||||
fetchDeleteAttachment,
|
||||
} from "./caseControl";
|
||||
} from "./attachmentControl";
|
||||
|
||||
const defaultTestCase = {
|
||||
id: 0,
|
||||
@@ -371,9 +369,10 @@ export default function CaseEditor({
|
||||
<h6>Attachments</h6>
|
||||
<CaseAttachmentsEditor
|
||||
attachments={testCase.Attachments}
|
||||
onAttachmentDownload={(attachmentId: number, downloadFileName: string) =>
|
||||
fetchDownloadAttachment(attachmentId, downloadFileName)
|
||||
}
|
||||
onAttachmentDownload={(
|
||||
attachmentId: number,
|
||||
downloadFileName: string
|
||||
) => fetchDownloadAttachment(attachmentId, downloadFileName)}
|
||||
onAttachmentDelete={onAttachmentDelete}
|
||||
/>
|
||||
<div
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import Config from "@/config/config";
|
||||
const apiServer = Config.apiServer;
|
||||
|
||||
async function fetchDownloadAttachment(
|
||||
attachmentId: number,
|
||||
downloadFileName: string
|
||||
) {
|
||||
const fetchOptions = {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
|
||||
const url = `${apiServer}/attachments/download/${attachmentId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
const downloadUrl = window.URL.createObjectURL(blob);
|
||||
|
||||
const link = document.createElement("a");
|
||||
link.href = downloadUrl;
|
||||
link.download = downloadFileName;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
} catch (error) {
|
||||
console.error("Error downloading file:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchCreateAttachments(caseId: number, files: File[]) {
|
||||
try {
|
||||
const formData = new FormData();
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
formData.append("files", files[i]);
|
||||
}
|
||||
|
||||
const url = `${apiServer}/attachments?parentCaseId=${caseId}`;
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
|
||||
const responseData = await response.json();
|
||||
return responseData;
|
||||
} catch (error) {
|
||||
console.error("Error uploading files:", error);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchDeleteAttachment(attachmentId: number) {
|
||||
const fetchOptions = {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
|
||||
const url = `${apiServer}/attachments/${attachmentId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error deleting file:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
fetchDownloadAttachment,
|
||||
fetchCreateAttachments,
|
||||
fetchDeleteAttachment,
|
||||
};
|
||||
@@ -1,199 +0,0 @@
|
||||
import Config from "@/config/config";
|
||||
const apiServer = Config.apiServer;
|
||||
import { CaseType } from "@/types/case";
|
||||
|
||||
/**
|
||||
* fetch case
|
||||
*/
|
||||
async function fetchCase(caseId: number) {
|
||||
const url = `${apiServer}/cases/${caseId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* create step
|
||||
*/
|
||||
async function fetchCreateStep(newStepNo: number, parentCaseId: number) {
|
||||
const fetchOptions = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
|
||||
const url = `${apiServer}/steps?newStepNo=${newStepNo}&parentCaseId=${parentCaseId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error("Error deleting project:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* delete step
|
||||
*/
|
||||
async function fetchDeleteStep(stepId: number, parentCaseId: number) {
|
||||
const fetchOptions = {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
|
||||
const url = `${apiServer}/steps/${stepId}?parentCaseId=${parentCaseId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error deleting project:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update case
|
||||
*/
|
||||
async function updateCase(updateCaseData: CaseType) {
|
||||
const fetchOptions = {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(updateCaseData),
|
||||
};
|
||||
|
||||
const url = `${apiServer}/cases/${updateCaseData.id}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error("Error updating project:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload attachments
|
||||
*/
|
||||
async function fetchCreateAttachments(caseId: number, files: File[]) {
|
||||
try {
|
||||
const formData = new FormData();
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
formData.append("files", files[i]);
|
||||
}
|
||||
|
||||
const url = `${apiServer}/attachments?parentCaseId=${caseId}`;
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
|
||||
const responseData = await response.json();
|
||||
return responseData;
|
||||
} catch (error) {
|
||||
console.error("Error uploading files:", error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* download attachment
|
||||
*/
|
||||
async function fetchDownloadAttachment(attachmentId: number, downloadFileName: string) {
|
||||
const fetchOptions = {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
|
||||
const url = `${apiServer}/attachments/download/${attachmentId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
const downloadUrl = window.URL.createObjectURL(blob);
|
||||
|
||||
const link = document.createElement("a");
|
||||
link.href = downloadUrl;
|
||||
link.download = downloadFileName;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
} catch (error) {
|
||||
console.error("Error downloading file:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* delete attachment
|
||||
*/
|
||||
async function fetchDeleteAttachment(attachmentId: number) {
|
||||
const fetchOptions = {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
|
||||
const url = `${apiServer}/attachments/${attachmentId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error deleting file:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
fetchCase,
|
||||
fetchCreateStep,
|
||||
fetchDeleteStep,
|
||||
updateCase,
|
||||
fetchCreateAttachments,
|
||||
fetchDownloadAttachment,
|
||||
fetchDeleteAttachment,
|
||||
};
|
||||
@@ -0,0 +1,50 @@
|
||||
import Config from "@/config/config";
|
||||
const apiServer = Config.apiServer;
|
||||
|
||||
async function fetchCreateStep(newStepNo: number, parentCaseId: number) {
|
||||
const fetchOptions = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
|
||||
const url = `${apiServer}/steps?newStepNo=${newStepNo}&parentCaseId=${parentCaseId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error("Error deleting project:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchDeleteStep(stepId: number, parentCaseId: number) {
|
||||
const fetchOptions = {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
|
||||
const url = `${apiServer}/steps/${stepId}?parentCaseId=${parentCaseId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error deleting project:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
fetchCreateStep,
|
||||
fetchDeleteStep,
|
||||
};
|
||||
@@ -1,5 +1,28 @@
|
||||
import Config from "@/config/config";
|
||||
const apiServer = Config.apiServer;
|
||||
import { CaseType } from "@/types/case";
|
||||
|
||||
async function fetchCase(caseId: number) {
|
||||
const url = `${apiServer}/cases/${caseId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchCases(folderId: string) {
|
||||
const url = `${apiServer}/cases?folderId=${folderId}`;
|
||||
@@ -60,6 +83,30 @@ async function createCase(folderId: string) {
|
||||
}
|
||||
}
|
||||
|
||||
async function updateCase(updateCaseData: CaseType) {
|
||||
const fetchOptions = {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(updateCaseData),
|
||||
};
|
||||
|
||||
const url = `${apiServer}/cases/${updateCaseData.id}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error("Error updating project:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteCase(caseId: number) {
|
||||
const fetchOptions = {
|
||||
method: "DELETE",
|
||||
@@ -103,4 +150,11 @@ async function deleteCases(deleteCases: string[]) {
|
||||
}
|
||||
}
|
||||
|
||||
export { fetchCases, createCase, deleteCase, deleteCases };
|
||||
export {
|
||||
fetchCase,
|
||||
fetchCases,
|
||||
updateCase,
|
||||
createCase,
|
||||
deleteCase,
|
||||
deleteCases,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user