Skip to content

Commit

Permalink
Merge pull request #6 from MrDanaT/v2
Browse files Browse the repository at this point in the history
Last cleanup for V2
  • Loading branch information
MrDanaT authored Feb 10, 2022
2 parents f01dd72 + 4aaa53c commit a010526
Showing 1 changed file with 90 additions and 50 deletions.
140 changes: 90 additions & 50 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,7 @@ export default {
};
},
mounted() {
if (localStorage.hotspotAddress) {
const local = JSON.parse(localStorage.hotspotAddress);
local.forEach((address) => {
this.inputHotspotAddress = address;
this.addAddress();
});
} else {
localStorage.hotspotAddress = JSON.stringify([]);
}
if (localStorage.ownerAddress) {
const local = JSON.parse(localStorage.ownerAddress);
local.forEach((address) => {
this.inputOwnerAddress = address;
this.addOwnersHotspots();
});
} else {
localStorage.ownerAddress = JSON.stringify([]);
}
this.loadDataFromLocalStorage();
},
computed: {
mappedHotspots() {
Expand All @@ -215,7 +198,7 @@ export default {
allDates() {
const dateArray = new Array();
let currentDate = this.getStartOfGivenDay(this.datePicked.toDate());
while (currentDate < this.currDay) {
while (currentDate <= this.currDay) {
dateArray.push(new Date(currentDate));
currentDate = currentDate.addDays(1);
}
Expand Down Expand Up @@ -294,7 +277,7 @@ export default {
let res = {
date: arr[0],
};
if (rest.length > 0) {
if (this.isPositive(rest.length)) {
res = {
...res,
...rest.reduce(
Expand Down Expand Up @@ -329,6 +312,26 @@ export default {
},
},
methods: {
loadDataFromLocalStorage() {
if (localStorage.hotspotAddress) {
const local = JSON.parse(localStorage.hotspotAddress);
local.forEach((address) => {
this.inputHotspotAddress = address;
this.addAddress();
});
} else {
this.clearLocalAddresses();
}
if (localStorage.ownerAddress) {
const local = JSON.parse(localStorage.ownerAddress);
local.forEach((address) => {
this.inputOwnerAddress = address;
this.addOwnersHotspots();
});
} else {
this.clearLocalOwnerAddresses();
}
},
convertToComma(txt) {
return txt.toString().replace(".", ",");
},
Expand Down Expand Up @@ -375,13 +378,7 @@ export default {
"error"
);
} else {
if (this.shouldCache) {
const local = JSON.parse(localStorage.hotspotAddress);
if (!local.includes(this.inputHotspotAddress)) {
local.push(this.inputHotspotAddress);
localStorage.hotspotAddress = JSON.stringify(local);
}
}
this.addLocalAddress();
this.processHotspot(this.inputHotspotAddress)
.then(() => {
this.inputHotspotAddress = "";
Expand Down Expand Up @@ -462,13 +459,7 @@ export default {
return results;
},
addOwnersHotspots() {
if (this.shouldCache) {
const local = JSON.parse(localStorage.ownerAddress);
if (!local.includes(this.inputOwnerAddress)) {
local.push(this.inputOwnerAddress);
localStorage.ownerAddress = JSON.stringify(local);
}
}
this.addLocalOwnerAddress();
let isAdded = 0;
let isIgnored = 0;
this.getOwnersHotspots()
Expand All @@ -484,26 +475,42 @@ export default {
});
})
.then(() => {
if (isAdded > 0) {
if (this.isPositive(isAdded)) {
this.openNotification(
"Succesful",
`${isAdded} hotspot${isAdded > 1 ? "s" : ""} ${
isAdded > 1 ? "have" : "has"
} been added.`,
`${isAdded} ${this.pluralize(
isAdded,
"hotspot"
)} ${this.hasPlural(isAdded)} been added.`,
"success"
);
}
if (isIgnored > 0) {
if (this.isPositive(isIgnored)) {
this.openNotification(
`Cannot add hotspot${isIgnored > 1 ? "s" : ""}`,
`${isIgnored} hotspot${isIgnored > 1 ? "s" : ""} ${
isIgnored > 1 ? "are" : "is"
} already in the list.`,
`Cannot add ${this.pluralize(isIgnored, "hotspot")}`,
`${isIgnored} ${this.pluralize(
isIgnored,
"hotspot"
)} ${this.isPlural(isIgnored)} already in the list.`,
"warning"
);
}
});
},
isPositive(number) {
return number > 0;
},
hasPlural(count) {
return count > 1 ? "have" : "has";
},
isPlural(count) {
return count > 1 ? "are" : "is";
},
pluralize(count, word) {
let result = word;
if (count > 1) result += "s";
return result;
},
getStartOfGivenDay(date) {
var res = new Date(date.getTime());
res.setUTCHours(0, 0, 0, 0);
Expand Down Expand Up @@ -550,16 +557,16 @@ export default {
if (res.status == 200) {
return res.data;
}
} catch (err) {
console.error(err);
} catch ($e) {
this.openNotification("Cannot get reward.", $e, "error");
}
},
removeAddress(address) {
const idx = this.findIndexOf(address);
if (idx === -1) {
this.openNotification(
"Cannot remove hotspot",
"Hotspot is not in the list.",
`${address || "/"} is not in the list.`,
"error"
);
} else {
Expand All @@ -577,16 +584,25 @@ export default {
(hs) => hs.owner !== this.inputOwnerAddress
);
const remainingCount = this.hotspotDict.length - rest.length;
if (remainingCount > 0) {
if (this.isPositive(remainingCount)) {
this.openNotification(
"Succesful",
`${remainingCount} hotspot${remainingCount > 1 ? "s" : ""} ${
remainingCount > 1 ? "have" : "has"
} been removed.`,
`${remainingCount} ${this.pluralize(
remainingCount,
"hotspot"
)} ${this.hasPlural(remainingCount)} been removed.`,
"success"
);
this.hotspotDict = rest;
this.inputOwnerAddress = "";
} else {
this.openNotification(
"Cannot remove owner's hotspot(s)",
`${
this.inputOwnerAddress || "/"
}'s hotspots are currently not in the list.`,
"error"
);
}
},
loadData() {
Expand All @@ -607,7 +623,31 @@ export default {
});
},
clearCache() {
this.clearLocalAddresses();
this.clearLocalOwnerAddresses();
},
addLocalAddress() {
if (this.shouldCache) {
const local = JSON.parse(localStorage.hotspotAddress);
if (!local.includes(this.inputHotspotAddress)) {
local.push(this.inputHotspotAddress);
localStorage.hotspotAddress = JSON.stringify(local);
}
}
},
clearLocalAddresses() {
localStorage.hotspotAddress = JSON.stringify([]);
},
addLocalOwnerAddress() {
if (this.shouldCache) {
const local = JSON.parse(localStorage.ownerAddress);
if (!local.includes(this.inputOwnerAddress)) {
local.push(this.inputOwnerAddress);
localStorage.ownerAddress = JSON.stringify(local);
}
}
},
clearLocalOwnerAddresses() {
localStorage.ownerAddress = JSON.stringify([]);
},
},
Expand Down

0 comments on commit a010526

Please sign in to comment.