From 8079dae89329819338d4593c0eb90d606667d4b7 Mon Sep 17 00:00:00 2001 From: Tibor Postek Date: Thu, 16 Apr 2020 11:33:22 +0200 Subject: [PATCH] #11 add not completed fix of infinity loop during creating initial snapshot --- .../preview/redux/dispatchActions.js | 10 +- js/components/preview/withLoadingProtein.js | 10 +- .../projects/projectPreview/index.js | 31 +++-- .../projects/redux/dispatchActions.js | 120 ++++++++++-------- 4 files changed, 103 insertions(+), 68 deletions(-) diff --git a/js/components/preview/redux/dispatchActions.js b/js/components/preview/redux/dispatchActions.js index 4fbe7d80b..f7b1c077c 100644 --- a/js/components/preview/redux/dispatchActions.js +++ b/js/components/preview/redux/dispatchActions.js @@ -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)); diff --git a/js/components/preview/withLoadingProtein.js b/js/components/preview/withLoadingProtein.js index 4de61ca1e..d7217c3e4 100644 --- a/js/components/preview/withLoadingProtein.js +++ b/js/components/preview/withLoadingProtein.js @@ -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 ; diff --git a/js/components/projects/projectPreview/index.js b/js/components/projects/projectPreview/index.js index a59af053f..ca25e818b 100644 --- a/js/components/projects/projectPreview/index.js +++ b/js/components/projects/projectPreview/index.js @@ -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'; @@ -13,31 +13,36 @@ 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 => { @@ -45,7 +50,7 @@ export const ProjectPreview = memo(({}) => { throw new Error(error); }); } - }, [dispatch, projectId, snapshotId]); + }, [currentSnapshotID, dispatch, projectId, snapshotId]); if (canShow === false) { setSnackBarTitle('Not valid snapshot!'); diff --git a/js/components/projects/redux/dispatchActions.js b/js/components/projects/redux/dispatchActions.js index f5009c397..8d9d2c612 100644 --- a/js/components/projects/redux/dispatchActions.js +++ b/js/components/projects/redux/dispatchActions.js @@ -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 => ({