import PropTypes from 'prop-types' import { createPortal } from 'react-dom' import { useDispatch } from 'react-redux' import { useState, useEffect } from 'react' import { Box, Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControl, IconButton, OutlinedInput, Stack, Typography } from '@mui/material' import { IconEraser, IconTrash, IconX } from '@tabler/icons' import PerfectScrollbar from 'react-perfect-scrollbar' import { BackdropLoader } from 'ui-component/loading/BackdropLoader' import { StyledButton } from 'ui-component/button/StyledButton' import scraperApi from 'api/scraper' import useNotifier from 'utils/useNotifier' import { HIDE_CANVAS_DIALOG, SHOW_CANVAS_DIALOG, enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackbarAction } from 'store/actions' const ManageScrapedLinksDialog = ({ show, dialogProps, onCancel, onSave }) => { const portalElement = document.getElementById('portal') const dispatch = useDispatch() useNotifier() const enqueueSnackbar = (...args) => dispatch(enqueueSnackbarAction(...args)) const closeSnackbar = (...args) => dispatch(closeSnackbarAction(...args)) const [loading, setLoading] = useState(false) const [selectedLinks, setSelectedLinks] = useState([]) const [url, setUrl] = useState('') useEffect(() => { if (dialogProps.url) setUrl(dialogProps.url) if (dialogProps.selectedLinks) setSelectedLinks(dialogProps.selectedLinks) return () => { setLoading(false) setSelectedLinks([]) setUrl('') } }, [dialogProps]) useEffect(() => { if (show) dispatch({ type: SHOW_CANVAS_DIALOG }) else dispatch({ type: HIDE_CANVAS_DIALOG }) return () => dispatch({ type: HIDE_CANVAS_DIALOG }) }, [show, dispatch]) const handleFetchLinks = async () => { setLoading(true) try { const fetchLinksResp = await scraperApi.fetchLinks(url, dialogProps.relativeLinksMethod, dialogProps.limit) if (fetchLinksResp.data) { setSelectedLinks(fetchLinksResp.data.links) enqueueSnackbar({ message: 'Successfully fetched links', options: { key: new Date().getTime() + Math.random(), variant: 'success', action: (key) => ( ) } }) } } catch (error) { const errorData = error.response.data || `${error.response.status}: ${error.response.statusText}` enqueueSnackbar({ message: errorData, options: { key: new Date().getTime() + Math.random(), variant: 'error', persist: true, action: (key) => ( ) } }) } setLoading(false) } const handleChangeLink = (index, event) => { const { value } = event.target const links = [...selectedLinks] links[index] = value setSelectedLinks(links) } const handleRemoveLink = (index) => { const links = [...selectedLinks] links.splice(index, 1) setSelectedLinks(links) } const handleRemoveAllLinks = () => { setSelectedLinks([]) } const handleSaveLinks = () => { onSave(url, selectedLinks) } const component = show ? ( {dialogProps.title || `Manage Scraped Links - ${url}`} { setUrl(e.target.value) }} /> Scraped Links {selectedLinks.length > 0 ? ( } > Clear All ) : null} <> {loading && } {selectedLinks.length > 0 ? ( {selectedLinks.map((link, index) => (
handleChangeLink(index, e)} size='small' value={link} name={`link_${index}`} /> handleRemoveLink(index)} edge='end' >
))}
) : (
Links scraped from the URL will appear here
)}
Save
) : null return createPortal(component, portalElement) } ManageScrapedLinksDialog.propTypes = { show: PropTypes.bool, dialogProps: PropTypes.object, onCancel: PropTypes.func, onSave: PropTypes.func } export default ManageScrapedLinksDialog