From 905eb0231de118f676f0669558ed4413bc535522 Mon Sep 17 00:00:00 2001 From: Boris Kovar Date: Mon, 29 Mar 2021 15:15:49 +0200 Subject: [PATCH] Squashed commit of the following: commit b9205ddf6d076bed7c4b3ee3b63d11acd3422fc6 Author: Boris Kovar Date: Mon Mar 29 15:13:32 2021 +0200 - implemented "lazy" handling of the Discourse --- js/components/header/index.js | 76 +++++++++++------------------- js/components/projects/index.js | 43 +++++------------ js/components/target/targetList.js | 46 +++++++----------- 3 files changed, 56 insertions(+), 109 deletions(-) diff --git a/js/components/header/index.js b/js/components/header/index.js index f291668da..9e795d625 100644 --- a/js/components/header/index.js +++ b/js/components/header/index.js @@ -46,9 +46,7 @@ import { TrackingModal } from '../tracking/trackingModal'; import { version } from '../../../package.json'; import { isDiscourseAvailable } from '../../utils/discourse'; import { useSelector, useDispatch } from 'react-redux'; -import { setCurrentTargetLink } from '../target/redux/actions'; import { generateDiscourseTargetURL, getExistingPost } from '../../utils/discourse'; -import { setCurrentProjectDiscourseLink } from '../projects/redux/actions'; import { DiscourseErrorModal } from './discourseErrorModal'; import { setOpenDiscourseErrorModal } from '../../reducers/api/actions'; @@ -108,55 +106,13 @@ export default memo( const [openTrackingModal, setOpenTrackingModal] = useState(false); const currentProject = useSelector(state => state.projectReducers.currentProject); - const targetDiscourseLinks = useSelector(state => state.targetReducers.targetDiscourseLinks); - const targetId = useSelector(state => state.apiReducers.target_on); const targetName = useSelector(state => state.apiReducers.target_on_name); - const targetDiscourseLink = useSelector(state => state.targetReducers.currentTargetLink); - const projectDiscourseLink = useSelector(state => state.projectReducers.currentProjectDiscourseLink); const openDiscourseError = useSelector(state => state.apiReducers.open_discourse_error_modal); const discourseAvailable = isDiscourseAvailable(); - const targetDiscourseVisible = targetDiscourseLink && discourseAvailable; - const projectDiscourseVisible = discourseAvailable && projectDiscourseLink; - - useEffect(() => { - if (targetId && targetDiscourseLinks && targetDiscourseLinks[targetId]) { - dispatch(setCurrentTargetLink(targetDiscourseLinks[targetId])); - } else if (targetName) { - generateDiscourseTargetURL(targetName) - .then(response => { - const url = response.data['Post url']; - if (url) { - dispatch(setCurrentTargetLink(url)); - } - }) - .catch(err => { - console.log(err); - dispatch(setOpenDiscourseErrorModal(true)); - }); - } - }, [targetDiscourseLinks, targetId, dispatch, targetName]); - - useEffect(() => { - if (currentProject) { - if (targetDiscourseLinks && targetDiscourseLinks[currentProject.projectID]) { - dispatch(setCurrentProjectDiscourseLink(targetDiscourseLinks[currentProject.projectID])); - } else if (currentProject.title && targetName) { - getExistingPost(currentProject.title) - .then(response => { - const url = response.data['Post url']; - if (url) { - dispatch(setCurrentProjectDiscourseLink(url)); - } - }) - .catch(err => { - console.log(err); - dispatch(setOpenDiscourseErrorModal(true)); - }); - } - } - }, [currentProject, targetDiscourseLinks, dispatch, targetName]); + const targetDiscourseVisible = discourseAvailable; + const projectDiscourseVisible = discourseAvailable; const openXchem = () => { // window.location.href = 'https://www.diamond.ac.uk/Instruments/Mx/Fragment-Screening.html'; @@ -284,7 +240,19 @@ export default memo( startIcon={} variant="text" size="small" - onClick={() => openDiscourseLink(targetDiscourseLink)} + onClick={() => { + generateDiscourseTargetURL(targetName) + .then(response => { + const url = response.data['Post url']; + if (url) { + openDiscourseLink(url); + } + }) + .catch(err => { + console.log(err); + dispatch(setOpenDiscourseErrorModal(true)); + }); + }} > )} {projectDiscourseVisible && ( @@ -292,7 +260,19 @@ export default memo( startIcon={} variant="text" size="small" - onClick={() => openDiscourseLink(projectDiscourseLink)} + onClick={() => { + getExistingPost(currentProject.title) + .then(response => { + const url = response.data['Post url']; + if (url) { + openDiscourseLink(url); + } + }) + .catch(err => { + console.log(err); + dispatch(setOpenDiscourseErrorModal(true)); + }); + }} > )} diff --git a/js/components/projects/index.js b/js/components/projects/index.js index a8306c3cc..fa78c9387 100644 --- a/js/components/projects/index.js +++ b/js/components/projects/index.js @@ -21,7 +21,7 @@ import { Link } from 'react-router-dom'; import { debounce } from 'lodash'; import { URLS } from '../routes/constants'; import moment from 'moment'; -import { setProjectModalOpen, setProjectDiscourseLinks } from './redux/actions'; +import { setProjectModalOpen } from './redux/actions'; import { useDispatch, useSelector } from 'react-redux'; import { ProjectModal } from './projectModal'; import { loadListOfAllProjects, removeProject, searchInProjects } from './redux/dispatchActions'; @@ -76,35 +76,6 @@ export const Projects = memo(({}) => { }); }, [dispatch]); - useEffect(() => { - if (isDiscourseAvailable() && !projectDiscourseLinks) { - const tempLinks = {}; - const source = listOfProjects.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage); - processProjectItem(tempLinks, source, 0); - } - }, [listOfProjects, page, processProjectItem, projectDiscourseLinks, rowsPerPage]); - - const processProjectItem = useCallback( - (links, sourceData, index) => { - if (sourceData && sourceData.length >= index + 1) { - const project = sourceData[index]; - getExistingPost(project.name) - .then(response => { - if (response.data['Post url']) { - links[project.id] = response.data['Post url']; - dispatch(setProjectDiscourseLinks(links)); - processProjectItem(links, sourceData, index + 1); - } - }) - .catch(err => { - console.log(err); - dispatch(setOpenDiscourseErrorModal(true)); - }); - } - }, - [dispatch] - ); - const handleChangePage = (event, newPage) => { setPage(newPage); }; @@ -210,7 +181,17 @@ export const Projects = memo(({}) => { { - window.open(projectDiscourseLinks[project.id], '_blank'); + getExistingPost(project.name) + .then(response => { + if (response.data['Post url']) { + const link = response.data['Post url']; + window.open(link, '_blank'); + } + }) + .catch(err => { + console.log(err); + dispatch(setOpenDiscourseErrorModal(true)); + }); }} > diff --git a/js/components/target/targetList.js b/js/components/target/targetList.js index 00333c574..dc133a0cc 100644 --- a/js/components/target/targetList.js +++ b/js/components/target/targetList.js @@ -9,40 +9,12 @@ import { List, ListItem, Panel } from '../common'; import { Link } from 'react-router-dom'; import { URLS } from '../routes/constants'; import { isDiscourseAvailable, generateDiscourseTargetURL } from '../../utils/discourse'; -import { setTargetDiscourseLinks } from './redux/actions'; import { setOpenDiscourseErrorModal } from '../../reducers/api/actions'; export const TargetList = memo(() => { const dispatch = useDispatch(); const isTargetLoading = useSelector(state => state.targetReducers.isTargetLoading); const target_id_list = useSelector(state => state.apiReducers.target_id_list); - const targetDiscourseLinks = useSelector(state => state.targetReducers.targetDiscourseLinks); - - useEffect(() => { - if (isDiscourseAvailable() && !targetDiscourseLinks) { - const tempLinks = {}; - processTargetItem(tempLinks, target_id_list, 0); - } - }, [target_id_list, targetDiscourseLinks, dispatch, processTargetItem]); - - const processTargetItem = useCallback( - (links, sourceData, index) => { - if (sourceData && sourceData.length >= index + 1) { - const data = sourceData[index]; - generateDiscourseTargetURL(data.title) - .then(response => { - links[data.id] = response.data['Post url']; - dispatch(setTargetDiscourseLinks(links)); - processTargetItem(links, sourceData, index + 1); - }) - .catch(err => { - console.log(err); - dispatch(setOpenDiscourseErrorModal(true)); - }); - } - }, - [dispatch] - ); const render_method = data => { const preview = URLS.target + data.title; @@ -63,8 +35,22 @@ export const TargetList = memo(() => { Open SGC summary )} - {discourseAvailable && targetDiscourseLinks?.hasOwnProperty(data.id) && ( - + {discourseAvailable && ( + { + generateDiscourseTargetURL(data.title) + .then(response => { + const link = response.data['Post url']; + window.open(link, '_blank'); + }) + .catch(err => { + console.log(err); + dispatch(setOpenDiscourseErrorModal(true)); + }); + }} + > Discourse )}