Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit b9205dd
Author: Boris Kovar <boris.kovar@m2ms.sk>
Date:   Mon Mar 29 15:13:32 2021 +0200

    - implemented "lazy" handling of the Discourse
  • Loading branch information
boriskovar-m2ms committed Mar 29, 2021
1 parent 8a9197e commit 905eb02
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 109 deletions.
76 changes: 28 additions & 48 deletions js/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -284,15 +240,39 @@ export default memo(
startIcon={<Chat />}
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));
});
}}
></Button>
)}
{projectDiscourseVisible && (
<Button
startIcon={<QuestionAnswer />}
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));
});
}}
></Button>
)}
</ButtonGroup>
Expand Down
43 changes: 12 additions & 31 deletions js/components/projects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -210,7 +181,17 @@ export const Projects = memo(({}) => {
<IconButton
disabled={!isDiscourseAvailable() && !projectDiscourseLinks?.hasOwnProperty(project.id)}
onClick={() => {
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));
});
}}
>
<QuestionAnswer />
Expand Down
46 changes: 16 additions & 30 deletions js/components/target/targetList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -63,8 +35,22 @@ export const TargetList = memo(() => {
Open SGC summary
</a>
)}
{discourseAvailable && targetDiscourseLinks?.hasOwnProperty(data.id) && (
<a href={targetDiscourseLinks[data.id]} target="new">
{discourseAvailable && (
<a
href=""
target="new"
onClick={() => {
generateDiscourseTargetURL(data.title)
.then(response => {
const link = response.data['Post url'];
window.open(link, '_blank');
})
.catch(err => {
console.log(err);
dispatch(setOpenDiscourseErrorModal(true));
});
}}
>
Discourse
</a>
)}
Expand Down

0 comments on commit 905eb02

Please sign in to comment.