From 39db493e4afaa0db31535a3ccc445307c9a06e02 Mon Sep 17 00:00:00 2001 From: Takeshi Kimata <117462761+kimatata@users.noreply.github.com> Date: Thu, 11 Jul 2024 21:08:33 +0900 Subject: [PATCH] update runCase at once --- backend/routes/runcases/edit.js | 6 +- .../[folderId]/cases/[caseId]/CaseEditor.tsx | 1 + .../[projectId]/runs/[runId]/RunEditor.tsx | 30 ++++------ .../projects/[projectId]/runs/runsControl.ts | 59 ++++++++++--------- frontend/utils/formGuard.ts | 1 - 5 files changed, 44 insertions(+), 53 deletions(-) diff --git a/backend/routes/runcases/edit.js b/backend/routes/runcases/edit.js index 62cb791..fabd071 100644 --- a/backend/routes/runcases/edit.js +++ b/backend/routes/runcases/edit.js @@ -13,10 +13,6 @@ module.exports = function (sequelize) { const runCases = req.body; const t = await sequelize.transaction(); - console.log('############## start edit'); - console.log(runCases); - console.log('############## end edit'); - const createRunCase = async (runCase) => { const newRunCase = await RunCase.create( { @@ -47,7 +43,7 @@ module.exports = function (sequelize) { transaction: t, } ); - return step; + return runCase; }; try { diff --git a/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/CaseEditor.tsx b/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/CaseEditor.tsx index dd540b2..d9728ef 100644 --- a/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/CaseEditor.tsx +++ b/frontend/src/app/[locale]/projects/[projectId]/folders/[folderId]/cases/[caseId]/CaseEditor.tsx @@ -209,6 +209,7 @@ export default function CaseEditor({ projectId, folderId, caseId, messages, loca await updateSteps(context.token.access_token, Number(caseId), testCase.Steps); } setIsUpdating(false); + setIsDirty(false); }} > {isUpdating ? messages.updating : messages.update} diff --git a/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/RunEditor.tsx b/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/RunEditor.tsx index b33c59d..bbae39c 100644 --- a/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/RunEditor.tsx +++ b/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/RunEditor.tsx @@ -140,30 +140,21 @@ export default function RunEditor({ projectId, runId, messages, locale }: Props) const handleIncludeExcludeCase = async (isInclude: boolean, clickedTestCaseId: number) => { setIsDirty(true); - if (isInclude) { - const newRunCase: RunCaseType = { - id: 0, - runId: Number(runId), - caseId: clickedTestCaseId, - status: 0, - editState: 'new', - }; + const keys = [clickedTestCaseId]; + const newRunCases = processRunCases(isInclude, keys, Number(runId), runCases); + setRunCases(newRunCases); - setRunCases([...runCases, newRunCase]); - } else { - const deleteRunCase = runCases.find((runCase) => runCase.caseId === clickedTestCaseId); - if (!deleteRunCase) { - return; + const updatedTestCases = testcases.map((testcase) => { + if (testcase.id === clickedTestCaseId) { + return { ...testcase, isIncluded: isInclude }; } - deleteRunCase.editState = 'deleted'; - - setRunCases((prevRunCases) => - prevRunCases.map((runCase) => (runCase.caseId === deleteRunCase.runId ? deleteRunCase : runCase)) - ); - } + return testcase; + }); + setTestCases(updatedTestCases); }; const handleBulkIncludeExcludeCases = async (isInclude: boolean) => { + setIsDirty(true); let keys: number[] = []; if (selectedKeys === 'all') { keys = testcases.map((item) => item.id); @@ -215,6 +206,7 @@ export default function RunEditor({ projectId, runId, messages, locale }: Props) await updateRun(context.token.access_token, testRun); await updateRunCases(context.token.access_token, Number(runId), runCases); setIsUpdating(false); + setIsDirty(false); }} > {isUpdating ? messages.updating : messages.update} diff --git a/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.ts b/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.ts index 96723c4..1b31c1f 100644 --- a/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.ts +++ b/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.ts @@ -156,17 +156,23 @@ function processRunCases( runId: number, currentRunCases: RunCaseType[] ): RunCaseType[] { - if (isInclude) { - const updatedRunCases = currentRunCases.map((runCase) => { - if (keys.includes(runCase.caseId) && runCase.editState === 'deleted') { - return { ...runCase, editState: 'changed' } as RunCaseType; - } - return runCase; - }); + const updatedRunCases = [...currentRunCases]; + if (isInclude) { keys.forEach((caseId) => { const existingRunCase = currentRunCases.find((runCase) => runCase.caseId === caseId); - if (!existingRunCase) { + if (existingRunCase) { + // already included + if (existingRunCase.editState === 'notChanged') { + // do nothing + } else if (existingRunCase.editState === 'changed') { + // do nothing + } else if (existingRunCase.editState === 'new') { + // do nothing + } else if (existingRunCase.editState === 'deleted') { + existingRunCase.editState = 'changed'; + } + } else { updatedRunCases.push({ id: -1, runId: runId, @@ -176,27 +182,26 @@ function processRunCases( }); } }); - - return updatedRunCases; } else { - const updatedRunCases = currentRunCases - .filter((runCase) => { - // If editState is 'new', remove from the array - if (keys.includes(runCase.caseId) && runCase.editState === 'new') { - return false; + keys.forEach((caseId) => { + const existingRunCase = currentRunCases.find((runCase) => runCase.caseId === caseId); + if (!existingRunCase) { + // already excluded + } else { + if (existingRunCase.editState === 'notChanged') { + existingRunCase.editState = 'deleted'; + } else if (existingRunCase.editState === 'changed') { + existingRunCase.editState = 'deleted'; + } else if (existingRunCase.editState === 'new') { + existingRunCase.editState = 'deleted'; + } else if (existingRunCase.editState === 'deleted') { + // do nothing } - return true; - }) - .map((runCase) => { - // If editState isn't 'new', set editState to 'deleted'. - if (keys.includes(runCase.caseId) && runCase.editState !== 'new') { - return { ...runCase, editState: 'deleted' } as RunCaseType; - } - return runCase; - }); - - return updatedRunCases; + } + }); } + + return updatedRunCases; } async function updateRunCases(jwt: string, runId: number, runCases: RunCaseType[]) { @@ -209,9 +214,7 @@ async function updateRunCases(jwt: string, runId: number, runCases: RunCaseType[ body: JSON.stringify(runCases), }; - console.log(runCases); const url = `${apiServer}/runcases/update?runId=${runId}`; - try { const response = await fetch(url, fetchOptions); if (!response.ok) { diff --git a/frontend/utils/formGuard.ts b/frontend/utils/formGuard.ts index 80d4185..f0e5d8a 100644 --- a/frontend/utils/formGuard.ts +++ b/frontend/utils/formGuard.ts @@ -2,7 +2,6 @@ import { useEffect } from 'react'; export const useFormGuard = (isDirty: boolean, confirmText: string) => { useEffect(() => { - console.log(isDirty); const handleClick = (event: MouseEvent) => { if (isDirty && event.target instanceof Element && event.target.closest('a:not([target="_blank"]')) { if (!window.confirm(confirmText)) {