update runCase at once

This commit is contained in:
Takeshi Kimata
2024-07-11 21:08:33 +09:00
parent 5581118ec9
commit 39db493e4a
5 changed files with 44 additions and 53 deletions

View File

@@ -13,10 +13,6 @@ module.exports = function (sequelize) {
const runCases = req.body; const runCases = req.body;
const t = await sequelize.transaction(); const t = await sequelize.transaction();
console.log('############## start edit');
console.log(runCases);
console.log('############## end edit');
const createRunCase = async (runCase) => { const createRunCase = async (runCase) => {
const newRunCase = await RunCase.create( const newRunCase = await RunCase.create(
{ {
@@ -47,7 +43,7 @@ module.exports = function (sequelize) {
transaction: t, transaction: t,
} }
); );
return step; return runCase;
}; };
try { try {

View File

@@ -209,6 +209,7 @@ export default function CaseEditor({ projectId, folderId, caseId, messages, loca
await updateSteps(context.token.access_token, Number(caseId), testCase.Steps); await updateSteps(context.token.access_token, Number(caseId), testCase.Steps);
} }
setIsUpdating(false); setIsUpdating(false);
setIsDirty(false);
}} }}
> >
{isUpdating ? messages.updating : messages.update} {isUpdating ? messages.updating : messages.update}

View File

@@ -140,30 +140,21 @@ export default function RunEditor({ projectId, runId, messages, locale }: Props)
const handleIncludeExcludeCase = async (isInclude: boolean, clickedTestCaseId: number) => { const handleIncludeExcludeCase = async (isInclude: boolean, clickedTestCaseId: number) => {
setIsDirty(true); setIsDirty(true);
if (isInclude) { const keys = [clickedTestCaseId];
const newRunCase: RunCaseType = { const newRunCases = processRunCases(isInclude, keys, Number(runId), runCases);
id: 0, setRunCases(newRunCases);
runId: Number(runId),
caseId: clickedTestCaseId,
status: 0,
editState: 'new',
};
setRunCases([...runCases, newRunCase]); const updatedTestCases = testcases.map((testcase) => {
} else { if (testcase.id === clickedTestCaseId) {
const deleteRunCase = runCases.find((runCase) => runCase.caseId === clickedTestCaseId); return { ...testcase, isIncluded: isInclude };
if (!deleteRunCase) {
return;
} }
deleteRunCase.editState = 'deleted'; return testcase;
});
setRunCases((prevRunCases) => setTestCases(updatedTestCases);
prevRunCases.map((runCase) => (runCase.caseId === deleteRunCase.runId ? deleteRunCase : runCase))
);
}
}; };
const handleBulkIncludeExcludeCases = async (isInclude: boolean) => { const handleBulkIncludeExcludeCases = async (isInclude: boolean) => {
setIsDirty(true);
let keys: number[] = []; let keys: number[] = [];
if (selectedKeys === 'all') { if (selectedKeys === 'all') {
keys = testcases.map((item) => item.id); 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 updateRun(context.token.access_token, testRun);
await updateRunCases(context.token.access_token, Number(runId), runCases); await updateRunCases(context.token.access_token, Number(runId), runCases);
setIsUpdating(false); setIsUpdating(false);
setIsDirty(false);
}} }}
> >
{isUpdating ? messages.updating : messages.update} {isUpdating ? messages.updating : messages.update}

View File

@@ -156,17 +156,23 @@ function processRunCases(
runId: number, runId: number,
currentRunCases: RunCaseType[] currentRunCases: RunCaseType[]
): RunCaseType[] { ): RunCaseType[] {
if (isInclude) { const updatedRunCases = [...currentRunCases];
const updatedRunCases = currentRunCases.map((runCase) => {
if (keys.includes(runCase.caseId) && runCase.editState === 'deleted') {
return { ...runCase, editState: 'changed' } as RunCaseType;
}
return runCase;
});
if (isInclude) {
keys.forEach((caseId) => { keys.forEach((caseId) => {
const existingRunCase = currentRunCases.find((runCase) => runCase.caseId === 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({ updatedRunCases.push({
id: -1, id: -1,
runId: runId, runId: runId,
@@ -176,27 +182,26 @@ function processRunCases(
}); });
} }
}); });
return updatedRunCases;
} else { } else {
const updatedRunCases = currentRunCases keys.forEach((caseId) => {
.filter((runCase) => { const existingRunCase = currentRunCases.find((runCase) => runCase.caseId === caseId);
// If editState is 'new', remove from the array if (!existingRunCase) {
if (keys.includes(runCase.caseId) && runCase.editState === 'new') { // already excluded
return false; } 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[]) { 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), body: JSON.stringify(runCases),
}; };
console.log(runCases);
const url = `${apiServer}/runcases/update?runId=${runId}`; const url = `${apiServer}/runcases/update?runId=${runId}`;
try { try {
const response = await fetch(url, fetchOptions); const response = await fetch(url, fetchOptions);
if (!response.ok) { if (!response.ok) {

View File

@@ -2,7 +2,6 @@ import { useEffect } from 'react';
export const useFormGuard = (isDirty: boolean, confirmText: string) => { export const useFormGuard = (isDirty: boolean, confirmText: string) => {
useEffect(() => { useEffect(() => {
console.log(isDirty);
const handleClick = (event: MouseEvent) => { const handleClick = (event: MouseEvent) => {
if (isDirty && event.target instanceof Element && event.target.closest('a:not([target="_blank"]')) { if (isDirty && event.target instanceof Element && event.target.closest('a:not([target="_blank"]')) {
if (!window.confirm(confirmText)) { if (!window.confirm(confirmText)) {