feat: refactored run single request events

This commit is contained in:
Anoop M D 2023-09-13 23:37:21 +05:30
parent cee8073bb7
commit e6c3a5af4c
5 changed files with 81 additions and 94 deletions

View File

@ -26,7 +26,7 @@ const VariablesTable = ({ variables, collectionVariables }) => {
<div className='variable-value pl-2 whitespace-normal text-left flex-grow'>{variable.value}</div>
</div>
);
}) : null}
}) : <small>No env variables found</small>}
<div className='mt-2 font-medium'>Collection Variables</div>
{(collectionVars && collectionVars.length) ? collectionVars.map((variable) => {
@ -36,7 +36,7 @@ const VariablesTable = ({ variables, collectionVariables }) => {
<div className='variable-value pl-2 whitespace-normal text-left flex-grow'>{variable.value}</div>
</div>
);
}) : null}
}) : <small>No collection variables found</small>}
</div>
</StyledWrapper>
);

View File

@ -14,6 +14,7 @@ const VariablesView = ({collection}) => {
const environment = findEnvironmentInCollection(collection, collection.activeEnvironmentUid);
const variables = get(environment, 'variables', []);
const enabledVariables = filter(variables, (variable) => variable.enabled);
const showVariablesTable = enabledVariables.length > 0 || (collection.collectionVariables && Object.keys(collection.collectionVariables).length > 0);
return (
<StyledWrapper
@ -34,7 +35,12 @@ const VariablesView = ({collection}) => {
handleClose={() => setPopOverOpen(false)}
>
<div className="px-2 py-1">
{(enabledVariables && enabledVariables.length) ? <VariablesTable variables={enabledVariables} collectionVariables={collection.collectionVariables}/> : 'No variables found'}
{showVariablesTable ? (
<VariablesTable
variables={enabledVariables}
collectionVariables={collection.collectionVariables}
/>
) : 'No variables found'}
</div>
</PopOver>
)}

View File

@ -7,12 +7,9 @@ import {
collectionUnlinkFileEvent,
collectionUnlinkDirectoryEvent,
collectionUnlinkEnvFileEvent,
requestSentEvent,
requestQueuedEvent,
testResultsEvent,
assertionResultsEvent,
scriptEnvironmentUpdateEvent,
collectionRenamedEvent,
runRequestEvent,
runFolderEvent
} from 'providers/ReduxStore/slices/collections';
import toast from 'react-hot-toast';
@ -96,26 +93,10 @@ const useCollectionTreeSync = () => {
}
};
const _httpRequestSent = (val) => {
dispatch(requestSentEvent(val));
};
const _scriptEnvironmentUpdate = (val) => {
dispatch(scriptEnvironmentUpdateEvent(val));
};
const _httpRequestQueued = (val) => {
dispatch(requestQueuedEvent(val));
};
const _testResults = (val) => {
dispatch(testResultsEvent(val));
};
const _assertionResults = (val) => {
dispatch(assertionResultsEvent(val));
};
const _collectionRenamed = (val) => {
dispatch(collectionRenamedEvent(val));
};
@ -124,19 +105,20 @@ const useCollectionTreeSync = () => {
dispatch(runFolderEvent(val));
};
const _runRequestEvent = (val) => {
dispatch(runRequestEvent(val));
};
ipcRenderer.invoke('renderer:ready');
const removeListener1 = ipcRenderer.on('main:collection-opened', _openCollection);
const removeListener2 = ipcRenderer.on('main:collection-tree-updated', _collectionTreeUpdated);
const removeListener3 = ipcRenderer.on('main:collection-already-opened', _collectionAlreadyOpened);
const removeListener4 = ipcRenderer.on('main:display-error', _displayError);
const removeListener5 = ipcRenderer.on('main:http-request-sent', _httpRequestSent);
const removeListener6 = ipcRenderer.on('main:script-environment-update', _scriptEnvironmentUpdate);
const removeListener7 = ipcRenderer.on('main:http-request-queued', _httpRequestQueued);
const removeListener8 = ipcRenderer.on('main:test-results', _testResults);
const removeListener9 = ipcRenderer.on('main:assertion-results', _assertionResults);
const removeListener10 = ipcRenderer.on('main:collection-renamed', _collectionRenamed);
const removeListener11 = ipcRenderer.on('main:run-folder-event', _runFolderEvent);
const removeListener5 = ipcRenderer.on('main:script-environment-update', _scriptEnvironmentUpdate);
const removeListener6 = ipcRenderer.on('main:collection-renamed', _collectionRenamed);
const removeListener7 = ipcRenderer.on('main:run-folder-event', _runFolderEvent);
const removeListener8 = ipcRenderer.on('main:run-request-event', _runRequestEvent);
return () => {
removeListener1();
@ -147,9 +129,6 @@ const useCollectionTreeSync = () => {
removeListener6();
removeListener7();
removeListener8();
removeListener9();
removeListener10();
removeListener11();
};
}, [isElectron]);
};

