diff --git a/packages/ui/src/ui-component/dialog/ManageScrapedLinksDialog.js b/packages/ui/src/ui-component/dialog/ManageScrapedLinksDialog.js new file mode 100644 index 00000000..ecfcd403 --- /dev/null +++ b/packages/ui/src/ui-component/dialog/ManageScrapedLinksDialog.js @@ -0,0 +1,184 @@ +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 { IconTrash } 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 { HIDE_CANVAS_DIALOG, SHOW_CANVAS_DIALOG } from 'store/actions' + +const ManageScrapedLinksDialog = ({ show, dialogProps, onCancel, onSave }) => { + const portalElement = document.getElementById('portal') + const dispatch = useDispatch() + + 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) + const fetchLinksResp = await scraperApi.fetchAllLinks(url, 'webCrawl') + if (fetchLinksResp.data) { + setSelectedLinks(fetchLinksResp.data.links) + } + 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 handleSaveLinks = () => { + onSave(selectedLinks) + } + + const component = show ? ( + + + {dialogProps.title || `Manage Scraped Links - ${url}`} + + + + + + { + setUrl(e.target.value) + }} + /> + + + + + Scraped Links + {selectedLinks.length > 0 ? ( + + {selectedLinks.map((link, index) => ( +
+ + handleChangeLink(index, e)} + size='small' + value={link} + name={`link_${index}`} + /> + + + handleRemoveLink(index)} + edge='end' + > + + + +
+ ))} +
+ ) : ( + <> + {loading && } +
+ 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