2021-12-03 20:37:38 +01:00
|
|
|
import produce from 'immer';
|
2021-12-10 23:29:44 +01:00
|
|
|
import {nanoid} from 'nanoid';
|
2021-12-03 20:37:38 +01:00
|
|
|
import find from 'lodash/find';
|
|
|
|
import filter from 'lodash/filter';
|
2022-01-09 18:05:48 +01:00
|
|
|
import last from 'lodash/last';
|
2021-12-03 20:37:38 +01:00
|
|
|
import actions from './actions';
|
|
|
|
import {
|
|
|
|
flattenItems,
|
|
|
|
findItem,
|
|
|
|
isItemARequest,
|
|
|
|
itemIsOpenedInTabs
|
|
|
|
} from './utils';
|
|
|
|
|
|
|
|
const reducer = (state, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case actions.SIDEBAR_COLLECTION_CLICK: {
|
|
|
|
return produce(state, (draft) => {
|
2021-12-09 17:44:49 +01:00
|
|
|
const collection = find(draft.collections, (c) => c.id === action.id);
|
2021-12-03 20:37:38 +01:00
|
|
|
|
2021-12-09 17:44:49 +01:00
|
|
|
if(collection) {
|
|
|
|
collection.collapsed = !collection.collapsed;
|
2021-12-03 20:37:38 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
case actions.SIDEBAR_COLLECTION_ITEM_CLICK: {
|
|
|
|
return produce(state, (draft) => {
|
2021-12-09 17:44:49 +01:00
|
|
|
const collection = find(draft.collections, (c) => c.id === action.collectionId);
|
2021-12-03 20:37:38 +01:00
|
|
|
|
2021-12-09 17:44:49 +01:00
|
|
|
if(collection) {
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
2021-12-03 20:37:38 +01:00
|
|
|
let item = findItem(flattenedItems, action.itemId);
|
|
|
|
|
|
|
|
if(item) {
|
|
|
|
item.collapsed = !item.collapsed;
|
|
|
|
|
|
|
|
if(isItemARequest(item)) {
|
|
|
|
if(itemIsOpenedInTabs(item, draft.requestTabs)) {
|
|
|
|
draft.activeRequestTabId = item.id;
|
|
|
|
} else {
|
|
|
|
draft.requestTabs.push({
|
|
|
|
id: item.id,
|
|
|
|
name: item.name,
|
|
|
|
method: item.request.method,
|
2021-12-09 17:44:49 +01:00
|
|
|
collectionId: collection.id
|
2021-12-03 20:37:38 +01:00
|
|
|
});
|
|
|
|
draft.activeRequestTabId = item.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
case actions.REQUEST_TAB_CLICK: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
draft.activeRequestTabId = action.requestTab.id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:11:27 +01:00
|
|
|
case actions.REQUEST_URL_CHANGED: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
const collection = find(draft.collections, (c) => c.id === action.collectionId);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
|
|
|
let item = findItem(flattenedItems, action.requestTab.id);
|
|
|
|
|
|
|
|
if(item) {
|
|
|
|
item.request.url = action.url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:25:22 +01:00
|
|
|
case actions.REQUEST_GQL_QUERY_CHANGED: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
const collection = find(draft.collections, (c) => c.id === action.collectionId);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
|
|
|
let item = findItem(flattenedItems, action.requestTab.id);
|
|
|
|
|
|
|
|
if(item) {
|
|
|
|
item.request.body.graphql.query = action.query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-04 18:00:15 +01:00
|
|
|
case actions.ADD_NEW_HTTP_REQUEST: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
const uid = nanoid();
|
|
|
|
draft.requestTabs.push({
|
|
|
|
id: uid,
|
2022-01-07 16:41:04 +01:00
|
|
|
name: 'New Tab',
|
2022-01-04 18:00:15 +01:00
|
|
|
method: 'GET',
|
|
|
|
request: {
|
2022-01-18 15:58:18 +01:00
|
|
|
type: 'http',
|
|
|
|
url: 'https://api.spacex.land/graphql/',
|
|
|
|
body: {}
|
|
|
|
},
|
|
|
|
collectionId: null
|
|
|
|
});
|
|
|
|
draft.activeRequestTabId = uid;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
case actions.ADD_NEW_GQL_REQUEST: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
const uid = nanoid();
|
|
|
|
draft.requestTabs.push({
|
|
|
|
id: uid,
|
|
|
|
name: 'New Tab',
|
|
|
|
method: 'GET',
|
|
|
|
request: {
|
|
|
|
type: 'graphql',
|
2022-01-04 18:00:15 +01:00
|
|
|
url: 'https://api.spacex.land/graphql/',
|
|
|
|
body: {
|
|
|
|
graphql: {
|
|
|
|
query: '{}'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
collectionId: null
|
|
|
|
});
|
|
|
|
draft.activeRequestTabId = uid;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-18 15:58:18 +01:00
|
|
|
case actions.SEND_REQUEST: {
|
2021-12-09 17:44:49 +01:00
|
|
|
return produce(state, (draft) => {
|
|
|
|
const collection = find(draft.collections, (c) => c.id === action.collectionId);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
|
|
|
let item = findItem(flattenedItems, action.requestTab.id);
|
|
|
|
|
2022-01-18 15:58:18 +01:00
|
|
|
if(item) {
|
|
|
|
item.response = item.response || {};
|
|
|
|
item.response.state = 'queued';
|
|
|
|
draft.requestQueuedToSend = {
|
|
|
|
collectionId: action.collectionId,
|
|
|
|
request: item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
case actions.SENDING_REQUEST: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
const collection = find(draft.collections, (c) => c.id === action.collectionId);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
|
|
|
let item = findItem(flattenedItems, action.request.id);
|
|
|
|
|
|
|
|
if(item) {
|
|
|
|
item.response.state = 'sending';
|
|
|
|
draft.requestQueuedToSend = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
case actions.RESPONSE_RECEIVED: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
const collection = find(draft.collections, (c) => c.id === action.collectionId);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
|
|
|
let item = findItem(flattenedItems, action.request.id);
|
|
|
|
|
2021-12-09 17:44:49 +01:00
|
|
|
if(item) {
|
|
|
|
item.response = action.response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:37:38 +01:00
|
|
|
case actions.REQUEST_TAB_CLOSE: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
draft.requestTabs = filter(draft.requestTabs, (rt) => rt.id !== action.requestTab.id);
|
|
|
|
|
|
|
|
if(draft.requestTabs && draft.requestTabs.length) {
|
2022-01-09 18:05:48 +01:00
|
|
|
// todo: closing tab needs to focus on the right adjacent tab
|
|
|
|
draft.activeRequestTabId = last(draft.requestTabs).id;
|
2021-12-03 20:37:38 +01:00
|
|
|
} else {
|
|
|
|
draft.activeRequestTabId = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-10 23:29:44 +01:00
|
|
|
case actions.ADD_REQUEST: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
const collection = find(draft.collections, (c) => c.id === action.collectionId);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
|
|
|
let item = findItem(flattenedItems, action.itemId);
|
|
|
|
|
|
|
|
if(item) {
|
|
|
|
if(!isItemARequest(item)) {
|
2021-12-12 19:11:36 +01:00
|
|
|
let newRequest = {
|
2021-12-10 23:29:44 +01:00
|
|
|
"id": nanoid(),
|
|
|
|
"depth": 2,
|
|
|
|
"name": "Capsules 2",
|
|
|
|
"request": {
|
2022-01-18 15:58:18 +01:00
|
|
|
"type": "graphql",
|
2021-12-10 23:29:44 +01:00
|
|
|
"url": "https://api.spacex.land/graphql/",
|
|
|
|
"method": "POST",
|
|
|
|
"headers": [],
|
|
|
|
"body": {
|
|
|
|
"mimeType": "application/graphql",
|
|
|
|
"graphql": {
|
|
|
|
"query": "{\n launchesPast(limit: 10) {\n mission_name\n launch_date_local\n launch_site {\n site_name_long\n }\n links {\n article_link\n video_link\n }\n rocket {\n rocket_name\n first_stage {\n cores {\n flight\n core {\n reuse_count\n status\n }\n }\n }\n second_stage {\n payloads {\n payload_type\n payload_mass_kg\n payload_mass_lbs\n }\n }\n }\n ships {\n name\n home_port\n image\n }\n }\n}",
|
|
|
|
"variables": ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"response": null
|
2021-12-12 19:11:36 +01:00
|
|
|
};
|
|
|
|
draft.activeRequestTabId = newRequest.id;
|
|
|
|
item.items.push(newRequest);
|
|
|
|
draft.requestTabs.push({
|
|
|
|
id: newRequest.id,
|
|
|
|
name: newRequest.name,
|
|
|
|
method: newRequest.request.method,
|
|
|
|
collectionId: collection.id
|
2021-12-10 23:29:44 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:37:38 +01:00
|
|
|
default: {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default reducer;
|