Skip to content

Commit

Permalink
enable to load snapshot and project without author
Browse files Browse the repository at this point in the history
  • Loading branch information
tibor-postek-m2ms committed Apr 27, 2020
1 parent 811274c commit 759b111
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 102 deletions.
10 changes: 9 additions & 1 deletion js/components/snapshot/downloadPdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -67,7 +69,13 @@ const DownloadPdb = memo(({ targetOn, targetOnName, key }) => {
);
} else {
return (
<Button key={key} color="primary" onClick={handlePdbDownload} startIcon={<CloudDownload />}>
<Button
key={key}
color="primary"
disabled={disableUserInteraction}
onClick={handlePdbDownload}
startIcon={<CloudDownload />}
>
Download structures
</Button>
);
Expand Down
109 changes: 18 additions & 91 deletions js/components/snapshot/redux/dispatchActions.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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: {
Expand All @@ -182,99 +190,18 @@ 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
}`
);
}
});
})
]);
};

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));
});
};
24 changes: 15 additions & 9 deletions js/components/snapshot/withSnapshotManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(() => {
Expand All @@ -42,7 +47,7 @@ export const withSnapshotManagement = WrappedComponent => {
color="primary"
onClick={() => dispatch(setOpenSnapshotSavingDialog(true))}
startIcon={<Save />}
// disabled={disableButtons}
disabled={!enableButton || disableUserInteraction}
>
Save
</Button>,
Expand All @@ -57,7 +62,7 @@ export const withSnapshotManagement = WrappedComponent => {
setHeaderNavbarTitle('');
};
}, [
disableButtons,
enableButton,
dispatch,
sessionTitle,
setHeaderNavbarTitle,
Expand All @@ -66,7 +71,8 @@ export const withSnapshotManagement = WrappedComponent => {
targetIdList,
targetName,
setSnackBarColor,
projectId
projectId,
disableUserInteraction
]);

return <WrappedComponent {...rest} />;
Expand Down
2 changes: 1 addition & 1 deletion js/components/target/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 759b111

Please sign in to comment.