import React, { useEffect, useState } from 'react'; // import { useQuery } from 'react-query'; import useUpdateSettings, { useFetchSettings } from '../../services/settings'; import Icon from '../common/Icon'; import SelectField, { SelectionOption } from '../common/SelectField'; type SettingsProps = { closeSettings: Function, settings?: SettingsType } type SettingsError = { type: string, msg: string } const defaultSettings = { scraper_type: 'none', notification_interval: 'daily', notification_email: '', smtp_server: '', smtp_port: '', smtp_username: '', smtp_password: '', notification_email_from: '', }; 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(); useEffect(() => { if (appSettings && appSettings.settings) { setSettings(appSettings.settings); } }, [appSettings]); useEffect(() => { const closeModalonEsc = (event:KeyboardEvent) => { if (event.key === 'Escape') { console.log(event.key); closeSettings(); } }; window.addEventListener('keydown', closeModalonEsc, false); return () => { window.removeEventListener('keydown', closeModalonEsc, false); }; }, [closeSettings]); const closeOnBGClick = (e:React.SyntheticEvent) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); if (e.target === e.currentTarget) { closeSettings(); } }; const updateSettings = (key: string, value:string|number) => { setSettings({ ...settings, [key]: value }); }; const performUpdate = () => { let error: null|SettingsError = null; if (settings.notification_interval !== 'never') { if (!settings.notification_email) { error = { type: 'no_email', msg: 'Insert a Valid Email address' }; } if (settings.notification_email && (!settings.smtp_port || !settings.smtp_server || !settings.notification_email_from)) { let type = 'no_smtp_from'; if (!settings.smtp_port) { type = 'no_smtp_port'; } if (!settings.smtp_server) { type = 'no_smtp_server'; } error = { type, msg: 'Insert SMTP Server details that will be used to send the emails.' }; } } if (['scrapingant', 'scrapingrobot', 'serply', 'serpapi'].includes(settings.scraper_type) && !settings.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); } }; const labelStyle = 'mb-2 font-semibold inline-block text-sm text-gray-700 capitalize'; const notificationOptions: SelectionOption[] = [ { label: 'Daily', value: 'daily' }, { label: 'Weekly', value: 'weekly' }, { label: 'Monthly', value: 'monthly' }, { label: 'Never', value: 'never' }, ]; const scraperOptions: SelectionOption[] = [ { label: 'None', value: 'none' }, { label: 'Proxy', value: 'proxy' }, { label: 'ScrapingAnt.com', value: 'scrapingant' }, { label: 'ScrapingRobot.com', value: 'scrapingrobot' }, { label: 'serply.io', value: 'serply' }, { label: 'serpapi.com', value: 'serpapi' }, ]; const tabStyle = 'inline-block px-4 py-1 rounded-full mr-3 cursor-pointer text-sm'; return (
{isLoading &&
}

Settings

  • setCurrentTab('scraper')}> Scraper
  • setCurrentTab('notification')}> Notification
{currentTab === 'scraper' && (
updateSettings('scraper_type', updatedTime[0])} multiple={false} rounded={'rounded'} minWidth={270} />
{['scrapingant', 'scrapingrobot', 'serply', 'serpapi'].includes(settings.scraper_type) && (
updateSettings('scaping_api', event.target.value)} />
)} {settings.scraper_type === 'proxy' && (