Skip to content

Commit 23f2474

Browse files
committed
Fixing CSV export and adding download button
1 parent 5117f42 commit 23f2474

4 files changed

Lines changed: 38 additions & 29 deletions

File tree

dr-patient-view-controler.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { drPatientLib } from "./dr-patient-view-lib.js";
2-
2+
import { exportCSVFile } from "./exportToCSV.js";
33
/**
44
* Based on
55
* - drApiConnecion
@@ -23,10 +23,25 @@ const tableHeaders = {
2323
description: 'Description'
2424
}
2525

26+
const downloadHeaders = Object.assign({
27+
streamId: 'Stream Id',
28+
eventType: 'Event Type'
29+
}, tableHeaders);
30+
2631
async function refresh () {
2732
const { patientApiEndpoint, questionaryId } = getRequestFrormApiEndPoint();
28-
console.log('>> zz', patientApiEndpoint, questionaryId);
29-
const lines = await drPatientLib.getPatientData(patientApiEndpoint, questionaryId);
33+
const { infos, lines } = await drPatientLib.getPatientData(patientApiEndpoint, questionaryId);
34+
console.log('>> zz', infos);
35+
// -- set patient Id
36+
const username = infos.user.username;
37+
document.getElementById('patient-label').innerHTML = username;
38+
39+
// -- set download button
40+
document.getElementById('button-download').onclick = () => {
41+
exportCSVFile(downloadHeaders, lines, username + '-' + questionaryId);
42+
};
43+
44+
// -- update table
3045
const table = document.getElementById('patient-data-table');
3146
const headerRow = table.insertRow(-1);
3247
for (const thLabel of Object.values(tableHeaders)) {

dr-patient-view-lib.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ async function getPatientData (patientApiEndoint, questionaryId) {
2929
}
3030

3131
const connection = new Pryv.Connection(patientApiEndoint);
32+
const infos = await connection.accessInfo();
3233
await connection.getEventsStreamed(queryParams, forEachEvent);
33-
return patientData;
34+
return { infos, lines: patientData };
3435
}
3536

3637

dr-patient-view.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
<div class="container">
1919
<img src="https://healthdatasafe.github.io/style/images/Horizontal/hds-logo-hz-1024x400-hz-color.png" alt="Logo" width="300px">
2020
<h1>Demo Form - Dr's Patient View</h1>
21+
<a href="dr.html" class="btn btn-primary">HOME</a>
2122
<div class="card">
2223
<div class="card-patient">
23-
Patient: <h2 class="patient-label">Patient X</h2>
24+
<h2> Patient: <span id="patient-label">Patient X</span></h2>
2425
</div>
2526
</div>
2627
<div class="card" id="data-view">
28+
<button id="button-download" class="btn btn-secondary">Download</button>
2729
<table id="patient-data-table" class="table">
2830

2931
</table>

exportToCSV.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
1-
function convertToCSV(objArray) {
2-
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
3-
var str = '';
4-
5-
for (var i = 0; i < array.length; i++) {
6-
var line = '';
7-
for (var index in array[i]) {
8-
if (line != '') line += ','
9-
10-
line += array[i][index];
11-
}
12-
13-
str += line + '\r\n';
14-
}
15-
16-
return str;
1+
/**
2+
* convert all " to '' and add ","
3+
*/
4+
function fenceLine(items) {
5+
return '"' + items.map(i => i.replaceAll('"','\'\'')).join('","') + '"\r\n';
176
}
187

19-
export function exportCSVFile(headers, items, fileTitle) {
20-
if (headers) {
21-
items.unshift(headers);
22-
}
23-
24-
// Convert Object to JSON
25-
var jsonObject = JSON.stringify(items);
8+
export function exportCSVFile(headers, lines, fileTitle) {
9+
let csv = fenceLine(Object.values(headers));
2610

27-
var csv = convertToCSV(jsonObject);
11+
for (const line of lines) {
12+
const items = [];
13+
for (const key of Object.keys(headers)) {
14+
const v = line[key];
15+
items.push(v == null ? '': v + '');
16+
}
17+
csv += fenceLine(items);
18+
}
2819

2920
var exportedFilenmae = fileTitle + '.csv' || 'export.csv';
3021

0 commit comments

Comments
 (0)