mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-01 05:50:35 +03:00
69782e7aca
no issue - Disabled import button if mapping screen has 0 members to be imported - Updated UI logic for no-member upload case - Used bullet list for email summary
74 lines
1.9 KiB
JavaScript
74 lines
1.9 KiB
JavaScript
import Component from '@glimmer/component';
|
|
import MemberImportError from 'ghost-admin/errors/member-import-error';
|
|
import papaparse from 'papaparse';
|
|
import {action} from '@ember/object';
|
|
import {isNone} from '@ember/utils';
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
export default class CsvFileMapping extends Component {
|
|
@tracked
|
|
error = null;
|
|
|
|
@tracked
|
|
fileData = null;
|
|
|
|
mappingResult = {};
|
|
|
|
constructor(...args) {
|
|
super(...args);
|
|
this.parseFileAndGenerateMapping(this.args.file);
|
|
}
|
|
|
|
parseFileAndGenerateMapping(file) {
|
|
papaparse.parse(file, {
|
|
header: true,
|
|
skipEmptyLines: true,
|
|
complete: (result) => {
|
|
if (result.data && result.data.length) {
|
|
this.fileData = result.data;
|
|
} else {
|
|
this.fileData = [];
|
|
}
|
|
this.args.setFileData(this.fileData);
|
|
}
|
|
});
|
|
}
|
|
|
|
get hasFileData() {
|
|
return !isNone(this.fileData);
|
|
}
|
|
|
|
@action
|
|
setMapping(mapping) {
|
|
if (this.fileData.length === 0) {
|
|
this.error = new MemberImportError({
|
|
message: 'File is empty, nothing to import. Please select a different file.'
|
|
});
|
|
} else if (!mapping.getKeyByValue('email')) {
|
|
this.error = new MemberImportError({
|
|
message: 'Please map "Email" to one of the fields in the CSV.'
|
|
});
|
|
} else {
|
|
this.error = null;
|
|
}
|
|
|
|
this.mapping = mapping;
|
|
this.setMappingResult();
|
|
}
|
|
|
|
@action
|
|
updateLabels(labels) {
|
|
this.labels = labels;
|
|
this.setMappingResult();
|
|
}
|
|
|
|
setMappingResult() {
|
|
this.args.setMappingResult({
|
|
mapping: this.mapping,
|
|
labels: this.labels,
|
|
membersCount: this.fileData?.length,
|
|
error: this.error
|
|
});
|
|
}
|
|
}
|