View File

@ -156,32 +156,6 @@ export const collectionsSlice = createSlice({
}
}
},
requestSentEvent: (state, action) => {
const { itemUid, collectionUid, cancelTokenUid, requestSent } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
if (collection) {
const item = findItemInCollection(collection, itemUid);
if (item) {
item.requestSent = requestSent
item.response = item.response || {};
item.requestState = 'sending';
item.cancelTokenUid = cancelTokenUid;
}
}
},
requestQueuedEvent: (state, action) => {
const { itemUid, collectionUid, cancelTokenUid } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
if (collection) {
const item = findItemInCollection(collection, itemUid);
if (item) {
item.requestState = 'queued';
item.cancelTokenUid = cancelTokenUid;
}
}
},
scriptEnvironmentUpdateEvent: (state, action) => {
const { collectionUid, envVariables, collectionVariables } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
@ -1010,31 +984,6 @@ export const collectionsSlice = createSlice({
}
}
},
testResultsEvent: (state, action) => {
const { itemUid, collectionUid, results } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
if (collection) {
const item = findItemInCollection(collection, itemUid);
if (item) {
item.testResults = results;
}
}
},
assertionResultsEvent: (state, action) => {
const { itemUid, collectionUid, results } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
console.log(results);
if (collection) {
const item = findItemInCollection(collection, itemUid);
if (item) {
item.assertionResults = results;
}
}
},
collectionRenamedEvent: (state, action) => {
const { collectionPathname, newName } = action.payload;
const collection = findCollectionByPathname(state.collections, collectionPathname);
@ -1075,6 +1024,45 @@ export const collectionsSlice = createSlice({
collection.runnerResult = null;
}
},
runRequestEvent: (state, action) => {
const { itemUid, collectionUid, type, requestUid } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
if (collection) {
const item = findItemInCollection(collection, itemUid);
if (item) {
if(type === 'request-queued') {
const { cancelTokenUid } = action.payload;
item.requestUid = requestUid;
item.requestState = 'queued';
item.response = null;
item.cancelTokenUid = cancelTokenUid;
}
if(type === 'request-sent') {
const { cancelTokenUid, requestSent } = action.payload;
item.requestSent = requestSent;
// sometimes the response is received before the request-sent event arrives
if(item.requestUid === requestUid && item.requestState === 'queued') {
item.requestUid = requestUid;
item.requestState = 'sending';
item.cancelTokenUid = cancelTokenUid;
}
}
if(type === 'assertion-results') {
const { results } = action.payload;
item.assertionResults = results;
}
if(type === 'test-results') {
const { results } = action.payload;
item.testResults = results;
}
}
}
},
runFolderEvent: (state, action) => {
const { collectionUid, folderUid, itemUid, type, isRecursive, error } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
@ -1162,8 +1150,6 @@ export const {
deleteItem,
renameItem,
cloneItem,
requestSentEvent,
requestQueuedEvent,
scriptEnvironmentUpdateEvent,
requestCancelled,
responseReceived,
@ -1204,13 +1190,12 @@ export const {
collectionUnlinkFileEvent,
collectionUnlinkDirectoryEvent,
collectionAddEnvFileEvent,
testResultsEvent,
assertionResultsEvent,
collectionRenamedEvent,
toggleRunnerView,
showRunnerView,
hideRunnerView,
resetRunResults,
runRequestEvent,
runFolderEvent,
closeCollectionRunner
} = collectionsSlice.actions;

View File

@ -76,8 +76,11 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
// handler for sending http request
ipcMain.handle('send-http-request', async (event, item, collectionUid, collectionPath, environment, collectionVariables) => {
const cancelTokenUid = uuid();
const requestUid = uuid();
mainWindow.webContents.send('main:http-request-queued', {
mainWindow.webContents.send('main:run-request-event', {
type: 'request-queued',
requestUid,
collectionUid,
itemUid: item.uid,
cancelTokenUid
@ -112,6 +115,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
mainWindow.webContents.send('main:script-environment-update', {
envVariables: result.envVariables,
collectionVariables: result.collectionVariables,
requestUid,
collectionUid
});
}
@ -125,6 +129,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
mainWindow.webContents.send('main:script-environment-update', {
envVariables: result.envVariables,
collectionVariables: result.collectionVariables,
requestUid,
collectionUid
});
}
@ -139,7 +144,8 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
// todo:
// i have no clue why electron can't send the request object
// without safeParseJSON(safeStringifyJSON(request.data))
mainWindow.webContents.send('main:http-request-sent', {
mainWindow.webContents.send('main:run-request-event', {
type: 'request-sent',
requestSent: {
url: request.url,
method: request.method,
@ -148,6 +154,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
},
collectionUid,
itemUid: item.uid,
requestUid,
cancelTokenUid
});
@ -188,6 +195,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
mainWindow.webContents.send('main:script-environment-update', {
envVariables: result.envVariables,
collectionVariables: result.collectionVariables,
requestUid,
collectionUid
});
}
@ -201,6 +209,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
mainWindow.webContents.send('main:script-environment-update', {
envVariables: result.envVariables,
collectionVariables: result.collectionVariables,
requestUid,
collectionUid
});
}
@ -210,9 +219,11 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
const assertRuntime = new AssertRuntime();
const results = assertRuntime.runAssertions(assertions, request, response, envVars, collectionVariables, collectionPath);
mainWindow.webContents.send('main:assertion-results', {
mainWindow.webContents.send('main:run-request-event', {
type: 'assertion-results',
results: results,
itemUid: item.uid,
requestUid,
collectionUid
});
@ -221,9 +232,11 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
const testRuntime = new TestRuntime();
const testResults = testRuntime.runTests(testFile, request, response, envVars, collectionVariables, collectionPath);
mainWindow.webContents.send('main:test-results', {
mainWindow.webContents.send('main:run-request-event', {
type: 'test-results',
results: testResults.results,
itemUid: item.uid,
requestUid,
collectionUid
});
@ -253,9 +266,11 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
const assertRuntime = new AssertRuntime();
const results = assertRuntime.runAssertions(assertions, request, error.response, envVars, collectionVariables, collectionPath);
mainWindow.webContents.send('main:assertion-results', {
mainWindow.webContents.send('main:run-request-event', {
type: 'assertion-results',
results: results,
itemUid: item.uid,
requestUid,
collectionUid
});
@ -264,9 +279,11 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
const testRuntime = new TestRuntime();
const testResults = testRuntime.runTests(testFile, request, error.response, envVars, collectionVariables, collectionPath);
mainWindow.webContents.send('main:test-results', {
mainWindow.webContents.send('main:run-request-event', {
type: 'test-results',
results: testResults.results,
itemUid: item.uid,
requestUid,
collectionUid
});