refactor: old store cleanup

This commit is contained in:
Anoop M D 2022-03-18 21:20:11 +05:30
parent 1fe1247cf3
commit a79593f951
8 changed files with 10 additions and 189 deletions

View File

@ -1,7 +1,5 @@
import React, { useState, useEffect } from 'react';
import find from 'lodash/find';
import { useStore } from 'providers/Store';
import actions from 'providers/Store/actions';
import { useSelector, useDispatch } from 'react-redux';
import QueryUrl from 'components/QueryUrl';
import GraphQLRequestPane from 'components/GraphQLRequestPane';
@ -23,7 +21,6 @@ const RequestTabPanel = () => {
const activeTabUid = useSelector((state) => state.tabs.activeTabUid);
const collections = useSelector((state) => state.collections.collections);
const dispatch = useDispatch();
const [store, storeDispatch] = useStore();
let asideWidth = 270;
let {
@ -61,17 +58,12 @@ const RequestTabPanel = () => {
}, [dragging, leftPaneWidth]);
const onGraphqlQueryChange = (value) => {
storeDispatch({
type: actions.REQUEST_GQL_QUERY_CHANGED,
query: value,
requestTab: focusedTab,
collectionUid: collection ? collection.uid : null
});
// todo
};
if(!activeTabUid) {
return (
<Welcome dispatch={storeDispatch} actions={actions}/>
<Welcome/>
);
}
@ -105,11 +97,7 @@ const RequestTabPanel = () => {
};
const runQuery = async () => {
storeDispatch({
type: actions.SEND_REQUEST,
requestTab: focusedTab,
collectionUid: collection ? collection.uid : null
});
// todo
};
const sendNetworkRequest = async () => {

View File

@ -2,18 +2,12 @@ import React from 'react';
import { IconPlus, IconUpload } from '@tabler/icons';
import StyledWrapper from './StyledWrapper';
const Welcome = ({dispatch, actions}) => {
const Welcome = () => {
const newGraphqlRequest = () => {
dispatch({
type: actions.ADD_NEW_GQL_REQUEST,
requestType: 'graphql'
});
// todo
};
const newHttpRequest = () => {
dispatch({
type: actions.ADD_NEW_HTTP_REQUEST,
requestType: 'http'
});
// todo
};
return (

View File

@ -1,4 +1,3 @@
import { StoreProvider } from 'providers/Store';
import { HotkeysProvider } from 'providers/Hotkeys';
import { AuthProvider } from 'providers/Auth';
import { AppProvider } from 'providers/App';
@ -27,11 +26,9 @@ function MyApp({ Component, pageProps }) {
<AuthProvider>
<Provider store={ReduxStore}>
<AppProvider>
<StoreProvider>
<HotkeysProvider>
<Component {...pageProps} />
</HotkeysProvider>
</StoreProvider>
<HotkeysProvider>
<Component {...pageProps} />
</HotkeysProvider>
</AppProvider>
</Provider>
</AuthProvider>

View File

@ -1,7 +0,0 @@
const REQUEST_GQL_QUERY_CHANGED = "REQUEST_GQL_QUERY_CHANGED";
const ADD_NEW_GQL_REQUEST = "ADD_NEW_GQL_REQUEST";
export default {
REQUEST_GQL_QUERY_CHANGED,
ADD_NEW_GQL_REQUEST,
};

View File

@ -1,41 +0,0 @@
import isArray from 'lodash/isArray';
export const saveCollectionToIdb = (connection, collection) => {
return new Promise((resolve, reject) => {
connection
.then((db) => {
let tx = db.transaction(`collection`, 'readwrite');
let collectionStore = tx.objectStore('collection');
if(isArray(collection)) {
for(let c of collection) {
collectionStore.put(c);
}
} else {
collectionStore.put(collection);
}
resolve();
})
.catch((err) => reject(err));
});
};
export const getCollectionsFromIdb = (connection) => {
return new Promise((resolve, reject) => {
connection
.then((db) => {
let tx = db.transaction('collection');
let collectionStore = tx.objectStore('collection');
return collectionStore.getAll();
})
.then((collections) => {
if(!Array.isArray(collections)) {
return new Error('IDB Corrupted');
}
return resolve(collections);
})
.catch((err) => reject(err));
});
};

View File

@ -1,9 +1,3 @@
import React, {useContext, useReducer, createContext } from 'react';
import { nanoid } from 'nanoid';
import reducer from './reducer';
import useIdb from './useIdb';
export const StoreContext = createContext();
const collection = {
"id": nanoid(),
"name": "spacex",
@ -106,32 +100,4 @@ const collection2 = {
]
}
]
};
const initialState = {
idbConnection: null,
collections: [],
activeRequestTabUid: null,
requestTabs: [],
collectionsToSyncToIdb: []
};
export const StoreProvider = props => {
const [state, dispatch] = useReducer(reducer, initialState);
useIdb(dispatch);
return <StoreContext.Provider value={[state, dispatch]} {...props} />;
};
export const useStore = () => {
const context = useContext(StoreContext);
if (context === undefined) {
throw new Error(`useStore must be used within a StoreProvider`);
}
return context;
};
export default StoreProvider;
};

View File

@ -1,28 +0,0 @@
import { useEffect } from 'react';
import { openDB } from 'idb';
import actions from './actions';
const useIdb = (dispatch) => {
useEffect(() => {
let dbName = `grafnode`;
let connection = openDB(dbName, 1, {
upgrade(db, oldVersion, newVersion, transaction) {
switch(oldVersion) {
case 0:
const collectionStore = db.createObjectStore('collection', { keyPath: 'uid' });
collectionStore.createIndex('transactionIdIndex', 'transaction_id');
}
}
});
connection.then(() => {
dispatch({
type: actions.IDB_CONNECTION_READY,
connection: connection
});
})
.catch((err) => console.log(err));
}, []);
};
export default useIdb;

View File

@ -1,48 +0,0 @@
import each from 'lodash/each';
import find from 'lodash/find';
import cloneDeep from 'lodash/cloneDeep';
export const flattenItems = (items = []) => {
const flattenedItems = [];
const flatten = (itms, flattened) => {
each(itms, (i) => {
flattened.push(i);
if(i.items && i.items.length) {
flatten(i.items, flattened);
}
})
}
flatten(items, flattenedItems);
return flattenedItems;
};
export const findItem = (items = [], itemUid) => {
return find(items, (i) => i.uid === itemUid);
};
export const isItemARequest = (item) => {
return item.hasOwnProperty('request');
};
export const itemIsOpenedInTabs = (item, tabs) => {
return find(tabs, (t) => t.uid === item.uid);
};
export const cloneItem = (item) => {
return cloneDeep(item);
};
export const updateRequestTabAsChanged = (requestTabs, itemUid) => {
let currentTab = find(requestTabs, (rt) => rt.uid == itemUid);
if(currentTab) {
currentTab.hasChanges = true;
}
};
export const findCollectionByUid = (collections, collectionUid) => {
return find(collections, (c) => c.uid === collectionUid);
};