Skip to content

Commit

Permalink
fix bug issue #50, #54
Browse files Browse the repository at this point in the history
  • Loading branch information
architec committed Nov 16, 2021
1 parent a1a97c2 commit 01022bb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 67 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leetcode-mistake-tracker",
"version": "1.1.0",
"version": "2.0.0",
"description": "Help LeetCode users keep track of the LeetCode questions they would like to re-do in the future",
"license": "MIT",
"repository": {
Expand Down
37 changes: 37 additions & 0 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,40 @@ export const isPast = (someDate) => {
const today = new Date();
return someDate < today;
};

export const getTitleFromUrl = (redoURI) => {
const spliceStart = redoURI.indexOf('problems/') + 'problems/'.length;
const spliceEnd = redoURI.indexOf('/', spliceStart);
let displayTitle = redoURI.slice(spliceStart, spliceEnd);
const lastSlash = displayTitle.lastIndexOf('/');
displayTitle = displayTitle.slice(lastSlash + 1);
return displayTitle;
}

/**
*
* @param {ISOString} startDate
* @param {ISOString} endDate
* @source https://stackoverflow.com/a/13904120
* @returns difference in minutes
*/
function diffInMinutes(startDate, endDate) {
// get total seconds between the times
var delta = Math.abs(endDate - startDate) / 1000;

// calculate (and subtract) whole days
var days = Math.floor(delta / 86400);
delta -= days * 86400;

// calculate (and subtract) whole hours
var hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;

// calculate (and subtract) whole minutes
var minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;

// what's left is seconds
var seconds = delta % 60; // in theory the modulus is not required
return minutes;
}
35 changes: 0 additions & 35 deletions src/pages/Background/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1 @@
// const main = () => {
// chrome.storage.sync.get('data', function (items) {
// let redos = items.data;
// printLine('Checking for Notifications');
// for(let i = 0; i < redos.length; i++) {
// const reminderDate = new Date(redos[i].reminderDate);
// console.log(`isToday: `, isToday(reminderDate));
// if (isToday(reminderDate) || isPast(reminderDate)) {
// console.log(`${redos[i].id} isToday`)
// var opt = {
// type: "basic",
// title: "Primary Title",
// message: "Primary message to display",
// iconUrl: "url_to_small_icon"
// }

// chrome.notifications.create(redos[i].uri, opt);
// return;
// }
// }
// });
// }

// main();
// console.log("Please wait...");
// chrome.runtime.onInstalled.addListener((reason) => {
// chrome.runtime.onInstalled.addListener(function(details){
// if(details.reason == "install"){
// //call a function to handle a first install
// console.log("install")
// }else if(details.reason == "update"){
// //call a function to handle an update
// console.log("update")
// }
// });
// });
9 changes: 5 additions & 4 deletions src/pages/Content/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { printLine } from './modules/print';
import { isToday, isPast } from './../../helper';
import { getTitleFromUrl } from './../../helper'

printLine('Content Script loaded');

Expand Down Expand Up @@ -28,20 +28,21 @@ const timeValue = setInterval(function () {
theButton.addEventListener('click', function () {
chrome.storage.sync.get('data', function (items) {
let redos;
const title = getTitleFromUrl(redo.uri);
if (Object.keys(items).length === 0) {
printLine('items is empty');
redos = [];
} else {
redos = items.data;
for (let i = 0; i < redos.length; i++) {
console.log(redos[i].uri === redo.uri);
if (redos[i].uri === redo.uri) {
if (redos[i].id === title) {
alert('You already marked this question');
return;
}
}
}
redo.id = redos.length + 1;
// redo.id = redos.length + 1;
redo.id = title;
redos.push(redo);
redos.sort(function (a, b) {
return new Date(a.reminderDate) - new Date(b.reminderDate);
Expand Down
47 changes: 20 additions & 27 deletions src/pages/Popup/Popup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,9 @@ import AssignmentIcon from '@mui/icons-material/Assignment';
import DeleteIcon from '@mui/icons-material/Delete';
import { ListItemSecondaryAction } from '@mui/material';

function WithoutTime(dateTime) {
var date = new Date(dateTime);
date.setHours(0, 0, 0, 0);
return date;
}

function ISOStringToDate(ISOString) {
return ISOString.slice(0, 10);
}
// helper functions
import { getTitleFromUrl } from '../../helper';

function diffInDays(startDate, endDate) {
const diffInMs = new Date(endDate) - new Date(startDate);
const diffInDays = diffInMs / (1000 * 60 * 60 * 24);
return Math.floor(diffInDays);
}

class Popup extends Component {
constructor(props) {
Expand Down Expand Up @@ -97,17 +85,22 @@ class Popup extends Component {
render() {
const itemsList = this.state.data.map((redo) => {
const redoURI = redo.uri;
const spliceStart = redoURI.indexOf('problems/') + 'problems/'.length;
const spliceEnd = redoURI.indexOf('/', spliceStart);
let displayTitle = redo.uri.slice(spliceStart, spliceEnd);
const lastSlash = displayTitle.lastIndexOf('/');
displayTitle = displayTitle.slice(lastSlash + 1);
const today = new Date().toISOString();
let daysAway = diffInDays(
ISOStringToDate(today),
ISOStringToDate(redo.reminderDate)
);
const avatarBgColor = daysAway <= 0 ? red[700] : green[700];
const today = new Date();

var delta = (new Date(redo.reminderDate) - today) / 1000;
// calculate (and subtract) whole days
var days = Math.floor(delta / 86400);
delta -= days * 86400;
// calculate (and subtract) whole hours
var hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;
// calculate (and subtract) whole minutes
var minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;
// what's left is seconds
var seconds = delta % 60;

const avatarBgColor = (new Date(redo.reminderDate) - today) <= 0 ? red[700] : green[700];
return (
<ListItem
key={redo.id}
Expand All @@ -125,9 +118,9 @@ class Popup extends Component {
</Avatar>
</ListItemAvatar>
<ListItemText
primary={displayTitle}
primary={redo.id}
secondary={
daysAway <= 0 ? 'Time to re-do!' : `${daysAway} days away`
(new Date(redo.reminderDate) - today) <= 0 ? 'Time to re-do!' : `${days} days ${hours} hours`
}
/>
<ListItemSecondaryAction>
Expand Down

0 comments on commit 01022bb

Please sign in to comment.