-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create csvUploader : issue => Pouvoir charger une liste d'idep * Install ppaparse csv parser & material ui lab for alerting component * add alert on success - warning - failed, import csv, * add context value * modify alert system & setup context * fix yarn build error * fix dep array setError * css delete modal * fix error with warning message * fix warning message & add console error * fix showAlert error.message * remove inline style & add limit 10 characters in campaignLabel * add error & setError to context & move useEffect * fix csvImport div * add availableSessions & organisationalUnits to context * remove filter on uppercase * bump version to 2.2.0
- Loading branch information
1 parent
9c84dba
commit bde08ce
Showing
7 changed files
with
254 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Papa from 'papaparse'; | ||
export const handleCSVUpload = async (event, setInterviewers, setInvalidValues, showAlert) => { | ||
const file = event.target.files[0]; | ||
const reader = new FileReader(); | ||
|
||
reader.onload = async e => { | ||
const contents = e.target.result; | ||
const newInvalidValues = []; // Use a temporary array to store invalid values | ||
|
||
// Use Papaparse to parse the CSV file | ||
Papa.parse(contents, { | ||
delimiter: ',', // Set the appropriate delimiter | ||
header: true, // Specify if the first row is a header (containing column names) | ||
dynamicTyping: true, // Attempt to automatically detect data types | ||
complete: result => { | ||
// 'result.data' contains the parsed data from the CSV | ||
const allValues = []; | ||
|
||
// Iterate through each row of the CSV | ||
result.data.forEach(row => { | ||
// Iterate through each column of the row | ||
Object.keys(row).forEach(columnName => { | ||
const value = row[columnName]; | ||
if (value !== null && typeof value === 'string' && value.trim().length === 6) { | ||
allValues.push(value.trim().toUpperCase()); | ||
} else { | ||
if (value !== null) { | ||
newInvalidValues.push(value); // Store invalid values | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
// Update the 'interviewers' state with valid values | ||
setInterviewers(allValues.map((value, index) => ({ id: value, index }))); | ||
// Update the 'invalidValues' state with invalid values | ||
setInvalidValues(newInvalidValues); | ||
|
||
// Show the alert with the message including invalidValues | ||
if (newInvalidValues.length > 0) { | ||
showAlert( | ||
`The following elements were not considered (expected: 6 uppercase characters): ${newInvalidValues.join( | ||
', ' | ||
)}`, | ||
'warning' | ||
); | ||
} | ||
}, | ||
error: error => { | ||
showAlert(`Erreur lors de l'analyse du fichier CSV : ${error.message}`, 'error'); | ||
console.error('Error parsing the CSV file:', error.message); | ||
}, | ||
}); | ||
}; | ||
|
||
reader.readAsText(file); | ||
}; |
Oops, something went wrong.