Skip to content

Commit

Permalink
xchem#11 add not completed fix of infinity loop during creating initi…
Browse files Browse the repository at this point in the history
…al snapshot
  • Loading branch information
tibor-postek-m2ms committed Apr 16, 2020
1 parent c33ec6f commit 8079dae
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 68 deletions.
10 changes: 8 additions & 2 deletions js/components/preview/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ export const shouldLoadProtein = ({
const targetIdList = state.apiReducers.target_id_list;
const targetOnName = state.apiReducers.target_on_name;
const currentSnapshotData = state.projectReducers.currentSnapshot.data;

if (targetIdList && targetIdList.length > 0 && nglViewList && nglViewList.length > 0) {
const isLoadingCurrentSnapshot = state.projectReducers.isLoadingCurrentSnapshot;
if (
targetIdList &&
targetIdList.length > 0 &&
nglViewList &&
nglViewList.length > 0 &&
isLoadingCurrentSnapshot === false
) {
// 1. Generate new protein or skip this action and everything will be loaded from session
if (!isStateLoaded && currentSnapshotID === null && !routeSnapshotID) {
dispatch(setProteinLoadingState(false));
Expand Down
10 changes: 9 additions & 1 deletion js/components/preview/withLoadingProtein.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ export const withLoadingProtein = WrappedComponent => {
const currentSnapshotID = useSelector(state => state.projectReducers.currentSnapshot.id);

useEffect(() => {
dispatch(shouldLoadProtein({ nglViewList, isStateLoaded, routeProjectID, routeSnapshotID, currentSnapshotID }));
dispatch(
shouldLoadProtein({
nglViewList,
isStateLoaded,
routeProjectID,
routeSnapshotID,
currentSnapshotID
})
);
}, [dispatch, isStateLoaded, nglViewList, routeProjectID, routeSnapshotID, currentSnapshotID]);

return <WrappedComponent isStateLoaded={isStateLoaded} {...rest} />;
Expand Down
31 changes: 18 additions & 13 deletions js/components/projects/projectPreview/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { memo, useContext, useEffect, useRef, useState } from 'react';
import Preview from '../../preview/Preview';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import { useRouteMatch } from 'react-router-dom';
import { loadCurrentSnapshotByID, loadSnapshotByProjectID } from '../redux/dispatchActions';
import { HeaderContext } from '../../header/headerContext';
Expand All @@ -13,39 +13,44 @@ export const ProjectPreview = memo(({}) => {
const dispatch = useDispatch();
const projectId = match && match.params && match.params.projectId;
const snapshotId = match && match.params && match.params.snapshotId;
const currentSnapshotID = useSelector(state => state.projectReducers.currentSnapshot.id);

useEffect(() => {
if (!snapshotId) {
if (!snapshotId && currentSnapshotID === null) {
dispatch(loadSnapshotByProjectID(projectId))
.then(response => {
isSnapshotLoaded.current = response;
setCanShow(true);
if (response !== false) {
isSnapshotLoaded.current = response;
setCanShow(true);
}
})
.catch(error => {
setCanShow(true);
throw new Error(error);
});
} else {
dispatch(loadCurrentSnapshotByID(snapshotId))
dispatch(loadCurrentSnapshotByID(snapshotId || currentSnapshotID))
.then(response => {
if (response) {
if (response.session_project && `${response.session_project.id}` === projectId) {
isSnapshotLoaded.current = response.id;
setCanShow(true);
if (response !== false) {
if (response) {
if (response.session_project && `${response.session_project.id}` === projectId) {
isSnapshotLoaded.current = response.id;
setCanShow(true);
} else {
setCanShow(false);
}
} else {
isSnapshotLoaded.current = response;
setCanShow(false);
}
} else {
isSnapshotLoaded.current = response;
setCanShow(false);
}
})
.catch(error => {
setCanShow(false);
throw new Error(error);
});
}
}, [dispatch, projectId, snapshotId]);
}, [currentSnapshotID, dispatch, projectId, snapshotId]);

if (canShow === false) {
setSnackBarTitle('Not valid snapshot!');
Expand Down
120 changes: 68 additions & 52 deletions js/components/projects/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,61 +107,77 @@ export const removeProject = projectID => dispatch => {
};

export const loadSnapshotByProjectID = projectID => (dispatch, getState) => {
dispatch(setIsLoadingCurrentSnapshot(true));
dispatch(resetCurrentSnapshot());
return api({ url: `${base_url}/api/snapshots/?session_project=${projectID}&type=INIT` })
.then(response => {
if (response.data.results.length === 0) {
return Promise.resolve(null);
} else if (response.data.results[0] !== undefined) {
dispatch(
setCurrentSnapshot({
id: response.data.results[0].id,
type: response.data.results[0].type,
title: response.data.results[0].title,
author: response.data.results[0].author,
description: response.data.results[0].description,
created: response.data.results[0].created,
children: response.data.results[0].children,
parent: response.data.results[0].parent,
data: JSON.parse(response.data.results[0].data)
})
);
return Promise.resolve(response.data.results[0].id);
}
})
.finally(() => {
dispatch(setIsLoadingCurrentSnapshot(false));
});
const state = getState();
const isLoadingCurrentSnapshot = state.projectReducers.isLoadingCurrentSnapshot;
if (isLoadingCurrentSnapshot === false) {
dispatch(setIsLoadingCurrentSnapshot(true));
return api({ url: `${base_url}/api/snapshots/?session_project=${projectID}&type=INIT` })
.then(response => {
if (response.data.results.length === 0) {
dispatch(resetCurrentSnapshot());
return Promise.resolve(null);
} else if (response.data.results[0] !== undefined) {
dispatch(
setCurrentSnapshot({
id: response.data.results[0].id,
type: response.data.results[0].type,
title: response.data.results[0].title,
author: response.data.results[0].author,
description: response.data.results[0].description,
created: response.data.results[0].created,
children: response.data.results[0].children,
parent: response.data.results[0].parent,
data: JSON.parse(response.data.results[0].data)
})
);
return Promise.resolve(response.data.results[0].id);
}
})
.catch(error => {
dispatch(resetCurrentSnapshot());
})
.finally(() => {
dispatch(setIsLoadingCurrentSnapshot(false));
});
}
return Promise.resolve(false);
};

export const loadCurrentSnapshotByID = snapshotID => (dispatch, getState) => {
dispatch(setIsLoadingCurrentSnapshot(true));
dispatch(resetCurrentSnapshot());
return api({ url: `${base_url}/api/snapshots/${snapshotID}` })
.then(response => {
if (response.data.id === undefined) {
return Promise.resolve(null);
} else {
dispatch(
setCurrentSnapshot({
id: response.data.id,
type: response.data.type,
title: response.data.title,
author: response.data.author,
description: response.data.description,
created: response.data.created,
children: response.data.children,
parent: response.data.parent,
data: JSON.parse(response.data.data)
})
);
return Promise.resolve(response.data);
}
})
.finally(() => {
dispatch(setIsLoadingCurrentSnapshot(false));
});
const state = getState();
const isLoadingCurrentSnapshot = state.projectReducers.isLoadingCurrentSnapshot;
if (isLoadingCurrentSnapshot === false) {
dispatch(setIsLoadingCurrentSnapshot(true));
return api({ url: `${base_url}/api/snapshots/${snapshotID}` })
.then(response => {
if (response.data.id === undefined) {
dispatch(resetCurrentSnapshot());
return Promise.resolve(null);
} else {
dispatch(
setCurrentSnapshot({
id: response.data.id,
type: response.data.type,
title: response.data.title,
author: response.data.author,
description: response.data.description,
created: response.data.created,
children: response.data.children,
parent: response.data.parent,
data: JSON.parse(response.data.data)
})
);
return Promise.resolve(response.data);
}
})
.catch(error => {
dispatch(resetCurrentSnapshot());
})
.finally(() => {
dispatch(setIsLoadingCurrentSnapshot(false));
});
}
return Promise.resolve(false);
};

const parseSnapshotAttributes = data => ({
Expand Down

0 comments on commit 8079dae

Please sign in to comment.