Skip to content

Commit

Permalink
Fix learnt today and merge imported streaks
Browse files Browse the repository at this point in the history
  • Loading branch information
cubetastic33 committed Feb 15, 2024
1 parent 12a3f70 commit c7d683f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion static/pwa/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const version = "20240211-2::";
const version = "20240214-0::";

// Caches for different resources
const core_cache_name = version + "core";
Expand Down
19 changes: 13 additions & 6 deletions static/scripts/streaks.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ function draw_map(end_date, start_year=true) {
}

const today = new Date();
// If they haven't learnt today yet, take yesterday
const learnt_today = days_learnt[numerify(today)] || days_learnt[numerify(new Date(today - DAY))] || 0;
// If the user learnt today, add 1 to the current streak
if (learnt_today === 0) current_streak = 0;

const learnt_today = days_learnt[numerify(today)] || 0;
if (current_streak > longest_streak) longest_streak = current_streak;

// If the user hasn't learnt today OR yesterday, set current_streak to 0
if (!learnt_today > 0 && !days_learnt[numerify(new Date(today - DAY))] > 0) current_streak = 0;

$('#longest').text(`${longest_streak} day${longest_streak === 1 ? '' : 's'}`);
$('#current').text(`${current_streak} day${current_streak === 1 ? '' : 's'}`);
Expand Down Expand Up @@ -167,7 +166,15 @@ $('#replace').on('click', async () => {
$('#merge').on('click', async () => {
const contents = JSON.parse(await $file[0].files[0].text());
const current = JSON.parse(localStorage.getItem('days_learnt')) || {};
localStorage.setItem('days_learnt', JSON.stringify({ ...current, ...contents }));
console.log('current', current);
console.log('contents', contents);
for (const day in contents) {
console.log('day', day);
if (current[day]) current[day] += contents[day];
else current[day] = contents[day];
}
console.log('current', current);
localStorage.setItem('days_learnt', JSON.stringify(current));
draw_map(new Date(), false);
$('#import_streaks_dialog').hide('slow').then($('#import_streaks_dialog + .overlay').hide());
});

0 comments on commit c7d683f

Please sign in to comment.