2021-12-03 20:37:38 +01:00
|
|
|
import produce from 'immer';
|
2021-12-10 23:29:44 +01:00
|
|
|
import {nanoid} from 'nanoid';
|
2022-03-13 16:57:59 +01:00
|
|
|
import union from 'lodash/union';
|
2022-03-15 20:30:35 +01:00
|
|
|
import find from 'lodash/find';
|
2021-12-03 20:37:38 +01:00
|
|
|
import actions from './actions';
|
|
|
|
import {
|
|
|
|
flattenItems,
|
|
|
|
findItem,
|
2022-01-23 13:38:01 +01:00
|
|
|
cloneItem,
|
2022-03-13 18:22:14 +01:00
|
|
|
updateRequestTabAsChanged,
|
|
|
|
findCollectionByUid
|
2021-12-03 20:37:38 +01:00
|
|
|
} from './utils';
|
|
|
|
|
|
|
|
const reducer = (state, action) => {
|
|
|
|
switch (action.type) {
|
2022-02-03 18:55:38 +01:00
|
|
|
case actions.IDB_CONNECTION_READY: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
draft.idbConnection = action.connection;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-13 16:57:59 +01:00
|
|
|
case actions.IDB_COLLECTIONS_SYNC_STARTED: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
draft.collectionsToSyncToIdb = [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
case actions.IDB_COLLECTIONS_SYNC_ERROR: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
draft.collectionsToSyncToIdb = union(draft.collectionsToSyncToIdb, action.collectionUids);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-13 13:13:21 +01:00
|
|
|
case actions.LOAD_COLLECTIONS_FROM_IDB: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
draft.collections = action.collections;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-13 22:13:36 +01:00
|
|
|
case actions.SIDEBAR_COLLECTION_NEW_REQUEST: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
const collection = findCollectionByUid(draft.collections, action.collectionUid);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
const uid = nanoid();
|
|
|
|
const item = {
|
|
|
|
uid: uid,
|
|
|
|
name: action.requestName,
|
2022-03-14 18:46:49 +01:00
|
|
|
type: 'http-request',
|
2022-03-13 22:13:36 +01:00
|
|
|
request: {
|
|
|
|
method: 'GET',
|
|
|
|
url: 'https://reqbin.com/echo/get/json',
|
|
|
|
headers: [],
|
|
|
|
body: null
|
|
|
|
},
|
|
|
|
depth: 1
|
|
|
|
};
|
|
|
|
collection.items.push(item);
|
|
|
|
|
|
|
|
draft.requestTabs.push({
|
|
|
|
uid: item.uid,
|
|
|
|
name: item.name,
|
|
|
|
method: item.request.method,
|
|
|
|
collectionUid: collection.uid,
|
|
|
|
hasChanges: false
|
|
|
|
});
|
|
|
|
draft.activeRequestTabUid = uid;
|
|
|
|
draft.collectionsToSyncToIdb.push(collection.uid);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
case actions.SIDEBAR_COLLECTION_NEW_FOLDER: {
|
2022-03-13 13:13:21 +01:00
|
|
|
return produce(state, (draft) => {
|
2022-03-13 18:22:14 +01:00
|
|
|
const collection = findCollectionByUid(draft.collections, action.collectionUid);
|
2022-03-13 13:13:21 +01:00
|
|
|
|
|
|
|
if(collection) {
|
2022-03-13 22:13:36 +01:00
|
|
|
collection.items.push({
|
|
|
|
uid: nanoid(),
|
|
|
|
name: action.folderName,
|
2022-03-15 21:57:52 +01:00
|
|
|
type: 'folder',
|
2022-03-13 22:13:36 +01:00
|
|
|
items: [],
|
|
|
|
// todo: this will be autoassigned
|
|
|
|
depth: 1
|
2022-03-13 13:13:21 +01:00
|
|
|
});
|
2022-03-13 16:57:59 +01:00
|
|
|
draft.collectionsToSyncToIdb.push(collection.uid);
|
2022-03-13 13:13:21 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:11:27 +01:00
|
|
|
case actions.REQUEST_URL_CHANGED: {
|
|
|
|
return produce(state, (draft) => {
|
2022-03-13 18:22:14 +01:00
|
|
|
const collection = findCollectionByUid(draft.collections, action.collectionUid);
|
2022-01-18 17:11:27 +01:00
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
2022-03-15 20:30:35 +01:00
|
|
|
let item = findItem(flattenedItems, action.itemUid);
|
2022-01-18 17:11:27 +01:00
|
|
|
|
|
|
|
if(item) {
|
2022-01-23 13:38:01 +01:00
|
|
|
if(!item.draft) {
|
|
|
|
item.draft = cloneItem(item);
|
|
|
|
}
|
|
|
|
item.draft.request.url = action.url;
|
2022-03-15 20:30:35 +01:00
|
|
|
updateRequestTabAsChanged(draft.requestTabs, item.uid);
|
2022-01-18 17:11:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:25:22 +01:00
|
|
|
case actions.REQUEST_GQL_QUERY_CHANGED: {
|
|
|
|
return produce(state, (draft) => {
|
2022-03-13 18:22:14 +01:00
|
|
|
const collection = findCollectionByUid(draft.collections, action.collectionUid);
|
2022-01-18 17:25:22 +01:00
|
|
|
|
|
|
|
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-18 15:58:18 +01:00
|
|
|
case actions.ADD_NEW_GQL_REQUEST: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
const uid = nanoid();
|
|
|
|
draft.requestTabs.push({
|
2022-03-13 22:13:36 +01:00
|
|
|
uid: uid,
|
2022-01-18 15:58:18 +01:00
|
|
|
name: 'New Tab',
|
2022-03-14 18:46:49 +01:00
|
|
|
type: 'graphql-request',
|
2022-01-18 15:58:18 +01:00
|
|
|
request: {
|
2022-03-14 18:46:49 +01:00
|
|
|
method: 'GET',
|
2022-01-04 18:00:15 +01:00
|
|
|
url: 'https://api.spacex.land/graphql/',
|
|
|
|
body: {
|
|
|
|
graphql: {
|
|
|
|
query: '{}'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-03-13 22:13:36 +01:00
|
|
|
collectionUid: null
|
2022-01-04 18:00:15 +01:00
|
|
|
});
|
2022-03-13 22:13:36 +01:00
|
|
|
draft.activeRequestTabUid = uid;
|
2022-01-04 18:00:15 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-15 20:30:35 +01:00
|
|
|
case actions.HOTKEY_SAVE: {
|
|
|
|
return produce(state, (draft) => {
|
|
|
|
if(!draft.activeRequestTabUid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find request tab
|
|
|
|
const activeRequestTab = find(draft.requestTabs, (t) => t.uid === draft.activeRequestTabUid);
|
|
|
|
|
|
|
|
// resolve item, save and delete draft
|
|
|
|
if(activeRequestTab) {
|
|
|
|
const collection = findCollectionByUid(draft.collections, activeRequestTab.collectionUid);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
let flattenedItems = flattenItems(collection.items);
|
|
|
|
let item = findItem(flattenedItems, activeRequestTab.uid);
|
|
|
|
|
|
|
|
if(item && item.draft) {
|
|
|
|
item.name = item.draft.name;
|
|
|
|
item.request = item.draft.request;
|
|
|
|
item.draft = null;
|
|
|
|
activeRequestTab.hasChanges = false;
|
|
|
|
draft.collectionsToSyncToIdb.push(collection.uid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:37:38 +01:00
|
|
|
default: {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default reducer;
|