bruno/renderer/providers/Store/reducer.js

182 lines
5.0 KiB
JavaScript
Raw Normal View History

2021-12-03 20:37:38 +01:00
import produce from 'immer';
2021-12-10 23:29:44 +01:00
import {nanoid} from 'nanoid';
import union from 'lodash/union';
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;
});
}
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;
});
}
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',
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) {
collection.items.push({
uid: nanoid(),
name: action.folderName,
2022-03-15 21:57:52 +01:00
type: 'folder',
items: [],
// todo: this will be autoassigned
depth: 1
2022-03-13 13:13:21 +01:00
});
draft.collectionsToSyncToIdb.push(collection.uid);
2022-03-13 13:13:21 +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);
if(collection) {
let flattenedItems = flattenItems(collection.items);
let item = findItem(flattenedItems, action.itemUid);
if(item) {
2022-01-23 13:38:01 +01:00
if(!item.draft) {
item.draft = cloneItem(item);
}
item.draft.request.url = action.url;
updateRequestTabAsChanged(draft.requestTabs, item.uid);
}
}
});
}
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);
if(collection) {
let flattenedItems = flattenItems(collection.items);
let item = findItem(flattenedItems, action.requestTab.id);
if(item) {
item.request.body.graphql.query = action.query;
}
}
});
}
case actions.ADD_NEW_GQL_REQUEST: {
return produce(state, (draft) => {
const uid = nanoid();
draft.requestTabs.push({
uid: uid,
name: 'New Tab',
2022-03-14 18:46:49 +01:00
type: 'graphql-request',
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: '{}'
}
}
},
collectionUid: null
2022-01-04 18:00:15 +01:00
});
draft.activeRequestTabUid = uid;
2022-01-04 18:00:15 +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;