From cbfd7fa5f4c6dfa023c4f4fe7c0a929f77218c08 Mon Sep 17 00:00:00 2001 From: Amr osama Date: Sun, 22 Oct 2023 15:32:27 +0300 Subject: [PATCH] Added presets to collection settings --- .../Presets/StyledWrapper.js | 27 ++++ .../CollectionSettings/Presets/index.js | 93 ++++++++++++ .../components/CollectionSettings/index.js | 7 + .../Collection/CollectionProperties/index.js | 138 +++--------------- .../components/Sidebar/NewRequest/index.js | 6 +- .../src/providers/App/useIpcEvents.js | 10 +- .../ReduxStore/slices/collections/actions.js | 4 +- .../ReduxStore/slices/collections/index.js | 10 +- packages/bruno-electron/src/ipc/collection.js | 8 +- 9 files changed, 168 insertions(+), 135 deletions(-) create mode 100644 packages/bruno-app/src/components/CollectionSettings/Presets/StyledWrapper.js create mode 100644 packages/bruno-app/src/components/CollectionSettings/Presets/index.js diff --git a/packages/bruno-app/src/components/CollectionSettings/Presets/StyledWrapper.js b/packages/bruno-app/src/components/CollectionSettings/Presets/StyledWrapper.js new file mode 100644 index 000000000..c8f1241c5 --- /dev/null +++ b/packages/bruno-app/src/components/CollectionSettings/Presets/StyledWrapper.js @@ -0,0 +1,27 @@ +import styled from 'styled-components'; + +const StyledWrapper = styled.div` + .settings-label { + width: 80px; + } + + .textbox { + border: 1px solid #ccc; + padding: 0.15rem 0.45rem; + box-shadow: none; + border-radius: 0px; + outline: none; + box-shadow: none; + transition: border-color ease-in-out 0.1s; + border-radius: 3px; + background-color: ${(props) => props.theme.modal.input.bg}; + border: 1px solid ${(props) => props.theme.modal.input.border}; + + &:focus { + border: solid 1px ${(props) => props.theme.modal.input.focusBorder} !important; + outline: none !important; + } + } +`; + +export default StyledWrapper; diff --git a/packages/bruno-app/src/components/CollectionSettings/Presets/index.js b/packages/bruno-app/src/components/CollectionSettings/Presets/index.js new file mode 100644 index 000000000..e6b4bcec1 --- /dev/null +++ b/packages/bruno-app/src/components/CollectionSettings/Presets/index.js @@ -0,0 +1,93 @@ +import React, { useEffect } from 'react'; +import { useFormik } from 'formik'; +import { useDispatch } from 'react-redux'; +import StyledWrapper from './StyledWrapper'; +import toast from 'react-hot-toast'; +import { updateCollectionPresets } from 'providers/ReduxStore/slices/collections/actions'; + +const PresetsSettings = ({ collection }) => { + const dispatch = useDispatch(); + const { + brunoConfig: { presets: defaultPresets = {} } + } = collection; + + const formik = useFormik({ + enableReinitialize: true, + initialValues: { + defaultType: defaultPresets.defaultType || 'http-request', + defaultRequestUrl: defaultPresets.defaultRequestUrl || '' + }, + onSubmit: (newPresets) => { + dispatch(updateCollectionPresets(newPresets, collection.uid)); + toast.success('Collection presets updated'); + } + }); + + return ( + +

Collection Presets

+
+
+ +
+ + + + + +
+
+
+ +
+
+ +
+
+
+
+ +
+
+
+ ); +}; + +export default PresetsSettings; diff --git a/packages/bruno-app/src/components/CollectionSettings/index.js b/packages/bruno-app/src/components/CollectionSettings/index.js index 62d774e2b..46c4b2e9d 100644 --- a/packages/bruno-app/src/components/CollectionSettings/index.js +++ b/packages/bruno-app/src/components/CollectionSettings/index.js @@ -13,6 +13,7 @@ import Auth from './Auth'; import Script from './Script'; import Test from './Tests'; import Docs from './Docs'; +import Presets from './Presets'; import StyledWrapper from './StyledWrapper'; const CollectionSettings = ({ collection }) => { @@ -75,6 +76,9 @@ const CollectionSettings = ({ collection }) => { case 'headers': { return ; } + case 'presets': { + return ; + } case 'auth': { return ; } @@ -114,6 +118,9 @@ const CollectionSettings = ({ collection }) => {
setTab('headers')}> Headers
+
setTab('presets')}> + Presets +
setTab('auth')}> Auth
diff --git a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionProperties/index.js b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionProperties/index.js index 0a72eb490..376a88db4 100644 --- a/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionProperties/index.js +++ b/packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionProperties/index.js @@ -1,12 +1,5 @@ import React from 'react'; import Modal from 'components/Modal'; -import { useState } from 'react'; -import { useFormik } from 'formik'; -import * as Yup from 'yup'; -import StyledWrapper from './StyledWrapper'; -import { useDispatch } from 'react-redux'; -import toast from 'react-hot-toast'; -import { updateCollectionProperties } from 'providers/ReduxStore/slices/collections/actions'; function countRequests(items) { let count = 0; @@ -28,116 +21,29 @@ function countRequests(items) { } const CollectionProperties = ({ collection, onClose }) => { - const dispatch = useDispatch(); - const { - brunoConfig: { properties: defaultProperties = {} } - } = collection; - const formik = useFormik({ - enableReinitialize: true, - initialValues: { - defaultType: defaultProperties.defaultType || 'http-request', - defaultUrl: defaultProperties.defaultUrl || '' - }, - onSubmit: (newProperties) => { - dispatch(updateCollectionProperties(newProperties, collection.uid)); - toast.success('Collection properties updated'); - onClose(); - } - }); - - const onSubmit = () => formik.handleSubmit(); - return ( - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Name :{collection.name}
Location :{collection.pathname}
Environments :{collection.environments?.length || 0}
Requests :{countRequests(collection.items)}
Default Request Type : -
- - - - - -
-
Default Base URL : -
-
- -
-
-
-
- {formik.touched.defaultUrl && formik.errors.defaultUrl ? ( -
{formik.errors.defaultUrl}
- ) : null} -
-
-
-
+ + + + + + + + + + + + + + + + + + + + +
Name :{collection.name}
Location :{collection.pathname}
Environments :{collection.environments?.length || 0}
Requests :{countRequests(collection.items)}
+
); }; diff --git a/packages/bruno-app/src/components/Sidebar/NewRequest/index.js b/packages/bruno-app/src/components/Sidebar/NewRequest/index.js index 99a7fe3bb..0389e574b 100644 --- a/packages/bruno-app/src/components/Sidebar/NewRequest/index.js +++ b/packages/bruno-app/src/components/Sidebar/NewRequest/index.js @@ -16,14 +16,14 @@ const NewRequest = ({ collection, item, isEphemeral, onClose }) => { const dispatch = useDispatch(); const inputRef = useRef(); const { - brunoConfig: { properties: collectionProperties = {} } + brunoConfig: { presets: collectionPresets = {} } } = collection; const formik = useFormik({ enableReinitialize: true, initialValues: { requestName: '', - requestType: collectionProperties.defaultType || 'http-request', - requestUrl: collectionProperties.defaultUrl || '', + requestType: collectionPresets.defaultType || 'http-request', + requestUrl: collectionPresets.defaultRequestUrl || '', requestMethod: 'GET' }, validationSchema: Yup.object({ diff --git a/packages/bruno-app/src/providers/App/useIpcEvents.js b/packages/bruno-app/src/providers/App/useIpcEvents.js index 1c8787486..6cef0bfa2 100644 --- a/packages/bruno-app/src/providers/App/useIpcEvents.js +++ b/packages/bruno-app/src/providers/App/useIpcEvents.js @@ -12,13 +12,13 @@ import { collectionRenamedEvent, runRequestEvent, runFolderEvent, - brunoConfigUpdateEvent + brunoConfigUpdateEvent, + collectionPresetsUpdatedEvent } from 'providers/ReduxStore/slices/collections'; import { updatePreferences } from 'providers/ReduxStore/slices/app'; import toast from 'react-hot-toast'; import { openCollectionEvent, collectionAddEnvFileEvent } from 'providers/ReduxStore/slices/collections/actions'; import { isElectron } from 'utils/common/platform'; -import { collectionPropertiesUpdatedEvent } from 'providers/ReduxStore/slices/collections/index'; const useIpcEvents = () => { const dispatch = useDispatch(); @@ -108,8 +108,8 @@ const useIpcEvents = () => { dispatch(collectionRenamedEvent(val)); }); - const removeCollectionPropertiesUpdatedListener = ipcRenderer.on('main:collection-properties-updated', (val) => { - dispatch(collectionPropertiesUpdatedEvent(val)); + const removeCollectionPresetsUpdatedListener = ipcRenderer.on('main:collection-presets-updated', (val) => { + dispatch(collectionPresetsUpdatedEvent(val)); }); const removeRunFolderEventListener = ipcRenderer.on('main:run-folder-event', (val) => { @@ -143,7 +143,7 @@ const useIpcEvents = () => { removeDisplayErrorListener(); removeScriptEnvUpdateListener(); removeCollectionRenamedListener(); - removeCollectionPropertiesUpdatedListener(); + removeCollectionPresetsUpdatedListener(); removeRunFolderEventListener(); removeRunRequestEventListener(); removeProcessEnvUpdatesListener(); diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js index 62839b9c9..0d6167d85 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js @@ -47,7 +47,7 @@ import { resolveRequestFilename } from 'utils/common/platform'; import { parseQueryParams, splitOnFirst } from 'utils/url/index'; import { each } from 'lodash'; -export const updateCollectionProperties = (newProperties, collectionUid) => (dispatch, getState) => { +export const updateCollectionPresets = (newPresets, collectionUid) => (dispatch, getState) => { const state = getState(); const collection = findCollectionByUid(state.collections.collections, collectionUid); @@ -57,7 +57,7 @@ export const updateCollectionProperties = (newProperties, collectionUid) => (dis } ipcRenderer - .invoke('renderer:update-collection-properties', newProperties, collection.pathname) + .invoke('renderer:update-collection-presets', newPresets, collection.pathname) .then(resolve) .catch(reject); }); diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js index aef4d179e..eb3b620ab 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js @@ -1224,12 +1224,12 @@ export const collectionsSlice = createSlice({ collection.name = newName; } }, - collectionPropertiesUpdatedEvent: (state, action) => { - const { collectionPathname, newProperties } = action.payload; + collectionPresetsUpdatedEvent: (state, action) => { + const { collectionPathname, newPresets } = action.payload; const collection = findCollectionByPathname(state.collections, collectionPathname); - if (collection) { - collection.properties = newProperties; + if (collection.brunoConfig) { + collection.brunoConfig.presets = newPresets; } }, resetRunResults: (state, action) => { @@ -1435,7 +1435,7 @@ export const { collectionUnlinkDirectoryEvent, collectionAddEnvFileEvent, collectionRenamedEvent, - collectionPropertiesUpdatedEvent, + collectionPresetsUpdatedEvent, resetRunResults, runRequestEvent, runFolderEvent, diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index c0a2e004b..f5d5357ab 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -95,21 +95,21 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection }); // update collection properties - ipcMain.handle('renderer:update-collection-properties', async (event, newProperties, collectionPathname) => { + ipcMain.handle('renderer:update-collection-presets', async (event, newPresets, collectionPathname) => { try { const brunoJsonFilePath = path.join(collectionPathname, 'bruno.json'); const content = fs.readFileSync(brunoJsonFilePath, 'utf8'); const json = JSON.parse(content); - json.properties = newProperties; + json.presets = newPresets; const newContent = await stringifyJson(json); await writeFile(brunoJsonFilePath, newContent); // fire an event in renderer to change the collection properties - mainWindow.webContents.send('main:collection-properties-updated', { + mainWindow.webContents.send('main:collection-presets-updated', { collectionPathname, - newProperties + newPresets }); } catch (error) { return Promise.reject(error);