@@ -1342,6 +1342,98 @@ class Browser extends DashboardView {
1342
1342
document . body . removeChild ( element ) ;
1343
1343
}
1344
1344
1345
+ async confirmImport ( file ) {
1346
+ this . setState ( { showImportDialog : null } ) ;
1347
+ const className = this . props . params . className ;
1348
+ const classColumns = this . getClassColumns ( className , false ) ;
1349
+ const columnsObject = { } ;
1350
+ classColumns . forEach ( ( column ) => {
1351
+ columnsObject [ column . name ] = column ;
1352
+ } ) ;
1353
+ const { base64, type} = file . _source ;
1354
+ if ( type === 'text/csv' ) {
1355
+ const csvToArray = ( text ) => {
1356
+ let p = '' , row = [ '' ] , ret = [ row ] , i = 0 , r = 0 , s = ! 0 , l ;
1357
+ for ( l of text ) {
1358
+ if ( '"' === l ) {
1359
+ if ( s && l === p ) row [ i ] += l ;
1360
+ s = ! s ;
1361
+ } else if ( ',' === l && s ) l = row [ ++ i ] = '' ;
1362
+ else if ( '\n' === l && s ) {
1363
+ if ( '\r' === p ) row [ i ] = row [ i ] . slice ( 0 , - 1 ) ;
1364
+ row = ret [ ++ r ] = [ l = '' ] ; i = 0 ;
1365
+ } else row [ i ] += l ;
1366
+ p = l ;
1367
+ }
1368
+ return ret ;
1369
+ } ;
1370
+ const csv = atob ( base64 ) ;
1371
+ const [ columns , ...rows ] = csvToArray ( csv ) ;
1372
+ await Parse . Object . saveAll ( rows . filter ( row => row . join ( ) !== '' ) . map ( row => {
1373
+ const json = { className} ;
1374
+ for ( let i = 1 ; i < row . length ; i ++ ) {
1375
+ const column = columns [ i ] ;
1376
+ const value = row [ i ] ;
1377
+ if ( value === 'null' ) {
1378
+ continue ;
1379
+ }
1380
+ const { type, targetClass, name} = columnsObject [ column ] || { } ;
1381
+ if ( type === 'Relation' ) {
1382
+ json [ column ] = {
1383
+ __type : 'Relation' ,
1384
+ className : targetClass ,
1385
+ } ;
1386
+ continue ;
1387
+ }
1388
+ if ( type === 'Pointer' ) {
1389
+ json [ column ] = {
1390
+ __type : 'Pointer' ,
1391
+ className : targetClass ,
1392
+ objectId : value ,
1393
+ } ;
1394
+ continue ;
1395
+ }
1396
+ if ( name === 'ACL' ) {
1397
+ json . ACL = new Parse . ACL ( JSON . parse ( value ) ) ;
1398
+ continue ;
1399
+ }
1400
+ if ( type === 'Date' ) {
1401
+ json [ column ] = new Date ( value ) ;
1402
+ continue ;
1403
+ }
1404
+ if ( type === 'Boolean' ) {
1405
+ json [ column ] = value === 'true' ;
1406
+ continue ;
1407
+ }
1408
+ if ( type === 'String' ) {
1409
+ json [ column ] = value ;
1410
+ continue ;
1411
+ }
1412
+ if ( type === 'Number' ) {
1413
+ json [ column ] = Number ( value ) ;
1414
+ continue ;
1415
+ }
1416
+ try {
1417
+ json [ column ] = JSON . parse ( value ) ;
1418
+ } catch ( e ) {
1419
+ /* */
1420
+ }
1421
+ }
1422
+ return Parse . Object . fromJSON ( json , false , true ) ;
1423
+ } ) , { useMasterKey : true } ) ;
1424
+ }
1425
+ this . refresh ( ) ;
1426
+
1427
+ // Deliver to browser to download file
1428
+ // const element = document.createElement('a');
1429
+ // const file = new Blob([csvString], { type: 'text/csv' });
1430
+ // element.href = URL.createObjectURL(file);
1431
+ // element.download = `${className}.csv`;
1432
+ // document.body.appendChild(element); // Required for this to work in FireFox
1433
+ // element.click();
1434
+ // document.body.removeChild(element);
1435
+ }
1436
+
1345
1437
getClassRelationColumns ( className ) {
1346
1438
const currentClassName = this . props . params . className ;
1347
1439
return this . getClassColumns ( className , false )
@@ -1664,7 +1756,7 @@ class Browser extends DashboardView {
1664
1756
< ImportDialog
1665
1757
className = { className }
1666
1758
onCancel = { ( ) => this . setState ( { showImportDialog : false } ) }
1667
- onConfirm = { ( ) => this . exportClass ( className ) } />
1759
+ onConfirm = { ( file ) => this . confirmImport ( file ) } />
1668
1760
) ;
1669
1761
} else if ( this . state . showAttachRowsDialog ) {
1670
1762
extras = (
0 commit comments