Skip to content

Commit db446c2

Browse files
- integrated solution with new version of backend
1 parent 42a74b8 commit db446c2

File tree

6 files changed

+69
-142
lines changed

6 files changed

+69
-142
lines changed

js/components/datasets/selectedCompoundsList.js

+42-134
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ import { VIEWS } from '../../constants/constants';
2828
import FileSaver from 'file-saver';
2929
import JSZip from 'jszip';
3030
import { isCompoundFromVectorSelector } from '../preview/compounds/redux/dispatchActions';
31+
import { saveAndShareSnapshot } from '../snapshot/redux/dispatchActions';
32+
import { setDontShowShareSnapshot, setSharedSnapshot } from '../snapshot/redux/actions';
33+
import { initSharedSnapshot } from '../snapshot/redux/reducer';
34+
import { base_url } from '../routes/constants';
35+
import { api, METHOD } from '../../utils/api';
3136

3237

3338
const useStyles = makeStyles(theme => ({
@@ -67,7 +72,7 @@ export const SelectedCompoundList = memo(({ height }) => {
6772
const crossReferenceDialogRef = useRef();
6873
const scrollBarRef = useRef();
6974

70-
const { getNglView } = useContext(NglContext);
75+
const { getNglView, nglViewList } = useContext(NglContext);
7176
const stage = getNglView(VIEWS.MAJOR_VIEW) && getNglView(VIEWS.MAJOR_VIEW).stage;
7277

7378
const loadNextMolecules = () => {
@@ -172,75 +177,6 @@ export const SelectedCompoundList = memo(({ height }) => {
172177
return [...unionOfProps];
173178
};
174179

175-
const prepareHeader = (props, maxIdsCount) => {
176-
let header = getIdsHeader(maxIdsCount);
177-
178-
if (header.length > 0 && props.length > 0) {
179-
header += ',';
180-
}
181-
182-
for (let i = 0; i < props.length; i++) {
183-
const prop = props[i];
184-
if (i < props.length - 1) {
185-
header += `${encodeURIComponent(prop)},`;
186-
} else {
187-
header += `${encodeURIComponent(prop)}`;
188-
}
189-
}
190-
191-
return header;
192-
};
193-
194-
const convertCompoundToCsvLine = (compound, props, maxIdsCount) => {
195-
let line = '';
196-
197-
const molecule = compound.molecule;
198-
199-
line = prepareMolIds(compound, maxIdsCount);
200-
201-
if (line.length > 0 && props.length > 0) {
202-
line += ',';
203-
} else if (line.length === 0 && maxIdsCount > 0) {
204-
line += ',';
205-
}
206-
207-
let value = '';
208-
for (let i = 0; i < props.length; i++) {
209-
value = '';
210-
const prop = props[i];
211-
if (molecule.hasOwnProperty(prop)) {
212-
value = molecule[prop];
213-
} else {
214-
let mapOfNumScores = undefined;
215-
let mapOfTextScores = undefined;
216-
if (molecule.hasOwnProperty('numerical_scores')) {
217-
mapOfNumScores = molecule['numerical_scores'];
218-
}
219-
if (molecule.hasOwnProperty('text_scores')) {
220-
mapOfTextScores = molecule['text_scores'];
221-
}
222-
if (mapOfNumScores !== undefined) {
223-
if (mapOfNumScores.hasOwnProperty(prop)) {
224-
value = mapOfNumScores[prop];
225-
}
226-
}
227-
if (mapOfTextScores !== undefined) {
228-
if (mapOfTextScores.hasOwnProperty(prop)) {
229-
value = mapOfTextScores[prop];
230-
}
231-
}
232-
}
233-
234-
if (i < props.length - 1) {
235-
line += `${encodeURIComponent(value)},`;
236-
} else {
237-
line += `${encodeURIComponent(value)}`;
238-
}
239-
};
240-
241-
return line;
242-
};
243-
244180
const populateMolObject = (molObj, compound, props) => {
245181
const molecule = compound.molecule;
246182

@@ -305,50 +241,6 @@ export const SelectedCompoundList = memo(({ height }) => {
305241
return maxLength;
306242
};
307243

308-
const getIdsHeader = (maxIdsCount) => {
309-
let idsHeader = '';
310-
311-
for (let i = 0; i < maxIdsCount; i++) {
312-
if (i < maxIdsCount - 1) {
313-
idsHeader += `id${i},`;
314-
} else {
315-
idsHeader += `id${i}`;
316-
}
317-
}
318-
319-
return idsHeader;
320-
};
321-
322-
const prepareMolIds = (compound, maxIdsCount) => {
323-
let idsHeader = '';
324-
325-
if (compound.molecule.hasOwnProperty('compound_ids')) {
326-
const ids = compound.molecule['compound_ids'];
327-
for (let i = 0; i < maxIdsCount; i++) {
328-
if (i <= ids.length - 1) {
329-
const id = ids[i];
330-
if (i < ids.length - 1) {
331-
idsHeader += `${id},`;
332-
} else {
333-
idsHeader += `${id}`;
334-
}
335-
} else {
336-
if (i < maxIdsCount) {
337-
idsHeader += ',';
338-
}
339-
}
340-
};
341-
} else {
342-
for (let i = 0; i < maxIdsCount; i++) {
343-
if (i < maxIdsCount - 1) {
344-
idsHeader += ',';
345-
}
346-
}
347-
}
348-
349-
return idsHeader;
350-
};
351-
352244
const getUsedDatasets = (mols) => {
353245
const setOfDataSets = {};
354246
mols.forEach(mol => {
@@ -372,26 +264,42 @@ export const SelectedCompoundList = memo(({ height }) => {
372264
return molObj;
373265
}
374266

375-
const downloadAsCsv = () => {
376-
const usedDatasets = getUsedDatasets(moleculesObjectIDListOfCompoundsToBuy);
377-
const props = getSetOfProps(usedDatasets);
378-
let maxIdsCount = getMaxNumberOfCmpIds(moleculesObjectIDListOfCompoundsToBuy);
379-
let data = prepareHeader(props, maxIdsCount);
380-
381-
const listOfMols = [];
382-
383-
moleculesObjectIDListOfCompoundsToBuy.forEach(compound => {
384-
data += `\n${convertCompoundToCsvLine(compound, props, maxIdsCount)}`;
385-
let molObj = getEmptyMolObject(props, maxIdsCount);
386-
molObj = populateMolObject(molObj, compound, props)
387-
listOfMols.push(molObj);
267+
const downloadAsCsv = () => (dispatch, getState) => {
268+
dispatch(setDontShowShareSnapshot(true));
269+
dispatch(saveAndShareSnapshot(nglViewList, false)).then(() =>{
270+
const state = getState();
271+
const sharedSnapshot = state.snapshotReducers.sharedSnapshot;
272+
273+
dispatch(setSharedSnapshot(initSharedSnapshot));
274+
dispatch(setDontShowShareSnapshot(false));
275+
276+
const usedDatasets = getUsedDatasets(moleculesObjectIDListOfCompoundsToBuy);
277+
const props = getSetOfProps(usedDatasets);
278+
let maxIdsCount = getMaxNumberOfCmpIds(moleculesObjectIDListOfCompoundsToBuy);
279+
280+
const listOfMols = [];
281+
282+
moleculesObjectIDListOfCompoundsToBuy.forEach(compound => {
283+
let molObj = getEmptyMolObject(props, maxIdsCount);
284+
molObj = populateMolObject(molObj, compound, props);
285+
listOfMols.push(molObj);
286+
});
287+
288+
const reqObj = {title: sharedSnapshot.url, dict: listOfMols};
289+
const jsonString = JSON.stringify(reqObj);
290+
291+
api({
292+
url: `${base_url}/api/dicttocsv/`,
293+
method: METHOD.POST,
294+
data: jsonString
295+
}).then(resp => {
296+
var anchor = document.createElement('a');
297+
anchor.href = `${base_url}/api/dicttocsv/?file_url=${resp.data['file_url']}`;
298+
anchor.target = '_blank';
299+
anchor.download = 'download';
300+
anchor.click();
301+
});
388302
});
389-
const dataBlob = new Blob([data], { type: 'text/csv;charset=utf-8' });
390-
391-
const jsonString = JSON.stringify(listOfMols);
392-
console.log(jsonString);
393-
394-
FileSaver.saveAs(dataBlob, 'selectedCompounds.csv');
395303
};
396304

397305
const downloadAsSdf = async () => {
@@ -420,7 +328,7 @@ export const SelectedCompoundList = memo(({ height }) => {
420328
title="Selected Compounds"
421329
withTooltip
422330
headerActions={[
423-
<Button color="inherit" variant="text" onClick={downloadAsCsv} startIcon={<CloudDownload />} >
331+
<Button color="inherit" variant="text" onClick={() => dispatch(downloadAsCsv())} startIcon={<CloudDownload />} >
424332
Download CSV
425333
</Button>,
426334
<Button

js/components/snapshot/modals/modalShareSnapshot.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const ModalShareSnapshot = memo(({}) => {
2929
const classes = useStyles();
3030
const sharedSnapshot = useSelector(state => state.snapshotReducers.sharedSnapshot);
3131
const isLoadingSnapshotDialog = useSelector(state => state.snapshotReducers.isLoadingSnapshotDialog);
32+
const dontShowShareSnapshot = useSelector(state => state.snapshotReducers.dontShowShareSnapshot);
3233
const dispatch = useDispatch();
3334

3435
const openInNewTab = () => {
@@ -46,7 +47,7 @@ export const ModalShareSnapshot = memo(({}) => {
4647
};
4748

4849
return (
49-
<Modal open={sharedSnapshot.url !== null}>
50+
<Modal open={sharedSnapshot.url !== null && dontShowShareSnapshot === false}>
5051
{isLoadingSnapshotDialog === false ? (
5152
<>
5253
<DialogTitle id="form-dialog-title">{sharedSnapshot.title}</DialogTitle>

js/components/snapshot/redux/actions.js

+5
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ export const setSnapshotJustSaved = (saved) => ({
5050
type: constants.SET_SNAPSHOT_JUST_SAVED,
5151
payload: saved
5252
});
53+
54+
export const setDontShowShareSnapshot = (dontShow) => ({
55+
type: constants.SET_DONT_SHOW_SHARE_SNAPSHOT,
56+
payload: dontShow
57+
});

js/components/snapshot/redux/constants.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ export const constants = {
1919
SET_SELECTED_SNAPSHOT_TO_SWITCH: prefix + 'SET_SELECTED_SNAPSHOT_TO_SWITCH',
2020
SET_DISABLE_REDIRECT: prefix + 'SET_DISABLE_REDIRECT',
2121

22-
SET_SNAPSHOT_JUST_SAVED: prefix + 'SET_SNAPSHOT_JUST_SAVED'
22+
SET_SNAPSHOT_JUST_SAVED: prefix + 'SET_SNAPSHOT_JUST_SAVED',
23+
SET_DONT_SHOW_SHARE_SNAPSHOT: prefix + 'SET_DONT_SHOW_SHARE_SNAPSHOT'
2324
};

js/components/snapshot/redux/dispatchActions.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ export const createNewSnapshotWithoutStateModification = ({
423423
});
424424
};
425425

426-
export const saveAndShareSnapshot = nglViewList => async (dispatch, getState) => {
426+
export const saveAndShareSnapshot = ( nglViewList, showDialog = true ) => async (dispatch, getState) => {
427427
const state = getState();
428428
const targetId = state.apiReducers.target_on;
429429
const loggedInUserID = DJANGO_CONTEXT['pk'];
@@ -432,7 +432,9 @@ export const saveAndShareSnapshot = nglViewList => async (dispatch, getState) =>
432432

433433
if (targetId) {
434434
dispatch(captureScreenOfSnapshot());
435-
dispatch(setIsLoadingSnapshotDialog(true));
435+
if (showDialog) {
436+
dispatch(setIsLoadingSnapshotDialog(true));
437+
}
436438
const data = {
437439
title: ProjectCreationType.READ_ONLY,
438440
description: ProjectCreationType.READ_ONLY,
@@ -465,10 +467,14 @@ export const saveAndShareSnapshot = nglViewList => async (dispatch, getState) =>
465467
nglViewList
466468
})
467469
);
468-
469-
dispatch(setIsLoadingSnapshotDialog(false));
470+
471+
if (showDialog) {
472+
dispatch(setIsLoadingSnapshotDialog(false));
473+
}
470474
} catch (error) {
471-
dispatch(setIsLoadingSnapshotDialog(false));
475+
if (showDialog) {
476+
dispatch(setIsLoadingSnapshotDialog(false));
477+
}
472478
throw new Error(error);
473479
}
474480
}

js/components/snapshot/redux/reducer.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export const INITIAL_STATE = {
2222
isOpenModalSaveSnapshotBeforeExit: false,
2323
selectedSnapshotToSwitch: null,
2424
disableRedirect: false,
25-
snapshotJustSaved: false
25+
snapshotJustSaved: false,
26+
dontShowShareSnapshot: false
2627
};
2728

2829
export const snapshotReducers = (state = INITIAL_STATE, action = {}) => {
@@ -88,6 +89,11 @@ export const snapshotReducers = (state = INITIAL_STATE, action = {}) => {
8889
return Object.assign({}, state, {
8990
disableRedirect: action.payload
9091
});
92+
93+
case constants.SET_DONT_SHOW_SHARE_SNAPSHOT:
94+
return Object.assign({}, state, {
95+
dontShowShareSnapshot: action.payload
96+
});
9197

9298
case constants.SET_SNAPSHOT_JUST_SAVED: {
9399
return Object.assign({}, state, {

0 commit comments

Comments
 (0)