2020-09-19 15:24:48 +00:00
|
|
|
import { AUTH_KEY, LANGUAGE } from "../constants";
|
2020-09-30 08:43:19 +00:00
|
|
|
import sjcl from "sjcl";
|
2020-10-13 12:51:14 +00:00
|
|
|
import { isMobile } from "react-device-detect";
|
2020-01-21 05:56:04 +00:00
|
|
|
|
2019-11-19 12:35:56 +00:00
|
|
|
export const toUrlParams = (obj, skipNull) => {
|
|
|
|
let str = "";
|
|
|
|
for (var key in obj) {
|
|
|
|
if (skipNull && !obj[key]) continue;
|
|
|
|
|
|
|
|
if (str !== "") {
|
|
|
|
str += "&";
|
|
|
|
}
|
|
|
|
|
|
|
|
str += key + "=" + encodeURIComponent(obj[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
2020-09-19 15:24:48 +00:00
|
|
|
};
|
2019-11-19 12:35:56 +00:00
|
|
|
|
|
|
|
export function getObjectByLocation(location) {
|
|
|
|
if (!location.search || !location.search.length) return null;
|
|
|
|
|
|
|
|
const searchUrl = location.search.substring(1);
|
|
|
|
const object = JSON.parse(
|
|
|
|
'{"' +
|
|
|
|
decodeURIComponent(searchUrl)
|
|
|
|
.replace(/"/g, '\\"')
|
|
|
|
.replace(/&/g, '","')
|
|
|
|
.replace(/=/g, '":"') +
|
|
|
|
'"}'
|
|
|
|
);
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
2020-01-21 05:56:04 +00:00
|
|
|
|
2020-10-13 12:51:14 +00:00
|
|
|
export function changeLanguage(
|
|
|
|
i18n,
|
|
|
|
currentLng = localStorage.getItem(LANGUAGE)
|
|
|
|
) {
|
2020-09-19 15:24:48 +00:00
|
|
|
return currentLng
|
|
|
|
? i18n.language !== currentLng
|
|
|
|
? i18n.changeLanguage(currentLng)
|
|
|
|
: Promise.resolve((...args) => i18n.t(...args))
|
|
|
|
: i18n.changeLanguage("en");
|
2020-01-21 05:56:04 +00:00
|
|
|
}
|
2020-09-18 08:12:09 +00:00
|
|
|
|
2020-09-19 15:24:48 +00:00
|
|
|
export function redirectToDefaultPage() {
|
|
|
|
if (
|
2020-09-25 16:14:50 +00:00
|
|
|
(window.location.pathname === "/" ||
|
|
|
|
window.location.pathname === "" ||
|
|
|
|
window.location.pathname === "/login") &&
|
2020-09-19 15:24:48 +00:00
|
|
|
localStorage.getItem(AUTH_KEY) !== null
|
|
|
|
) {
|
|
|
|
setTimeout(() => window.location.replace("/products/files"), 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-09-23 15:42:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 21:44:49 +00:00
|
|
|
export function createPasswordHash(password, hashSettings) {
|
2020-10-01 16:04:10 +00:00
|
|
|
if (
|
|
|
|
!password ||
|
|
|
|
!hashSettings ||
|
|
|
|
typeof password !== "string" ||
|
|
|
|
typeof hashSettings !== "object" ||
|
|
|
|
!hashSettings.hasOwnProperty("salt") ||
|
|
|
|
!hashSettings.hasOwnProperty("size") ||
|
|
|
|
!hashSettings.hasOwnProperty("iterations") ||
|
|
|
|
typeof hashSettings.size !== "number" ||
|
|
|
|
typeof hashSettings.iterations !== "number" ||
|
|
|
|
typeof hashSettings.salt !== "string"
|
|
|
|
)
|
|
|
|
throw new Error("Invalid params.");
|
|
|
|
|
2020-09-30 21:44:49 +00:00
|
|
|
const { size, iterations, salt } = hashSettings;
|
2020-09-30 08:43:19 +00:00
|
|
|
|
|
|
|
let bits = sjcl.misc.pbkdf2(password, salt, iterations);
|
|
|
|
bits = bits.slice(0, size / 32);
|
|
|
|
const hash = sjcl.codec.hex.fromBits(bits);
|
|
|
|
|
|
|
|
return hash;
|
2020-01-21 05:56:04 +00:00
|
|
|
}
|
2020-10-05 12:37:09 +00:00
|
|
|
|
2020-09-23 15:42:47 +00:00
|
|
|
export function removeTempContent() {
|
|
|
|
const tempElm = document.getElementById("temp-content");
|
|
|
|
if (tempElm) {
|
|
|
|
tempElm.outerHTML = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hideLoader() {
|
2020-10-13 12:51:14 +00:00
|
|
|
if (isMobile) return;
|
|
|
|
|
|
|
|
if (window.loadingTimeout) {
|
|
|
|
clearTimeout(window.loadingTimeout);
|
|
|
|
window.loadingTimeout = null;
|
2020-09-23 15:42:47 +00:00
|
|
|
}
|
2020-10-13 12:51:14 +00:00
|
|
|
|
2020-10-13 13:20:49 +00:00
|
|
|
document.body.classList.remove("loading");
|
2020-09-23 15:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function showLoader() {
|
2020-10-13 12:51:14 +00:00
|
|
|
if (isMobile) return;
|
|
|
|
|
|
|
|
window.loadingTimeout = setTimeout(() => {
|
2020-10-13 13:20:49 +00:00
|
|
|
document.body.classList.add("loading");
|
2020-10-13 12:51:14 +00:00
|
|
|
}, 1000);
|
2020-09-23 15:42:47 +00:00
|
|
|
}
|
2020-11-17 03:29:24 +00:00
|
|
|
|
|
|
|
export { withLayoutSize } from "./withLayoutSize";
|