Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy committed Jan 19, 2023
1 parent 749e6e3 commit 78dfc29
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/components/BrowserCell/BrowserCell.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export default class BrowserCell extends Component {
} else if (this.props.type === 'Object' || this.props.type === 'Bytes') {
this.copyableValue = content = JSON.stringify(this.props.value);
} else if (this.props.type === 'File') {
const fileName = this.props.value.url() ? getFileName(this.props.value) : 'Uploading\u2026';
content = <Pill value={fileName} fileDownloadLink={this.props.value.url()} shrinkablePill />;
const fileName = this.props.value.url?.() ? getFileName(this.props.value) : 'Uploading\u2026';
content = <Pill value={fileName} fileDownloadLink={this.props.value.url?.()} shrinkablePill />;
this.copyableValue = fileName;
} else if (this.props.type === 'ACL') {
let pieces = [];
Expand Down
94 changes: 93 additions & 1 deletion src/dashboard/Data/Browser/Browser.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,98 @@ class Browser extends DashboardView {
document.body.removeChild(element);
}

async confirmImport(file) {
this.setState({ showImportDialog: null });
const className = this.props.params.className;
const classColumns = this.getClassColumns(className, false);
const columnsObject = {};
classColumns.forEach((column) => {
columnsObject[column.name] = column;
});
const { base64, type} = file._source;
if (type === 'text/csv') {
const csvToArray =(text) => {
let p = '', row = [''], ret = [row], i = 0, r = 0, s = !0, l;
for (l of text) {
if ('"' === l) {
if (s && l === p) row[i] += l;
s = !s;
} else if (',' === l && s) l = row[++i] = '';
else if ('\n' === l && s) {
if ('\r' === p) row[i] = row[i].slice(0, -1);
row = ret[++r] = [l = '']; i = 0;
} else row[i] += l;
p = l;
}
return ret;
};
const csv = atob(base64);
const [columns, ...rows] = csvToArray(csv);
await Parse.Object.saveAll(rows.filter(row => row.join() !== '').map(row => {
const json = {className};
for (let i = 1; i < row.length; i++) {
const column = columns[i];
const value = row[i];
if (value === 'null') {
continue;
}
const {type, targetClass, name} = columnsObject[column] || {};
if (type === 'Relation') {
json[column] = {
__type: 'Relation',
className: targetClass,
};
continue;
}
if (type === 'Pointer') {
json[column] = {
__type: 'Pointer',
className: targetClass,
objectId: value,
};
continue;
}
if (name === 'ACL') {
json.ACL = new Parse.ACL(JSON.parse(value));
continue;
}
if (type === 'Date') {
json[column] = new Date(value);
continue;
}
if (type === 'Boolean') {
json[column] = value === 'true';
continue;
}
if (type === 'String') {
json[column] = value;
continue;
}
if (type === 'Number') {
json[column] = Number(value);
continue;
}
try {
json[column] = JSON.parse(value);
} catch (e) {
/* */
}
}
return Parse.Object.fromJSON(json, false, true);
}), {useMasterKey: true});
}
this.refresh();

// Deliver to browser to download file
// const element = document.createElement('a');
// const file = new Blob([csvString], { type: 'text/csv' });
// element.href = URL.createObjectURL(file);
// element.download = `${className}.csv`;
// document.body.appendChild(element); // Required for this to work in FireFox
// element.click();
// document.body.removeChild(element);
}

getClassRelationColumns(className) {
const currentClassName = this.props.params.className;
return this.getClassColumns(className, false)
Expand Down Expand Up @@ -1674,7 +1766,7 @@ class Browser extends DashboardView {
<ImportDialog
className={className}
onCancel={() => this.setState({ showImportDialog: false })}
onConfirm={() => this.exportClass(className)} />
onConfirm={(file) => this.confirmImport(file)} />
);
}else if (this.state.showAttachRowsDialog) {
extras = (
Expand Down
4 changes: 2 additions & 2 deletions src/dashboard/Data/Browser/ImportDialog.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ export default class ImportDialog extends React.Component {
icon='up-outline'
iconSize={40}
title={`Import Data into ${this.props.className}`}
subtitle='Note: If rows have a className, they will be imported into that class.'
subtitle='Note: Please make sure columns are defined in SCHEMA to import.'
confirmText='Import'
cancelText='Cancel'
disabled={!this.state.file}
buttonsInCenter={true}
onCancel={this.props.onCancel}
onConfirm={this.props.onConfirm}>
onConfirm={() => this.props.onConfirm(this.state.file)}>
<div style={{ padding: '25px' }}>
{this.state.file && <Pill value={getFileName(this.state.file) }/>}
<div style={{ cursor: 'pointer' }}>
Expand Down

0 comments on commit 78dfc29

Please sign in to comment.