Skip to content
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

Fixing button to download registered users #623

Merged
merged 1 commit into from
May 30, 2024
Merged
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
50 changes: 29 additions & 21 deletions src/pages/LEVANTE/RegisterUsers.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@
</PvDataTable>

<div class="submit-container">
<PvButton
v-if="registeredUsers.length"
label="Download Registered Users"
@click="addAccountToCSV(registeredUsers)"
/>
<PvButton v-if="registeredUsers.length" label="Download Registered Users" @click="downloadCSV" />
<PvButton
v-else
:label="activeSubmit ? 'Registering Users' : 'Start Registration'"
Expand Down Expand Up @@ -326,18 +322,20 @@ async function submitUsers() {
}
}

const csvBlob = ref(null);
const csvURL = ref(null);

function convertUsersToCSV() {
const filename = 'registered-users.csv';
const headerObj = toRaw(rawUserFile.value[0]);

// Convert Objects to CSV String
const csvHeader = Object.keys(headerObj).join(',') + '\n'; // Get header from keys of first object
const csvHeader = Object.keys(headerObj).join(',') + '\n';
const csvRows = rawUserFile.value
.map((obj) =>
Object.values(obj)
.map((value) => {
if (value === null || value === undefined) return ''; // Handle null/undefined values
return `"${value.toString().replace(/"/g, '""')}"`; // Handle values containing commas or quotes
if (value === null || value === undefined) return '';
return `"${value.toString().replace(/"/g, '""')}"`;
})
.join(','),
)
Expand All @@ -346,21 +344,31 @@ function convertUsersToCSV() {
const csvString = csvHeader + csvRows;

// Create Blob from CSV String
const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8;' });
csvBlob.value = new Blob([csvString], { type: 'text/csv;charset=utf-8;' });

// Create URL from Blob
csvURL.value = URL.createObjectURL(csvBlob.value);

// Create Download Link
const link = document.createElement('a');
const url = URL.createObjectURL(blob);
link.setAttribute('href', url);
link.setAttribute('download', filename);
document.body.appendChild(link); // Required for Firefox
// Initiate download
downloadCSV();
}

function downloadCSV() {
const filename = 'registered-users.csv';

// Trigger the Download
link.click();
if (csvURL.value) {
// Create Download Link
const link = document.createElement('a');
link.setAttribute('href', csvURL.value);
link.setAttribute('download', filename);
document.body.appendChild(link); // Required for Firefox

// Cleanup
document.body.removeChild(link);
URL.revokeObjectURL(url);
// Trigger the Download
link.click();

// Cleanup
document.body.removeChild(link);
}
}

function addErrorUser(user, error) {
Expand Down
Loading