import React, { useEffect, useState } from 'react'; import { Toaster } from 'react-hot-toast'; import { useFetchSettings, useUpdateSettings } from '../../services/settings'; import Icon from '../common/Icon'; import NotificationSettings from './NotificationSettings'; import ScraperSettings from './ScraperSettings'; import useOnKey from '../../hooks/useOnKey'; import IntegrationSettings from './IntegrationSettings'; type SettingsProps = { closeSettings: Function, settings?: SettingsType } type SettingsError = { type: string, msg: string } export const defaultSettings: SettingsType = { scraper_type: 'none', scrape_delay: 'none', scrape_retry: false, notification_interval: 'daily', notification_email: '', smtp_server: '', smtp_port: '', smtp_username: '', smtp_password: '', notification_email_from: '', notification_email_from_name: 'SerpBear', search_console: true, search_console_client_email: '', search_console_private_key: '', keywordsColumns: ['Best', 'History', 'Volume', 'Search Console'], }; const Settings = ({ closeSettings }:SettingsProps) => { const [currentTab, setCurrentTab] = useState('scraper'); const [settings, setSettings] = useState(defaultSettings); const [settingsError, setSettingsError] = useState(null); const { mutate: updateMutate, isLoading: isUpdating } = useUpdateSettings(() => console.log('')); const { data: appSettings, isLoading } = useFetchSettings(); useOnKey('Escape', closeSettings); useEffect(() => { if (appSettings && appSettings.settings) { setSettings(appSettings.settings); } }, [appSettings]); const closeOnBGClick = (e:React.SyntheticEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); if (e.target === e.currentTarget) { closeSettings(); } }; const updateSettings = (key: string, value:string|number|boolean) => { setSettings({ ...settings, [key]: value }); }; const performUpdate = () => { let error: null|SettingsError = null; const { notification_interval, notification_email, notification_email_from, scraper_type, smtp_port, smtp_server, scaping_api } = settings; if (notification_interval !== 'never') { if (!settings.notification_email) { error = { type: 'no_email', msg: 'Insert a Valid Email address' }; } if (notification_email && (!smtp_port || !smtp_server || !notification_email_from)) { let type = 'no_smtp_from'; if (!smtp_port) { type = 'no_smtp_port'; } if (!smtp_server) { type = 'no_smtp_server'; } error = { type, msg: 'Insert SMTP Server details that will be used to send the emails.' }; } } if (scraper_type !== 'proxy' && scraper_type !== 'none' && !scaping_api) { error = { type: 'no_api_key', msg: 'Insert a Valid API Key or Token for the Scraper Service.' }; } if (error) { setSettingsError(error); setTimeout(() => { setSettingsError(null); }, 3000); } else { // Perform Update updateMutate(settings); // If Scraper is updated, refresh the page. if (appSettings.settings === 'none' && scraper_type !== 'none') { window.location.reload(); } } }; const tabStyle = `inline-block px-3 py-2 rounded-md cursor-pointer text-xs lg:text-sm lg:mr-3 lg:px-4 select-none z-10 text-gray-600 border border-b-0 relative top-[1px] rounded-b-none`; const tabStyleActive = 'bg-white text-blue-600 border-slate-200'; return (
{isLoading &&
}

Settings

  • setCurrentTab('scraper')}> Scraper
  • setCurrentTab('notification')}> Notification
  • setCurrentTab('integrations')}> Integrations
{currentTab === 'scraper' && settings && ( )} {currentTab === 'notification' && settings && ( )} {currentTab === 'integrations' && settings && ( )}
); }; export default Settings;