2018-06-25 17:42:50 +00:00
|
|
|
import { createSelector } from 'reselect';
|
2018-06-27 05:13:48 +00:00
|
|
|
import * as models from '../../models';
|
2016-11-26 08:29:16 +00:00
|
|
|
|
2016-11-25 23:09:17 +00:00
|
|
|
// ~~~~~~~~~ //
|
|
|
|
// Selectors //
|
|
|
|
// ~~~~~~~~~ //
|
|
|
|
|
|
|
|
export const selectEntitiesLists = createSelector(
|
|
|
|
state => state.entities,
|
|
|
|
entities => {
|
|
|
|
const entitiesLists = {};
|
|
|
|
|
|
|
|
for (const k of Object.keys(entities)) {
|
|
|
|
const entityMap = entities[k];
|
|
|
|
entitiesLists[k] = Object.keys(entityMap).map(id => entityMap[id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return entitiesLists;
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2016-11-25 23:09:17 +00:00
|
|
|
);
|
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
export const selectEntitiesChildrenMap = createSelector(selectEntitiesLists, entities => {
|
|
|
|
const parentLookupMap = {};
|
|
|
|
for (const k of Object.keys(entities)) {
|
|
|
|
for (const e of entities[k]) {
|
|
|
|
if (!e.parentId) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-23 22:36:06 +00:00
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
if (parentLookupMap[e.parentId]) {
|
|
|
|
parentLookupMap[e.parentId].push(e);
|
|
|
|
} else {
|
|
|
|
parentLookupMap[e.parentId] = [e];
|
2019-04-18 00:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 22:54:07 +00:00
|
|
|
}
|
2019-04-18 00:50:03 +00:00
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
return parentLookupMap;
|
|
|
|
});
|
2019-04-18 00:50:03 +00:00
|
|
|
|
2020-06-08 22:32:49 +00:00
|
|
|
export const selectSettings = createSelector(selectEntitiesLists, entities => {
|
|
|
|
return entities.settings[0] || models.settings.init();
|
|
|
|
});
|
|
|
|
|
2016-11-25 23:09:17 +00:00
|
|
|
export const selectActiveWorkspace = createSelector(
|
|
|
|
state => selectEntitiesLists(state).workspaces,
|
|
|
|
state => state.entities,
|
|
|
|
state => state.global.activeWorkspaceId,
|
|
|
|
(workspaces, entities, activeWorkspaceId) => {
|
|
|
|
return entities.workspaces[activeWorkspaceId] || workspaces[0];
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2016-11-25 23:09:17 +00:00
|
|
|
);
|
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
export const selectActiveWorkspaceMeta = createSelector(
|
|
|
|
selectActiveWorkspace,
|
|
|
|
selectEntitiesLists,
|
|
|
|
(activeWorkspace, entities) => {
|
|
|
|
const id = activeWorkspace ? activeWorkspace._id : 'n/a';
|
|
|
|
return entities.workspaceMetas.find(m => m.parentId === id);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-06-08 22:32:49 +00:00
|
|
|
export const selectActiveEnvironment = createSelector(
|
|
|
|
selectActiveWorkspaceMeta,
|
|
|
|
selectEntitiesLists,
|
|
|
|
(meta, entities) => {
|
|
|
|
if (!meta) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entities.environments.find(e => e._id === meta.activeEnvironmentId) || null;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2017-10-31 18:05:35 +00:00
|
|
|
export const selectActiveWorkspaceClientCertificates = createSelector(
|
|
|
|
selectEntitiesLists,
|
|
|
|
selectActiveWorkspace,
|
|
|
|
(entities, activeWorkspace) => {
|
2018-10-17 16:42:33 +00:00
|
|
|
return entities.clientCertificates.filter(c => c.parentId === activeWorkspace._id);
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2017-10-31 18:05:35 +00:00
|
|
|
);
|
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
export const selectActiveGitRepository = createSelector(
|
2017-02-01 03:01:27 +00:00
|
|
|
selectEntitiesLists,
|
2020-04-26 20:33:39 +00:00
|
|
|
selectActiveWorkspaceMeta,
|
|
|
|
(entities, activeWorkspaceMeta) => {
|
|
|
|
if (!activeWorkspaceMeta) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = activeWorkspaceMeta ? activeWorkspaceMeta.gitRepositoryId : 'n/a';
|
|
|
|
const repo = entities.gitRepositories.find(r => r._id === id);
|
|
|
|
return repo || null;
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2017-02-01 03:01:27 +00:00
|
|
|
);
|
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
export const selectCollapsedRequestGroups = createSelector(selectEntitiesLists, entities => {
|
|
|
|
const collapsed = {};
|
2017-06-15 17:08:24 +00:00
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
// Default all to collapsed
|
|
|
|
for (const requestGroup of entities.requestGroups) {
|
|
|
|
collapsed[requestGroup._id] = true;
|
|
|
|
}
|
2017-06-15 17:08:24 +00:00
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
// Update those that have metadata (not all do)
|
|
|
|
for (const meta of entities.requestGroupMetas) {
|
|
|
|
collapsed[meta.parentId] = meta.collapsed;
|
|
|
|
}
|
2018-10-17 16:42:33 +00:00
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
return collapsed;
|
|
|
|
});
|
2016-12-01 01:50:59 +00:00
|
|
|
|
2019-04-23 22:36:06 +00:00
|
|
|
export const selectActiveWorkspaceEntities = createSelector(
|
|
|
|
selectActiveWorkspace,
|
|
|
|
selectEntitiesChildrenMap,
|
|
|
|
(activeWorkspace, childrenMap) => {
|
|
|
|
const descendants = [activeWorkspace];
|
|
|
|
const addChildrenOf = parent => {
|
2019-05-10 01:18:34 +00:00
|
|
|
// Don't add children of requests (eg. auth requests)
|
|
|
|
if (parent.type === models.request.type) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-04-23 22:36:06 +00:00
|
|
|
const children = childrenMap[parent._id] || [];
|
|
|
|
for (const child of children) {
|
|
|
|
descendants.push(child);
|
|
|
|
addChildrenOf(child);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Kick off the recursion
|
|
|
|
addChildrenOf(activeWorkspace);
|
|
|
|
|
|
|
|
return descendants;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
export const selectPinnedRequests = createSelector(selectEntitiesLists, entities => {
|
|
|
|
const pinned = {};
|
2019-05-07 14:46:35 +00:00
|
|
|
|
2020-10-28 22:44:37 +00:00
|
|
|
const requests = [...entities.requests, ...entities.grpcRequests];
|
|
|
|
const requestMetas = [...entities.requestMetas, ...entities.grpcRequestMetas];
|
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
// Default all to unpinned
|
2020-10-28 22:44:37 +00:00
|
|
|
for (const request of requests) {
|
2020-05-14 22:54:07 +00:00
|
|
|
pinned[request._id] = false;
|
|
|
|
}
|
2019-05-07 14:46:35 +00:00
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
// Update those that have metadata (not all do)
|
2020-10-28 22:44:37 +00:00
|
|
|
for (const meta of requestMetas) {
|
2020-05-14 22:54:07 +00:00
|
|
|
pinned[meta.parentId] = meta.pinned;
|
|
|
|
}
|
2019-05-07 14:46:35 +00:00
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
return pinned;
|
|
|
|
});
|
2019-05-07 14:46:35 +00:00
|
|
|
|
2016-11-26 07:33:55 +00:00
|
|
|
export const selectWorkspaceRequestsAndRequestGroups = createSelector(
|
2019-04-23 22:36:06 +00:00
|
|
|
selectActiveWorkspaceEntities,
|
|
|
|
entities => {
|
|
|
|
return entities.filter(
|
|
|
|
e => e.type === models.request.type || e.type === models.requestGroup.type,
|
|
|
|
);
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2016-11-26 07:33:55 +00:00
|
|
|
);
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
|
|
export const selectActiveRequest = createSelector(
|
|
|
|
state => state.entities,
|
|
|
|
selectActiveWorkspaceMeta,
|
|
|
|
(entities, workspaceMeta) => {
|
|
|
|
const id = workspaceMeta ? workspaceMeta.activeRequestId : 'n/a';
|
2020-11-02 00:55:22 +00:00
|
|
|
return entities.requests[id] || entities.grpcRequests[id] || null;
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2016-12-01 01:50:59 +00:00
|
|
|
);
|
|
|
|
|
2017-08-22 22:30:57 +00:00
|
|
|
export const selectActiveCookieJar = createSelector(
|
|
|
|
selectEntitiesLists,
|
|
|
|
selectActiveWorkspace,
|
|
|
|
(entities, workspace) => {
|
2018-10-17 16:42:33 +00:00
|
|
|
const cookieJar = entities.cookieJars.find(cj => cj.parentId === workspace._id);
|
2017-08-22 22:30:57 +00:00
|
|
|
return cookieJar || null;
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2017-08-22 22:30:57 +00:00
|
|
|
);
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
export const selectActiveOAuth2Token = createSelector(
|
|
|
|
selectEntitiesLists,
|
|
|
|
selectActiveWorkspaceMeta,
|
|
|
|
(entities, workspaceMeta) => {
|
|
|
|
const id = workspaceMeta ? workspaceMeta.activeRequestId : 'n/a';
|
|
|
|
return entities.oAuth2Tokens.find(t => t.parentId === id);
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2017-03-23 22:10:42 +00:00
|
|
|
);
|
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
export const selectUnseenWorkspaces = createSelector(selectEntitiesLists, entities => {
|
|
|
|
const { workspaces, workspaceMetas } = entities;
|
|
|
|
return workspaces.filter(workspace => {
|
|
|
|
const meta = workspaceMetas.find(m => m.parentId === workspace._id);
|
|
|
|
return !!(meta && !meta.hasSeen);
|
|
|
|
});
|
|
|
|
});
|
2017-07-27 22:59:07 +00:00
|
|
|
|
2016-12-01 01:50:59 +00:00
|
|
|
export const selectActiveRequestMeta = createSelector(
|
|
|
|
selectActiveRequest,
|
|
|
|
selectEntitiesLists,
|
|
|
|
(activeRequest, entities) => {
|
|
|
|
const id = activeRequest ? activeRequest._id : 'n/a';
|
|
|
|
return entities.requestMetas.find(m => m.parentId === id);
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2016-12-01 01:50:59 +00:00
|
|
|
);
|
2017-07-17 18:20:38 +00:00
|
|
|
|
|
|
|
export const selectActiveRequestResponses = createSelector(
|
|
|
|
selectActiveRequest,
|
|
|
|
selectEntitiesLists,
|
2020-06-08 22:32:49 +00:00
|
|
|
selectActiveEnvironment,
|
|
|
|
selectSettings,
|
|
|
|
(activeRequest, entities, activeEnvironment, settings) => {
|
2017-07-17 18:20:38 +00:00
|
|
|
const requestId = activeRequest ? activeRequest._id : 'n/a';
|
2020-02-11 18:45:47 +00:00
|
|
|
|
|
|
|
// Filter responses down if the setting is enabled
|
2017-07-17 18:20:38 +00:00
|
|
|
return entities.responses
|
2020-01-22 19:23:19 +00:00
|
|
|
.filter(response => {
|
|
|
|
const requestMatches = requestId === response.parentId;
|
|
|
|
|
2020-02-11 18:45:47 +00:00
|
|
|
if (settings.filterResponsesByEnv) {
|
2020-06-08 22:32:49 +00:00
|
|
|
const activeEnvironmentId = activeEnvironment ? activeEnvironment._id : null;
|
2020-02-11 18:45:47 +00:00
|
|
|
const environmentMatches = response.environmentId === activeEnvironmentId;
|
|
|
|
return requestMatches && environmentMatches;
|
|
|
|
} else {
|
|
|
|
return requestMatches;
|
|
|
|
}
|
2020-01-22 19:23:19 +00:00
|
|
|
})
|
2018-06-25 17:42:50 +00:00
|
|
|
.sort((a, b) => (a.created > b.created ? -1 : 1));
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2017-07-17 18:20:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
export const selectActiveResponse = createSelector(
|
|
|
|
selectActiveRequestMeta,
|
|
|
|
selectActiveRequestResponses,
|
|
|
|
(activeRequestMeta, responses) => {
|
2018-10-17 16:42:33 +00:00
|
|
|
const activeResponseId = activeRequestMeta ? activeRequestMeta.activeResponseId : 'n/a';
|
|
|
|
const activeResponse = responses.find(response => response._id === activeResponseId);
|
2017-07-17 18:20:38 +00:00
|
|
|
|
|
|
|
if (activeResponse) {
|
|
|
|
return activeResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
return responses[0] || null;
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2017-07-17 18:20:38 +00:00
|
|
|
);
|
2019-04-18 00:50:03 +00:00
|
|
|
|
2020-07-01 21:17:14 +00:00
|
|
|
export const selectActiveUnitTestResult = createSelector(
|
|
|
|
selectEntitiesLists,
|
|
|
|
selectActiveWorkspace,
|
|
|
|
(entities, activeWorkspace) => {
|
|
|
|
let recentResult = null;
|
|
|
|
for (const r of entities.unitTestResults) {
|
|
|
|
if (r.parentId !== activeWorkspace._id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!recentResult) {
|
|
|
|
recentResult = r;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r.created > recentResult.created) {
|
|
|
|
recentResult = r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return recentResult;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export const selectActiveUnitTestSuite = createSelector(
|
|
|
|
selectEntitiesLists,
|
|
|
|
selectActiveWorkspaceMeta,
|
|
|
|
(entities, activeWorkspaceMeta) => {
|
|
|
|
if (!activeWorkspaceMeta) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = activeWorkspaceMeta.activeUnitTestSuiteId;
|
|
|
|
return entities.unitTestSuites.find(s => s._id === id) || null;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export const selectActiveUnitTests = createSelector(
|
|
|
|
selectEntitiesLists,
|
|
|
|
selectActiveUnitTestSuite,
|
|
|
|
(entities, activeUnitTestSuite) => {
|
|
|
|
if (!activeUnitTestSuite) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return entities.unitTests.filter(s => s.parentId === activeUnitTestSuite._id);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export const selectActiveUnitTestSuites = createSelector(
|
|
|
|
selectEntitiesLists,
|
|
|
|
selectActiveWorkspace,
|
|
|
|
(entities, activeWorkspace) => {
|
|
|
|
return entities.unitTestSuites.filter(s => s.parentId === activeWorkspace._id);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-05-14 22:54:07 +00:00
|
|
|
export const selectSyncItems = createSelector(selectActiveWorkspaceEntities, workspaceEntities =>
|
|
|
|
workspaceEntities.filter(models.canSync).map(doc => ({
|
|
|
|
key: doc._id,
|
|
|
|
name: doc.name || '',
|
|
|
|
document: doc,
|
|
|
|
})),
|
2019-04-18 00:50:03 +00:00
|
|
|
);
|