2022-03-17 22:41:50 +01:00
|
|
|
import { nanoid } from 'nanoid';
|
2022-03-18 14:08:49 +01:00
|
|
|
import cloneDeep from 'lodash/cloneDeep';
|
2022-03-17 22:18:41 +01:00
|
|
|
import { createSlice } from '@reduxjs/toolkit'
|
2022-03-17 22:41:50 +01:00
|
|
|
import { getCollectionsFromIdb, saveCollectionToIdb } from 'utils/idb';
|
2022-03-18 10:15:27 +01:00
|
|
|
import { sendNetworkRequest } from 'utils/network';
|
2022-03-18 14:08:49 +01:00
|
|
|
import { findCollectionByUid, findItemInCollection, cloneItem, transformCollectionToSaveToIdb } from 'utils/collections';
|
2022-03-17 22:18:41 +01:00
|
|
|
|
2022-03-17 22:41:50 +01:00
|
|
|
// todo: errors should be tracked in each slice and displayed as toasts
|
|
|
|
|
2022-03-17 22:18:41 +01:00
|
|
|
const initialState = {
|
|
|
|
collections: []
|
|
|
|
};
|
|
|
|
|
|
|
|
export const collectionsSlice = createSlice({
|
2022-03-18 00:13:35 +01:00
|
|
|
name: 'collections',
|
2022-03-17 22:18:41 +01:00
|
|
|
initialState,
|
|
|
|
reducers: {
|
2022-03-17 22:41:50 +01:00
|
|
|
_loadCollections: (state, action) => {
|
2022-03-17 22:18:41 +01:00
|
|
|
state.collections = action.payload;
|
2022-03-17 22:29:10 +01:00
|
|
|
},
|
2022-03-17 22:41:50 +01:00
|
|
|
_createCollection: (state, action) => {
|
|
|
|
state.collections.push(action.payload);
|
|
|
|
},
|
2022-03-18 10:15:27 +01:00
|
|
|
_requestSent: (state, action) => {
|
|
|
|
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
const item = findItemInCollection(collection, action.payload.itemUid);
|
|
|
|
if(item) {
|
|
|
|
item.response = item.response || {};
|
|
|
|
item.response.state = 'sending';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_responseReceived: (state, action) => {
|
|
|
|
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
const item = findItemInCollection(collection, action.payload.itemUid);
|
|
|
|
if(item) {
|
|
|
|
item.response = action.payload.response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-03-18 14:08:49 +01:00
|
|
|
_saveRequest: (state, action) => {
|
|
|
|
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
const item = findItemInCollection(collection, action.payload.itemUid);
|
|
|
|
|
|
|
|
if(item && item.draft) {
|
|
|
|
item.name = item.draft.name;
|
|
|
|
item.request = item.draft.request;
|
|
|
|
item.draft = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-03-18 15:15:20 +01:00
|
|
|
_newFolder: (state, action) => {
|
|
|
|
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
collection.items.push({
|
|
|
|
uid: nanoid(),
|
|
|
|
name: action.payload.folderName,
|
|
|
|
type: 'folder',
|
|
|
|
items: []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2022-03-18 16:40:42 +01:00
|
|
|
_newRequest: (state, action) => {
|
|
|
|
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
collection.items.push(action.payload.item);
|
|
|
|
}
|
|
|
|
},
|
2022-03-17 22:29:10 +01:00
|
|
|
collectionClicked: (state, action) => {
|
|
|
|
const collection = findCollectionByUid(state.collections, action.payload);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
collection.collapsed = !collection.collapsed;
|
|
|
|
}
|
2022-03-18 14:08:49 +01:00
|
|
|
},
|
|
|
|
requestUrlChanged: (state, action) => {
|
|
|
|
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
const item = findItemInCollection(collection, action.payload.itemUid);
|
|
|
|
|
|
|
|
if(item) {
|
|
|
|
if(!item.draft) {
|
|
|
|
item.draft = cloneItem(item);
|
|
|
|
}
|
|
|
|
item.draft.request.url = action.payload.url;
|
|
|
|
}
|
|
|
|
}
|
2022-03-17 22:18:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-17 22:41:50 +01:00
|
|
|
export const {
|
|
|
|
_loadCollections,
|
|
|
|
_createCollection,
|
2022-03-18 10:15:27 +01:00
|
|
|
_requestSent,
|
|
|
|
_responseReceived,
|
2022-03-18 14:08:49 +01:00
|
|
|
_saveRequest,
|
2022-03-18 15:15:20 +01:00
|
|
|
_newFolder,
|
2022-03-18 16:40:42 +01:00
|
|
|
_newRequest,
|
2022-03-18 14:08:49 +01:00
|
|
|
collectionClicked,
|
|
|
|
requestUrlChanged,
|
2022-03-17 22:41:50 +01:00
|
|
|
} = collectionsSlice.actions;
|
2022-03-17 22:18:41 +01:00
|
|
|
|
|
|
|
export const loadCollectionsFromIdb = () => (dispatch) => {
|
|
|
|
getCollectionsFromIdb(window.__idb)
|
2022-03-17 22:41:50 +01:00
|
|
|
.then((collections) => dispatch(_loadCollections(collections)))
|
|
|
|
.catch((err) => console.log(err));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const createCollection = (collectionName) => (dispatch) => {
|
|
|
|
const newCollection = {
|
|
|
|
uid: nanoid(),
|
|
|
|
name: collectionName,
|
|
|
|
items: [],
|
|
|
|
environments: [],
|
|
|
|
userId: null
|
|
|
|
};
|
|
|
|
|
|
|
|
saveCollectionToIdb(window.__idb, newCollection)
|
|
|
|
.then(() => dispatch(_createCollection(newCollection)))
|
2022-03-17 22:18:41 +01:00
|
|
|
.catch((err) => console.log(err));
|
|
|
|
};
|
|
|
|
|
2022-03-18 10:15:27 +01:00
|
|
|
export const sendRequest = (item, collectionUid) => (dispatch) => {
|
|
|
|
dispatch(_requestSent({
|
|
|
|
itemUid: item.uid,
|
|
|
|
collectionUid: collectionUid
|
|
|
|
}));
|
|
|
|
sendNetworkRequest(item)
|
|
|
|
.then((response) => dispatch(_responseReceived({
|
|
|
|
itemUid: item.uid,
|
|
|
|
collectionUid: collectionUid,
|
|
|
|
response: response
|
|
|
|
})))
|
|
|
|
.catch((err) => console.log(err));
|
|
|
|
};
|
|
|
|
|
2022-03-18 14:08:49 +01:00
|
|
|
export const saveRequest = (itemUid, collectionUid) => (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const collection = findCollectionByUid(state.collections.collections, collectionUid);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
const collectionCopy = cloneDeep(collection);
|
|
|
|
const collectionToSave = transformCollectionToSaveToIdb(collectionCopy);
|
|
|
|
|
|
|
|
saveCollectionToIdb(window.__idb, collectionToSave)
|
|
|
|
.then(() => {
|
|
|
|
dispatch(_saveRequest({
|
|
|
|
itemUid: itemUid,
|
|
|
|
collectionUid: collectionUid
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
.catch((err) => console.log(err));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-18 15:15:20 +01:00
|
|
|
export const newFolder = (folderName, collectionUid) => (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const collection = findCollectionByUid(state.collections.collections, collectionUid);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
const collectionCopy = cloneDeep(collection);
|
|
|
|
collectionCopy.items.push({
|
|
|
|
uid: nanoid(),
|
|
|
|
name: folderName,
|
|
|
|
type: 'folder',
|
|
|
|
items: []
|
|
|
|
});
|
|
|
|
const collectionToSave = transformCollectionToSaveToIdb(collectionCopy);
|
|
|
|
|
|
|
|
saveCollectionToIdb(window.__idb, collectionToSave)
|
|
|
|
.then(() => {
|
|
|
|
dispatch(_newFolder({
|
|
|
|
folderName: folderName,
|
|
|
|
collectionUid: collectionUid
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
.catch((err) => console.log(err));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-18 16:40:42 +01:00
|
|
|
export const newHttpRequest = (requestName, collectionUid) => (dispatch, getState) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const state = getState();
|
|
|
|
const collection = findCollectionByUid(state.collections.collections, collectionUid);
|
|
|
|
|
|
|
|
if(collection) {
|
|
|
|
const collectionCopy = cloneDeep(collection);
|
|
|
|
const uid = nanoid();
|
|
|
|
const item = {
|
|
|
|
uid: uid,
|
|
|
|
name: requestName,
|
|
|
|
type: 'http-request',
|
|
|
|
request: {
|
|
|
|
method: 'GET',
|
|
|
|
url: 'https://reqbin.com/echo/get/json',
|
|
|
|
headers: [],
|
|
|
|
body: null
|
|
|
|
}
|
|
|
|
};
|
|
|
|
collectionCopy.items.push(item);
|
|
|
|
const collectionToSave = transformCollectionToSaveToIdb(collectionCopy);
|
|
|
|
|
|
|
|
saveCollectionToIdb(window.__idb, collectionToSave)
|
|
|
|
.then(() => {
|
|
|
|
Promise.resolve(dispatch(_newRequest({
|
|
|
|
item: item,
|
|
|
|
collectionUid: collectionUid
|
|
|
|
})))
|
|
|
|
.then((val) => resolve(val))
|
|
|
|
.catch((err) => reject(err));
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
console.log(err)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-03-17 22:18:41 +01:00
|
|
|
export default collectionsSlice.reducer;
|