Skip to content

Commit 16c5e30

Browse files
committed
wip
1 parent 89ccfaf commit 16c5e30

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
@@ -128,8 +128,8 @@ export default class BrowserCell extends Component {
128128
} else if (this.props.type === 'Object' || this.props.type === 'Bytes') {
129129
this.copyableValue = content = JSON.stringify(this.props.value);
130130
} else if (this.props.type === 'File') {
131-
const fileName = this.props.value.url() ? getFileName(this.props.value) : 'Uploading\u2026';
132-
content = <Pill value={fileName} fileDownloadLink={this.props.value.url()} shrinkablePill />;
131+
const fileName = this.props.value.url?.() ? getFileName(this.props.value) : 'Uploading\u2026';
132+
content = <Pill value={fileName} fileDownloadLink={this.props.value.url?.()} shrinkablePill />;
133133
this.copyableValue = fileName;
134134
} else if (this.props.type === 'ACL') {
135135
let pieces = [];

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

+93-1
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,98 @@ class Browser extends DashboardView {
13421342
document.body.removeChild(element);
13431343
}
13441344

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+
13451437
getClassRelationColumns(className) {
13461438
const currentClassName = this.props.params.className;
13471439
return this.getClassColumns(className, false)
@@ -1664,7 +1756,7 @@ class Browser extends DashboardView {
16641756
<ImportDialog
16651757
className={className}
16661758
onCancel={() => this.setState({ showImportDialog: false })}
1667-
onConfirm={() => this.exportClass(className)} />
1759+
onConfirm={(file) => this.confirmImport(file)} />
16681760
);
16691761
}else if (this.state.showAttachRowsDialog) {
16701762
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)