Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/#470' into allfunctionality
Browse files Browse the repository at this point in the history
  • Loading branch information
boriskovar-m2ms committed Mar 29, 2021
2 parents 2c04ce1 + 69c3226 commit b09453a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 38 deletions.
47 changes: 28 additions & 19 deletions js/components/projects/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo, useEffect } from 'react';
import React, { memo, useEffect, useCallback } from 'react';
import { Panel } from '../common/Surfaces/Panel';
import {
Table,
Expand Down Expand Up @@ -77,24 +77,33 @@ export const Projects = memo(({}) => {
}, [dispatch]);

useEffect(() => {
if (isDiscourseAvailable()) {
listOfProjects.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage).forEach(project => {
if (!projectDiscourseLinks.hasOwnProperty(project.id)) {
getExistingPost(project.name)
.then(response => {
if (response.data['Post url']) {
projectDiscourseLinks[project.id] = response.data['Post url'];
dispatch(setProjectDiscourseLinks(projectDiscourseLinks));
}
})
.catch(err => {
console.log(err);
dispatch(setOpenDiscourseErrorModal(true));
});
}
});
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 @@ -199,7 +208,7 @@ export const Projects = memo(({}) => {
<Delete />
</IconButton>
<IconButton
disabled={!isDiscourseAvailable() && !projectDiscourseLinks.hasOwnProperty(project.id)}
disabled={!isDiscourseAvailable() && !projectDiscourseLinks?.hasOwnProperty(project.id)}
onClick={() => {
window.open(projectDiscourseLinks[project.id], '_blank');
}}
Expand Down
2 changes: 1 addition & 1 deletion js/components/projects/redux/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const INITIAL_STATE = {
currentSnapshotList: null,
forceCreateProject: false,
isForceProjectCreated: false,
projectDiscourseLinks: {}
projectDiscourseLinks: null
};

export const projectReducers = (state = INITIAL_STATE, action = {}) => {
Expand Down
2 changes: 1 addition & 1 deletion js/components/target/redux/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { constants } from './constatnts';
export const INITIAL_STATE = {
oldUrl: '',
isTargetLoading: false,
targetDiscourseLinks: {},
targetDiscourseLinks: null,
currentTargetLink: null
};

Expand Down
42 changes: 25 additions & 17 deletions js/components/target/targetList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Created by abradley on 13/03/2018.
*/

import React, { memo, useEffect } from 'react';
import React, { memo, useEffect, useCallback } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { ListItemText, ListItemSecondaryAction, Grid } from '@material-ui/core';
import { List, ListItem, Panel } from '../common';
Expand All @@ -19,22 +19,30 @@ export const TargetList = memo(() => {
const targetDiscourseLinks = useSelector(state => state.targetReducers.targetDiscourseLinks);

useEffect(() => {
if (isDiscourseAvailable()) {
target_id_list.forEach(data => {
if (!targetDiscourseLinks.hasOwnProperty(data.id)) {
generateDiscourseTargetURL(data.title)
.then(response => {
targetDiscourseLinks[data.id] = response.data['Post url'];
dispatch(setTargetDiscourseLinks(targetDiscourseLinks));
})
.catch(err => {
console.log(err);
dispatch(setOpenDiscourseErrorModal(true));
});
}
});
if (isDiscourseAvailable() && !targetDiscourseLinks) {
const tempLinks = {};
processTargetItem(tempLinks, target_id_list, 0);
}
}, [target_id_list, targetDiscourseLinks, dispatch]);
}, [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 @@ -55,7 +63,7 @@ export const TargetList = memo(() => {
Open SGC summary
</a>
)}
{discourseAvailable && targetDiscourseLinks.hasOwnProperty(data.id) && (
{discourseAvailable && targetDiscourseLinks?.hasOwnProperty(data.id) && (
<a href={targetDiscourseLinks[data.id]} target="new">
Discourse
</a>
Expand Down

0 comments on commit b09453a

Please sign in to comment.