Skip to content

Commit

Permalink
Add csv functions
Browse files Browse the repository at this point in the history
  • Loading branch information
npbreland committed Sep 18, 2023
1 parent c656b97 commit 24a65a8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
50 changes: 50 additions & 0 deletions apps/habitbuilder/csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const CSV_HEADER = "date,question,response\n";

// Cleans the CSV data by removing duplicate entries
document.addEventListener('click', function (event) {
if (event.target.id === 'download-csv') {
Util.readStorage("habitbuilder.data.csv", data=>{
console.log(data);
});

return;
// String representation of the CSV post-cleaning
let cleanCsvStr = CSV_HEADER;

Check warning on line 12 in apps/habitbuilder/csv.js

View workflow job for this annotation

GitHub Actions / build

Unreachable code
Util.readStorage("habitbuilder.data.csv", data=>{
let lines = data.split("\n");

// Don't bother with the first line, which is a header
lines = lines.slice(1, lines.length-1);

const memory = {
date: null,
questions: {}
};

let line, parts;
while (lines.length > 0) {
line = shift(lines);
parts = line.split(",");

if (parts[0] !== memory.date && memory.date !== null) {
// New date, so write out the memory

for (let [question, response] of Object.entries(memory.questions)) {
cleanCsvStr += memory.date + "," + question + "," + response + "\n";
}

memory.date = parts[0];
memory.questions = {};
}

// Overwrite the memory with the new value (we only care about the last)
memory.questions[parts[1]] = parts[2];
}
});
} else if (e.target.id === 'clear-csv') {
if (confirm("Are you sure you want to clear the CSV data?")) {
// Reset the CSV data to just the header
Util.writeStorage("habitbuilder.data.csv", CSV_HEADER);
}
}
});
15 changes: 10 additions & 5 deletions apps/habitbuilder/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@
<link rel="stylesheet" href="../../css/spectre.min.css">
</head>
<body>
<button id="download-csv" class="btn btn-default">Download CSV</button>
<button id="clear-csv" class="btn btn-danger">Clear CSV</button>
<h2>Settings</h2>
<div id="settings"></div>
<button class="btn btn-default" id="btnSave">Save</button>
<button class="btn btn-primary" id="btnSave">Save</button>

<script src="../../core/lib/interface.js"></script>
<script src="settings-form.js"></script>
<script src="csv.js"></script>
<script>

const FILENAME = "habitbuilder.settings.json";

const SETTINGS_FILE = "habitbuilder.settings.json";

const settingsElement = document.getElementById("settings");

function disableFormInput() {
document.querySelectorAll(".form-input").forEach(el => el.disabled = true);
document.querySelectorAll(".btn").forEach(el => el.disabled = true);
}

function getData() {
function getSettings() {
// show loading window
Util.showModal("Loading...");
Util.readStorage(FILENAME, data=>{
Util.readStorage(SETTINGS_FILE, data=>{
const settings = JSON.parse(data || "[]");

// remove window
Expand All @@ -48,7 +53,7 @@ <h2>Settings</h2>

// Called when app starts
function onInit() {
getData();
getSettings();
}
</script>
</body>
Expand Down

0 comments on commit 24a65a8

Please sign in to comment.