Merge pull request #58 from Molorius/master

Added parameters first-time and previous-pattern.
master
Mike Bryant 5 years ago committed by GitHub
commit 10f9b3a755
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -65,9 +65,10 @@ const fillFields = function (prices, first_buy, previous_pattern) {
const initialize = function () { const initialize = function () {
try { try {
const prices = getPrices() const previous = getPrevious();
const first_buy = getFirstBuyState(); const first_buy = previous[0];
const previous_pattern = getPreviousPatternState(); const previous_pattern = previous[1];
const prices = previous[2];
if (prices === null) { if (prices === null) {
fillFields([], first_buy, previous_pattern) fillFields([], first_buy, previous_pattern)
} else { } else {
@ -101,14 +102,65 @@ const isEmpty = function (arr) {
return filtered.length == 0 return filtered.length == 0
} }
const getFirstBuyState = function () { const getFirstBuyStateFromQuery = function (param) {
try {
const params = new URLSearchParams(window.location.search.substr(1));
const firstbuy_str = params.get(param);
if (firstbuy_str == null) {
return null;
}
firstbuy = null;
if (firstbuy_str == "1" || firstbuy_str == "yes" || firstbuy_str == "true") {
firstbuy = true;
} else if (firstbuy_str == "0" || firstbuy_str == "no" || firstbuy_str == "false") {
firstbuy = false;
}
return firstbuy;
} catch (e) {
return null;
}
}
const getFirstBuyStateFromLocalstorage = function () {
return JSON.parse(localStorage.getItem('first_buy')) return JSON.parse(localStorage.getItem('first_buy'))
} }
const getPreviousPatternState = function () { const getPreviousPatternStateFromLocalstorage = function () {
return JSON.parse(localStorage.getItem('previous_pattern')) return JSON.parse(localStorage.getItem('previous_pattern'))
} }
const getPreviousPatternStateFromQuery = function (param) {
try {
const params = new URLSearchParams(window.location.search.substr(1));
const pattern_str = params.get(param);
if (pattern_str == null) {
return null;
}
if (pattern_str == "0" || pattern_str == "fluctuating") {
pattern = 0;
} else if (pattern_str == "1" || pattern_str == "large-spike") {
pattern = 1;
} else if (pattern_str == "2" || pattern_str == "decreasing") {
pattern = 2;
} else if (pattern_str == "3" || pattern_str == "small-spike") {
pattern = 3;
} else {
pattern = -1;
}
return pattern;
} catch (e) {
return null;
}
}
const getPricesFromLocalstorage = function () { const getPricesFromLocalstorage = function () {
try { try {
const sell_prices = JSON.parse(localStorage.getItem("sell_prices")); const sell_prices = JSON.parse(localStorage.getItem("sell_prices"));
@ -123,10 +175,10 @@ const getPricesFromLocalstorage = function () {
} }
}; };
const getPricesFromQuery = function () { const getPricesFromQuery = function (param) {
try { try {
const params = new URLSearchParams(window.location.search.substr(1)); const params = new URLSearchParams(window.location.search.substr(1));
const sell_prices = params.get("prices").split(".").map((x) => parseInt(x, 10)); const sell_prices = params.get(param).split(".").map((x) => parseInt(x, 10));
if (!Array.isArray(sell_prices)) { if (!Array.isArray(sell_prices)) {
return null; return null;
@ -141,15 +193,45 @@ const getPricesFromQuery = function () {
sell_prices.push(0); sell_prices.push(0);
} }
window.price_from_query = true;
return sell_prices; return sell_prices;
} catch (e) { } catch (e) {
return null; return null;
} }
}; };
const getPrices = function () { const getPreviousFromQuery = function () {
return getPricesFromQuery() || getPricesFromLocalstorage();
/* Check if valid prices are entered. Exit immediately if not. */
prices = getPricesFromQuery("prices");
if (prices == null) {
return null;
}
console.log("Using data from query.");
window.populated_from_query = true;
return [
getFirstBuyStateFromQuery("first"),
getPreviousPatternStateFromQuery("pattern"),
prices
];
};
const getPreviousFromLocalstorage = function () {
return [
getFirstBuyStateFromLocalstorage(),
getPreviousPatternStateFromLocalstorage(),
getPricesFromLocalstorage()
];
};
/**
* Gets previous values. First tries to parse parameters,
* if none of them match then it looks in local storage.
* @return {[first time, previous pattern, prices]}
*/
const getPrevious = function () {
return getPreviousFromQuery() || getPreviousFromLocalstorage();
}; };
const getSellPrices = function () { const getSellPrices = function () {
@ -193,7 +275,7 @@ const update = function () {
const prices = [buy_price, buy_price, ...sell_prices]; const prices = [buy_price, buy_price, ...sell_prices];
if (!window.price_from_query) { if (!window.populated_from_query) {
updateLocalStorage(prices, first_buy, previous_pattern); updateLocalStorage(prices, first_buy, previous_pattern);
} }

Loading…
Cancel
Save