From 759b111775c71d18c070819ce83ddd2c95771422 Mon Sep 17 00:00:00 2001 From: Tibor Postek Date: Wed, 22 Apr 2020 12:42:43 +0200 Subject: [PATCH] enable to load snapshot and project without author --- js/components/snapshot/downloadPdb.js | 10 +- .../snapshot/redux/dispatchActions.js | 109 +++--------------- .../snapshot/withSnapshotManagement.js | 24 ++-- js/components/target/redux/dispatchActions.js | 2 +- 4 files changed, 43 insertions(+), 102 deletions(-) diff --git a/js/components/snapshot/downloadPdb.js b/js/components/snapshot/downloadPdb.js index 4aa9aed98..823c99e1d 100644 --- a/js/components/snapshot/downloadPdb.js +++ b/js/components/snapshot/downloadPdb.js @@ -8,9 +8,11 @@ import { Button } from '@material-ui/core'; import FileSaver from 'file-saver'; import { api } from '../../utils/api'; import { CloudDownload, Loop } from '@material-ui/icons'; +import { useDisableUserInteraction } from '../helpers/useEnableUserInteracion'; const DownloadPdb = memo(({ targetOn, targetOnName, key }) => { const [downloading, setDownloading] = useState(false); + const disableUserInteraction = useDisableUserInteraction(); const handlePdbDownload = async () => { setDownloading(true); @@ -67,7 +69,13 @@ const DownloadPdb = memo(({ targetOn, targetOnName, key }) => { ); } else { return ( - ); diff --git a/js/components/snapshot/redux/dispatchActions.js b/js/components/snapshot/redux/dispatchActions.js index 1ee92a032..960ab8230 100644 --- a/js/components/snapshot/redux/dispatchActions.js +++ b/js/components/snapshot/redux/dispatchActions.js @@ -1,13 +1,7 @@ import { reloadApiState, setSessionTitle } from '../../../reducers/api/actions'; import { reloadSelectionReducer } from '../../../reducers/selection/actions'; import { api, METHOD } from '../../../utils/api'; -import { - setDialogCurrentStep, - setIsLoadingListOfSnapshots, - setIsLoadingSnapshotDialog, - setListOfSnapshots, - setOpenSnapshotSavingDialog -} from './actions'; +import { setIsLoadingListOfSnapshots, setIsLoadingSnapshotDialog, setListOfSnapshots } from './actions'; import { DJANGO_CONTEXT } from '../../../utils/djangoContext'; import { assignSnapshotToProject, loadSnapshotTree } from '../../projects/redux/dispatchActions'; import { reloadPreviewReducer } from '../../preview/redux/dispatchActions'; @@ -18,6 +12,19 @@ import { reloadNglViewFromSnapshot } from '../../../reducers/ngl/dispatchActions import { base_url, URLS } from '../../routes/constants'; import { resetCurrentSnapshot, setCurrentSnapshot } from '../../projects/redux/actions'; +export const getListOfSnapshots = () => (dispatch, getState) => { + dispatch(setIsLoadingListOfSnapshots(true)); + return api({ url: `${base_url}/api/snapshots/?session_project!=null` }) + .then(response => { + if (response && response.data && response.data.results) { + dispatch(setListOfSnapshots(response.data.results)); + } + }) + .finally(() => { + dispatch(setIsLoadingListOfSnapshots(false)); + }); +}; + export const reloadSession = (snapshotData, nglViewList) => (dispatch, getState) => { const state = getState(); const snapshotTitle = state.projectReducers.currentSnapshot.title; @@ -169,6 +176,7 @@ export const createNewSnapshot = ({ title, description, type, author, parent, se if (response.data.count === 0) { newType = SnapshotType.INIT; } + return api({ url: `${base_url}/api/snapshots/`, data: { @@ -182,33 +190,14 @@ export const createNewSnapshot = ({ title, description, type, author, parent, se children: [] }, method: METHOD.POST - }).then(response => { - Promise.all([ - dispatch(setDialogCurrentStep(0)), - dispatch(setIsLoadingSnapshotDialog(false)), - dispatch(setOpenSnapshotSavingDialog(false)), - 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) - }) - ), - dispatch(getListOfSnapshots()) - ]); + }).then(res => { // redirect to project with newest created snapshot /:projectID/:snapshotID - if (response.data.id && session_project) { + if (res.data.id && session_project) { // Really bad usage or redirection. Hint for everybody in this line ignore it, but in other parts of code // use react-router ! window.location.replace( `${URLS.projects}${session_project}/${ - selectedSnapshotToSwitch === null ? response.data.id : selectedSnapshotToSwitch + selectedSnapshotToSwitch === null ? res.data.id : selectedSnapshotToSwitch }` ); } @@ -216,65 +205,3 @@ export const createNewSnapshot = ({ title, description, type, author, parent, se }) ]); }; - -export const createSnapshotFromOld = (snapshot, history) => (dispatch, getState) => { - if (!snapshot) { - return Promise.reject('Snapshot is missing!'); - } - const { title, description, data, author, session_project, children } = snapshot; - if (!session_project) { - return Promise.reject('Project ID is missing!'); - } - - return Promise.all([ - dispatch(setIsLoadingSnapshotDialog(true)), - api({ - url: `${base_url}/api/snapshots/`, - data: { - title, - description, - type: SnapshotType.INIT, - author, - parent: null, - session_project, - data: JSON.stringify(data), - children - }, - method: METHOD.POST - }).then(response => { - Promise.all([ - 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) - }) - ), - dispatch(getListOfSnapshots()) - ]); - // redirect to project with newest created snapshot /:projectID/:snapshotID - if (response.data.id && session_project) { - history.push(`${URLS.projects}${session_project}/${response.data.id}`); - } - }) - ]); -}; - -export const getListOfSnapshots = () => (dispatch, getState) => { - dispatch(setIsLoadingListOfSnapshots(true)); - return api({ url: `${base_url}/api/snapshots/?session_project!=null` }) - .then(response => { - if (response && response.data && response.data.results) { - dispatch(setListOfSnapshots(response.data.results)); - } - }) - .finally(() => { - dispatch(setIsLoadingListOfSnapshots(false)); - }); -}; diff --git a/js/components/snapshot/withSnapshotManagement.js b/js/components/snapshot/withSnapshotManagement.js index 3834ce6a5..df7d5f1c8 100644 --- a/js/components/snapshot/withSnapshotManagement.js +++ b/js/components/snapshot/withSnapshotManagement.js @@ -7,6 +7,8 @@ import { savingStateConst } from './constants'; import { HeaderContext } from '../header/headerContext'; import { setOpenSnapshotSavingDialog } from './redux/actions'; import { useRouteMatch } from 'react-router-dom'; +import { DJANGO_CONTEXT } from '../../utils/djangoContext'; +import { useDisableUserInteraction } from '../helpers/useEnableUserInteracion'; /** * Created by ricgillams on 13/06/2018. @@ -22,14 +24,17 @@ export const withSnapshotManagement = WrappedComponent => { const targetIdList = useSelector(state => state.apiReducers.target_id_list); const targetName = useSelector(state => state.apiReducers.target_on_name); + const currentProject = useSelector(state => state.projectReducers.currentProject); const projectId = match && match.params && match.params.projectId; + const target = match && match.params && match.params.target; + const disableUserInteraction = useDisableUserInteraction(); - const disableButtons = - (savingState && - (savingState.startsWith(savingStateConst.saving) || savingState.startsWith(savingStateConst.overwriting))) || - !projectId || - !targetName || - false; + const enableButton = + (projectId && + currentProject.projectID !== null && + DJANGO_CONTEXT['pk'] === currentProject.authorID && + currentProject.authorID !== null) || + target; // Function for set Header buttons, target title and snackBar information about session useEffect(() => { @@ -42,7 +47,7 @@ export const withSnapshotManagement = WrappedComponent => { color="primary" onClick={() => dispatch(setOpenSnapshotSavingDialog(true))} startIcon={} - // disabled={disableButtons} + disabled={!enableButton || disableUserInteraction} > Save , @@ -57,7 +62,7 @@ export const withSnapshotManagement = WrappedComponent => { setHeaderNavbarTitle(''); }; }, [ - disableButtons, + enableButton, dispatch, sessionTitle, setHeaderNavbarTitle, @@ -66,7 +71,8 @@ export const withSnapshotManagement = WrappedComponent => { targetIdList, targetName, setSnackBarColor, - projectId + projectId, + disableUserInteraction ]); return ; diff --git a/js/components/target/redux/dispatchActions.js b/js/components/target/redux/dispatchActions.js index a34e99140..12530c1c9 100644 --- a/js/components/target/redux/dispatchActions.js +++ b/js/components/target/redux/dispatchActions.js @@ -64,7 +64,7 @@ export const updateTarget = ({ target, setIsLoading, targetIdList, projectId }) dispatch( setCurrentProject({ projectID: response.data.id, - authorID: response.data.author.id, + authorID: (response.data.author && response.data.author.id) || null, title: response.data.title, description: response.data.description, targetID: response.data.target.id,