@@ -28,6 +28,11 @@ import { VIEWS } from '../../constants/constants';
28
28
import FileSaver from 'file-saver' ;
29
29
import JSZip from 'jszip' ;
30
30
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' ;
31
36
32
37
33
38
const useStyles = makeStyles ( theme => ( {
@@ -67,7 +72,7 @@ export const SelectedCompoundList = memo(({ height }) => {
67
72
const crossReferenceDialogRef = useRef ( ) ;
68
73
const scrollBarRef = useRef ( ) ;
69
74
70
- const { getNglView } = useContext ( NglContext ) ;
75
+ const { getNglView, nglViewList } = useContext ( NglContext ) ;
71
76
const stage = getNglView ( VIEWS . MAJOR_VIEW ) && getNglView ( VIEWS . MAJOR_VIEW ) . stage ;
72
77
73
78
const loadNextMolecules = ( ) => {
@@ -172,75 +177,6 @@ export const SelectedCompoundList = memo(({ height }) => {
172
177
return [ ...unionOfProps ] ;
173
178
} ;
174
179
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
-
244
180
const populateMolObject = ( molObj , compound , props ) => {
245
181
const molecule = compound . molecule ;
246
182
@@ -305,50 +241,6 @@ export const SelectedCompoundList = memo(({ height }) => {
305
241
return maxLength ;
306
242
} ;
307
243
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
-
352
244
const getUsedDatasets = ( mols ) => {
353
245
const setOfDataSets = { } ;
354
246
mols . forEach ( mol => {
@@ -372,26 +264,42 @@ export const SelectedCompoundList = memo(({ height }) => {
372
264
return molObj ;
373
265
}
374
266
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
+ } ) ;
388
302
} ) ;
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' ) ;
395
303
} ;
396
304
397
305
const downloadAsSdf = async ( ) => {
@@ -420,7 +328,7 @@ export const SelectedCompoundList = memo(({ height }) => {
420
328
title = "Selected Compounds"
421
329
withTooltip
422
330
headerActions = { [
423
- < Button color = "inherit" variant = "text" onClick = { downloadAsCsv } startIcon = { < CloudDownload /> } >
331
+ < Button color = "inherit" variant = "text" onClick = { ( ) => dispatch ( downloadAsCsv ( ) ) } startIcon = { < CloudDownload /> } >
424
332
Download CSV
425
333
</ Button > ,
426
334
< Button
0 commit comments