Skip to content

Commit 78dfc29

Browse files
committed
wip
1 parent 749e6e3 commit 78dfc29

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

src/components/BrowserCell/BrowserCell.react.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ export default class BrowserCell extends Component {
127127
} else if (this.props.type === 'Object' || this.props.type === 'Bytes') {
128128
this.copyableValue = content = JSON.stringify(this.props.value);
129129
} else if (this.props.type === 'File') {
130-
const fileName = this.props.value.url() ? getFileName(this.props.value) : 'Uploading\u2026';
131-
content = <Pill value={fileName} fileDownloadLink={this.props.value.url()} shrinkablePill />;
130+
const fileName = this.props.value.url?.() ? getFileName(this.props.value) : 'Uploading\u2026';
131+
content = <Pill value={fileName} fileDownloadLink={this.props.value.url?.()} shrinkablePill />;
132132
this.copyableValue = fileName;
133133
} else if (this.props.type === 'ACL') {
134134
let pieces = [];

src/dashboard/Data/Browser/Browser.react.js

+93-1
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,98 @@ class Browser extends DashboardView {
13521352
document.body.removeChild(element);
13531353
}
13541354

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+
13551447
getClassRelationColumns(className) {
13561448
const currentClassName = this.props.params.className;
13571449
return this.getClassColumns(className, false)
@@ -1674,7 +1766,7 @@ class Browser extends DashboardView {
16741766
<ImportDialog
16751767
className={className}
16761768
onCancel={() => this.setState({ showImportDialog: false })}
1677-
onConfirm={() => this.exportClass(className)} />
1769+
onConfirm={(file) => this.confirmImport(file)} />
16781770
);
16791771
}else if (this.state.showAttachRowsDialog) {
16801772
extras = (

src/dashboard/Data/Browser/ImportDialog.react.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ export default class ImportDialog extends React.Component {
6666
icon='up-outline'
6767
iconSize={40}
6868
title={`Import Data into ${this.props.className}`}
69-
subtitle='Note: If rows have a className, they will be imported into that class.'
69+
subtitle='Note: Please make sure columns are defined in SCHEMA to import.'
7070
confirmText='Import'
7171
cancelText='Cancel'
7272
disabled={!this.state.file}
7373
buttonsInCenter={true}
7474
onCancel={this.props.onCancel}
75-
onConfirm={this.props.onConfirm}>
75+
onConfirm={() => this.props.onConfirm(this.state.file)}>
7676
<div style={{ padding: '25px' }}>
7777
{this.state.file && <Pill value={getFileName(this.state.file) }/>}
7878
<div style={{ cursor: 'pointer' }}>

0 commit comments

Comments
 (0)