refactor: redux migration - new request

This commit is contained in:
Anoop M D
2022-03-18 21:10:42 +05:30
parent d90178240e
commit 1fe1247cf3
4 changed files with 58 additions and 47 deletions

View File

@@ -67,6 +67,13 @@ export const collectionsSlice = createSlice({
});
}
},
_newRequest: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload.collectionUid);
if(collection) {
collection.items.push(action.payload.item);
}
},
collectionClicked: (state, action) => {
const collection = findCollectionByUid(state.collections, action.payload);
@@ -98,6 +105,7 @@ export const {
_responseReceived,
_saveRequest,
_newFolder,
_newRequest,
collectionClicked,
requestUrlChanged,
} = collectionsSlice.actions;
@@ -180,4 +188,43 @@ export const newFolder = (folderName, collectionUid) => (dispatch, getState) =>
}
};
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)
});
}
});
};
export default collectionsSlice.reducer;