import React, { useState } from 'react'; import Icon from '../common/Icon'; import Modal from '../common/Modal'; import SelectField from '../common/SelectField'; import countries from '../../utils/countries'; import { useAddKeywords } from '../../services/keywords'; type AddKeywordsProps = { keywords: KeywordType[], scraperName: string, allowsCity: boolean, closeModal: Function, domain: string } type KeywordsInput = { keywords: string, device: string, country: string, domain: string, tags: string, city?:string, } const AddKeywords = ({ closeModal, domain, keywords, scraperName = '', allowsCity = false }: AddKeywordsProps) => { const [error, setError] = useState(''); const defCountry = localStorage.getItem('default_country') || 'US'; const [newKeywordsData, setNewKeywordsData] = useState({ keywords: '', device: 'desktop', country: defCountry, domain, tags: '' }); const { mutate: addMutate, isLoading: isAdding } = useAddKeywords(() => closeModal(false)); const deviceTabStyle = 'cursor-pointer px-3 py-2 rounded mr-2'; const addKeywords = () => { if (newKeywordsData.keywords) { const keywordsArray = [...new Set(newKeywordsData.keywords.split('\n').map((item) => item.trim()).filter((item) => !!item))]; const currentKeywords = keywords.map((k) => `${k.keyword}-${k.device}-${k.country}${k.city ? `-${k.city}` : ''}`); const keywordExist = keywordsArray.filter((k) => currentKeywords.includes( `${k}-${newKeywordsData.device}-${newKeywordsData.country}${newKeywordsData.city ? `-${newKeywordsData.city}` : ''}`, )); if (keywordExist.length > 0) { setError(`Keywords ${keywordExist.join(',')} already Exist`); setTimeout(() => { setError(''); }, 3000); } else { const { device, country, domain: kDomain, tags, city } = newKeywordsData; const newKeywordsArray = keywordsArray.map((nItem) => ({ keyword: nItem, device, country, domain: kDomain, tags, city })); addMutate(newKeywordsArray); } } else { setError('Please Insert a Keyword'); setTimeout(() => { setError(''); }, 3000); } }; return ( { closeModal(false); }} title={'Add New Keywords'} width="[420px]">
{ return { label: countries[countryISO][0], value: countryISO }; })} defaultLabel='All Countries' updateField={(updated:string[]) => { setNewKeywordsData({ ...newKeywordsData, country: updated[0] }); localStorage.setItem('default_country', updated[0]); }} rounded='rounded' maxHeight={48} flags={true} />
  • setNewKeywordsData({ ...newKeywordsData, device: 'desktop' })} > Desktop
  • setNewKeywordsData({ ...newKeywordsData, device: 'mobile' })} > Mobile
{/* TODO: Insert Existing Tags as Suggestions */} setNewKeywordsData({ ...newKeywordsData, tags: e.target.value })} />
setNewKeywordsData({ ...newKeywordsData, city: e.target.value })} />
{error &&
{error}
}
); }; export default AddKeywords;