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'; type SettingsProps = { closeSettings: Function, settings?: SettingsType } type SettingsError = { type: string, msg: string } const defaultSettings = { 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: '', }; 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); } }; 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' && settings && ( )} {currentTab === 'notification' && settings && ( )}
); }; export default Settings;