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