From e471d94fbc606d90f365733764d9bbef84e284a7 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Wed, 16 Mar 2022 01:00:35 +0530 Subject: [PATCH] feat: persist request url changes upon saving them --- renderer/components/QueryUrl/index.js | 2 +- renderer/components/RequestTabPanel/index.js | 18 +++++----- renderer/pages/_app.js | 8 ++--- renderer/providers/Hotkeys/index.js | 8 +++++ renderer/providers/Store/actions.js | 4 ++- renderer/providers/Store/reducer.js | 34 +++++++++++++++++-- .../Store/useSyncCollectionsToIdb.js | 2 ++ 7 files changed, 59 insertions(+), 17 deletions(-) diff --git a/renderer/components/QueryUrl/index.js b/renderer/components/QueryUrl/index.js index a940ed514..729b6cd44 100644 --- a/renderer/components/QueryUrl/index.js +++ b/renderer/components/QueryUrl/index.js @@ -84,7 +84,7 @@ const QueryUrl = ({value, onChange, handleRun, collections}) => {
onChange(event.target.value)} /> diff --git a/renderer/components/RequestTabPanel/index.js b/renderer/components/RequestTabPanel/index.js index 66b641da9..552a7e774 100644 --- a/renderer/components/RequestTabPanel/index.js +++ b/renderer/components/RequestTabPanel/index.js @@ -63,15 +63,6 @@ const RequestTabPanel = () => { }; }, [dragging, leftPaneWidth]); - const onUrlChange = (value) => { - storeDispatch({ - type: actions.REQUEST_URL_CHANGED, - url: value, - requestTab: focusedTab, - collectionUid: collection ? collection.uid : null - }); - }; - const onGraphqlQueryChange = (value) => { storeDispatch({ type: actions.REQUEST_GQL_QUERY_CHANGED, @@ -105,6 +96,15 @@ const RequestTabPanel = () => { let flattenedItems = flattenItems(collection.items); let item = findItem(flattenedItems, activeRequestTabUid); + const onUrlChange = (value) => { + storeDispatch({ + type: actions.REQUEST_URL_CHANGED, + url: value, + itemUid: item.uid, + collectionUid: collection ? collection.uid : null + }); + }; + const runQuery = async () => { storeDispatch({ type: actions.SEND_REQUEST, diff --git a/renderer/pages/_app.js b/renderer/pages/_app.js index e9932bfd3..68704e4ac 100644 --- a/renderer/pages/_app.js +++ b/renderer/pages/_app.js @@ -22,11 +22,11 @@ function MyApp({ Component, pageProps }) { return ( - - + + - - + + ); diff --git a/renderer/providers/Hotkeys/index.js b/renderer/providers/Hotkeys/index.js index ac703bad1..9482defde 100644 --- a/renderer/providers/Hotkeys/index.js +++ b/renderer/providers/Hotkeys/index.js @@ -1,13 +1,21 @@ import React, { useEffect } from 'react'; import Mousetrap from 'mousetrap'; +import { useStore } from 'providers/Store'; +import actions from 'providers/Store/actions'; export const HotkeysContext = React.createContext(); export const HotkeysProvider = props => { + const [store, storeDispatch] = useStore(); + useEffect(() => { Mousetrap.bind(['command+s', 'ctrl+s'], (e) => { console.log("Save hotkey"); + storeDispatch({ + type: actions.HOTKEY_SAVE + }); + return false; // this stops the event bubbling }); diff --git a/renderer/providers/Store/actions.js b/renderer/providers/Store/actions.js index f3a27f2c3..ffe10d53f 100644 --- a/renderer/providers/Store/actions.js +++ b/renderer/providers/Store/actions.js @@ -18,6 +18,7 @@ const IDB_CONNECTION_READY = "IDB_CONNECTION_READY"; const IDB_COLLECTIONS_SYNC_STARTED = "IDB_COLLECTIONS_SYNC_STARTED"; const IDB_COLLECTIONS_SYNC_ERROR = "IDB_COLLECTIONS_SYNC_ERROR"; const TOGGLE_LEFT_MENUBAR = "TOGGLE_LEFT_MENUBAR"; +const HOTKEY_SAVE = "HOTKEY_SAVE"; export default { SIDEBAR_COLLECTION_CLICK, @@ -39,5 +40,6 @@ export default { IDB_CONNECTION_READY, IDB_COLLECTIONS_SYNC_STARTED, IDB_COLLECTIONS_SYNC_ERROR, - TOGGLE_LEFT_MENUBAR + TOGGLE_LEFT_MENUBAR, + HOTKEY_SAVE }; diff --git a/renderer/providers/Store/reducer.js b/renderer/providers/Store/reducer.js index a5d91adfd..9ba1ef0a0 100644 --- a/renderer/providers/Store/reducer.js +++ b/renderer/providers/Store/reducer.js @@ -1,6 +1,7 @@ import produce from 'immer'; import {nanoid} from 'nanoid'; import union from 'lodash/union'; +import find from 'lodash/find'; import filter from 'lodash/filter'; import last from 'lodash/last'; import actions from './actions'; @@ -154,14 +155,14 @@ const reducer = (state, action) => { if(collection) { let flattenedItems = flattenItems(collection.items); - let item = findItem(flattenedItems, action.requestTab.id); + let item = findItem(flattenedItems, action.itemUid); if(item) { if(!item.draft) { item.draft = cloneItem(item); } item.draft.request.url = action.url; - updateRequestTabAsChanged(draft.requestTabs, item.id); + updateRequestTabAsChanged(draft.requestTabs, item.uid); } } }); @@ -340,6 +341,35 @@ const reducer = (state, action) => { }); } + 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); + } + } + } + }); + } + default: { return state; } diff --git a/renderer/providers/Store/useSyncCollectionsToIdb.js b/renderer/providers/Store/useSyncCollectionsToIdb.js index 484a1b0c9..31e5a857a 100644 --- a/renderer/providers/Store/useSyncCollectionsToIdb.js +++ b/renderer/providers/Store/useSyncCollectionsToIdb.js @@ -4,6 +4,8 @@ import filter from 'lodash/filter'; import actions from './actions'; import { saveCollectionToIdb } from './idb'; +// This hook listens to changes in 'collectionsToSyncToIdb' and syncs them to idb +// The app uses this when collections are created as well as when collections get updated const useSyncCollectionsToIdb = (collectionsToSyncToIdb, collections, idbConnection, dispatch) => { const [collectionsSyncingToIdb, setCollectionsSyncingToIdb] = useState(false);