feat: rename collection

This commit is contained in:
Anoop M D 2023-02-01 09:59:23 +05:30
parent d4f05fa843
commit be49ef5f12
6 changed files with 62 additions and 26 deletions

View File

@ -52,6 +52,12 @@ const Wrapper = styled.div`
}
}
}
#sidebar-collection-name {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
`;
export default Wrapper;

View File

@ -98,7 +98,7 @@ const Collection = ({ collection, searchText }) => {
{showRenameCollectionModal && <RenameCollection collection={collection} onClose={() => setShowRenameCollectionModal(false)} />}
{showRemoveCollectionModal && <RemoveCollection collection={collection} onClose={() => setShowRemoveCollectionModal(false)} />}
<div className="flex py-1 collection-name items-center" ref={drop}>
<div className="flex flex-grow items-center" onClick={handleClick}>
<div className="flex flex-grow items-center overflow-hidden" onClick={handleClick}>
<IconChevronRight size={16} strokeWidth={2} className={iconClassName} style={{ width: 16, color: 'rgb(160 160 160)' }} />
<div className="ml-1" id="sidebar-collection-name">{collection.name}</div>
</div>
@ -122,8 +122,7 @@ const Collection = ({ collection, searchText }) => {
>
New Folder
</div>
{/* Todo: implement rename collection */}
{/* <div
<div
className="dropdown-item"
onClick={(e) => {
menuDropdownTippyRef.current.hide();
@ -131,7 +130,7 @@ const Collection = ({ collection, searchText }) => {
}}
>
Rename
</div> */}
</div>
<div
className="dropdown-item"
onClick={(e) => {

View File

@ -10,7 +10,8 @@ import {
requestSentEvent,
requestQueuedEvent,
testResultsEvent,
scriptEnvironmentUpdateEvent
scriptEnvironmentUpdateEvent,
collectionRenamedEvent
} from 'providers/ReduxStore/slices/collections';
import toast from 'react-hot-toast';
import { openCollectionEvent, collectionAddEnvFileEvent } from 'providers/ReduxStore/slices/collections/actions';
@ -100,6 +101,10 @@ const useCollectionTreeSync = () => {
dispatch(testResultsEvent(val));
};
const _collectionRenamed = (val) => {
dispatch(collectionRenamedEvent(val));
};
ipcRenderer.invoke('renderer:ready');
const removeListener1 = ipcRenderer.on('main:collection-opened', _openCollection);
@ -110,6 +115,7 @@ const useCollectionTreeSync = () => {
const removeListener6 = ipcRenderer.on('main:script-environment-update', _scriptEnvironmentUpdate);
const removeListener7 = ipcRenderer.on('main:http-request-queued', _httpRequestQueued);
const removeListener8 = ipcRenderer.on('main:test-results', _testResults);
const removeListener9 = ipcRenderer.on('main:collection-renamed', _collectionRenamed);
return () => {
removeListener1();
@ -120,6 +126,7 @@ const useCollectionTreeSync = () => {
removeListener6();
removeListener7();
removeListener8();
removeListener9();
};
}, [isElectron]);
};

View File

@ -11,7 +11,6 @@ import {
moveCollectionItemToRootOfCollection,
findCollectionByUid,
recursivelyGetAllItemUids,
transformCollectionToSaveToIdb,
transformRequestToSaveToFilesystem,
findParentItemInCollection,
findEnvironmentInCollection,
@ -48,26 +47,16 @@ export const renameCollection = (newName, collectionUid) => (dispatch, getState)
const state = getState();
const collection = findCollectionByUid(state.collections.collections, collectionUid);
if (collection) {
const collectionCopy = cloneDeep(collection);
collectionCopy.name = newName;
const collectionToSave = transformCollectionToSaveToIdb(collectionCopy, {
ignoreDraft: true
});
collectionSchema
.validate(collectionToSave)
.then(() => saveCollectionToIdb(window.__idb, collectionToSave))
.then(() => {
dispatch(
_renameCollection({
newName: newName,
collectionUid: collectionUid
})
);
})
.catch((err) => console.log(err));
return new Promise((resolve, reject) => {
if (!collection) {
return reject(new Error('Collection not found'));
}
ipcRenderer
.invoke('renderer:rename-collection', newName, collection.pathname)
.then(resolve)
.catch(reject);
});
};
export const saveRequest = (itemUid, collectionUid) => (dispatch, getState) => {

View File

@ -11,6 +11,7 @@ import { createSlice } from '@reduxjs/toolkit';
import splitOnFirst from 'split-on-first';
import {
findCollectionByUid,
findCollectionByPathname,
findItemInCollection,
findEnvironmentInCollection,
findItemInCollectionByPathname,
@ -840,6 +841,14 @@ export const collectionsSlice = createSlice({
item.testResults = results;
}
}
},
collectionRenamedEvent: (state, action) => {
const { collectionPathname, newName } = action.payload;
const collection = findCollectionByPathname(state.collections, collectionPathname);
if (collection) {
collection.name = newName;
}
}
}
});
@ -891,7 +900,8 @@ export const {
collectionUnlinkFileEvent,
collectionUnlinkDirectoryEvent,
collectionAddEnvFileEvent,
testResultsEvent
testResultsEvent,
collectionRenamedEvent
} = collectionsSlice.actions;
export default collectionsSlice.reducer;

View File

@ -64,6 +64,31 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
}
});
// rename collection
ipcMain.handle('renderer:rename-collection', async (event, newName, collectionPathname) => {
try {
const brunoJsonFilePath = path.join(collectionPathname, 'bruno.json');
const content = fs.readFileSync(brunoJsonFilePath, 'utf8');
const json = JSON.parse(content);
json.name = newName;
const newContent = await stringifyJson(json);
await writeFile(brunoJsonFilePath, newContent);
// todo: listen for bruno.json changes and handle it in watcher
// the app will change the name of the collection after parsing the bruno.json file contents
mainWindow.webContents.send('main:collection-renamed', {
collectionPathname,
newName
});
return;
} catch (error) {
return Promise.reject(error);
}
});
// new request
ipcMain.handle('renderer:new-request', async (event, pathname, request) => {
try {