import React from 'react'; import classnames from 'classnames'; import get from 'lodash/get'; import cloneDeep from 'lodash/cloneDeep'; import toast from 'react-hot-toast'; import { updateBrunoConfig } from 'providers/ReduxStore/slices/collections/actions'; import { updateSettingsSelectedTab } from 'providers/ReduxStore/slices/collections'; import { useDispatch } from 'react-redux'; import ProxySettings from './ProxySettings'; import ClientCertSettings from './ClientCertSettings'; import Headers from './Headers'; import Auth from './Auth'; import Script from './Script'; import Test from './Tests'; import Docs from './Docs'; import Presets from './Presets'; import Info from './Info'; import StyledWrapper from './StyledWrapper'; const CollectionSettings = ({ collection }) => { const dispatch = useDispatch(); const tab = collection.settingsSelectedTab; const setTab = (tab) => { dispatch( updateSettingsSelectedTab({ collectionUid: collection.uid, tab }) ); }; const proxyConfig = get(collection, 'brunoConfig.proxy', {}); const clientCertConfig = get(collection, 'brunoConfig.clientCertificates.certs', []); const onProxySettingsUpdate = (config) => { const brunoConfig = cloneDeep(collection.brunoConfig); brunoConfig.proxy = config; dispatch(updateBrunoConfig(brunoConfig, collection.uid)) .then(() => { toast.success('Collection settings updated successfully.'); }) .catch((err) => console.log(err) && toast.error('Failed to update collection settings')); }; const onClientCertSettingsUpdate = (config) => { const brunoConfig = cloneDeep(collection.brunoConfig); if (!brunoConfig.clientCertificates) { brunoConfig.clientCertificates = { enabled: true, certs: [config] }; } else { brunoConfig.clientCertificates.certs.push(config); } dispatch(updateBrunoConfig(brunoConfig, collection.uid)) .then(() => { toast.success('Collection settings updated successfully'); }) .catch((err) => console.log(err) && toast.error('Failed to update collection settings')); }; const onClientCertSettingsRemove = (config) => { const brunoConfig = cloneDeep(collection.brunoConfig); brunoConfig.clientCertificates.certs = brunoConfig.clientCertificates.certs.filter( (item) => item.domain != config.domain ); dispatch(updateBrunoConfig(brunoConfig, collection.uid)) .then(() => { toast.success('Collection settings updated successfully'); }) .catch((err) => console.log(err) && toast.error('Failed to update collection settings')); }; const getTabPanel = (tab) => { switch (tab) { case 'headers': { return ; } case 'auth': { return ; } case 'script': { return ; } case 'tests': { return ; } case 'presets': { return ; } case 'proxy': { return ; } case 'clientCert': { return ( ); } case 'docs': { return ; } case 'info': { return ; } } }; const getTabClassname = (tabName) => { return classnames(`tab select-none ${tabName}`, { active: tabName === tab }); }; return ( setTab('headers')}> Headers setTab('auth')}> Auth setTab('script')}> Script setTab('tests')}> Tests setTab('presets')}> Presets setTab('proxy')}> Proxy setTab('clientCert')}> Client Certificates setTab('docs')}> Docs setTab('info')}> Info {getTabPanel(tab)} ); }; export default CollectionSettings;