Skip to content

[dataquery] Fixed Import csv for Empty csv and submitting without uploading #9882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 27.0-release
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions modules/dataquery/jsx/definefilters.importcsvmodal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ function ImportCSVModal(props: {
* @returns {Promise} - a stub promise
*/
const submitPromise = () =>
new Promise((resolve) => {
new Promise((resolve, reject) => {
if (!csvFile) {
swal.fire({
type: 'error',
title: 'No CSV Uploaded',
text: 'Please upload a CSV file before submitting.',
});
reject();
return;
}
resolve(null);
}
);
Expand All @@ -44,12 +53,25 @@ function ImportCSVModal(props: {
title: 'Invalid CSV',
text: 'Could not parse CSV file',
});
setCSVFile(null);
return;
}

// Check for empty CSV file
const startLine = csvHeader ? 1 : 0;
if (!value.data || value.data.length <= startLine) {
swal.fire({
type: 'error',
title: 'Empty CSV',
text: 'The uploaded CSV file is empty.',
});
setCSVFile(null);
return;
}

// If candidates: validate 1 column
// If sessions: validate 2 columns
const expectedLength = (csvType === 'session' ? 2 : 1);
const startLine = csvHeader ? 1 : 0;

for (let i = startLine; i < value.data.length; i++) {
if (value.data[i].length != expectedLength) {
Expand All @@ -60,6 +82,7 @@ function ImportCSVModal(props: {
+ ' Got ' + value.data[i].length + ' on line ' +
(i+1) + '.',
});
setCSVFile(null);
return;
}
if (idType === 'CandID') {
Expand All @@ -71,6 +94,7 @@ function ImportCSVModal(props: {
+ ') on line '
+ (i+1) + '.',
});
setCSVFile(null);
return;
}
}
Expand Down Expand Up @@ -187,7 +211,7 @@ function ImportCSVModal(props: {
// Only 1 column, papaparse can't detect
// the delimiter if it's not explicitly
// specified.
if (csvType == 'candidate') {
if (csvType == 'candidate' || csvType == 'session') {
papaparseConfig.delimiter = ',';
}
Papa.parse(file, papaparseConfig);
Expand Down
Loading