Commit 893416e4 authored by Jason Salyers's avatar Jason Salyers
Browse files

[JES] Switch to cookies from local storage for endpoint options

parent facf91f6
......@@ -12,6 +12,7 @@ import { serverApiRecordEvent } from 'app/utils/ServerApiClient';
import * as hive from '@hiveio/hive-js';
import { determineViewMode } from 'app/utils/Links';
import frontendLogger from 'app/utils/FrontendLogger';
import Cookies from 'universal-cookie';
window.addEventListener('error', frontendLogger);
......@@ -32,11 +33,13 @@ function runApp(initial_state) {
console.log('Initial state', initial_state);
const config = initial_state.offchain.config;
let cookies = new Cookies();
const alternativeApiEndpoints = config.alternative_api_endpoints;
const cookie_endpoint = cookies.get('user_preferred_api_endpoint');
const currentApiEndpoint =
localStorage.getItem('user_preferred_api_endpoint') === null
cookie_endpoint === null || cookie_endpoint === undefined
? config.hived_connection_client
: localStorage.getItem('user_preferred_api_endpoint');
: cookie_endpoint;
hive.api.setOptions({
url: currentApiEndpoint,
......@@ -44,7 +47,7 @@ function runApp(initial_state) {
useAppbaseApi: !!config.hived_use_appbase,
alternative_api_endpoints: alternativeApiEndpoints,
failover_threshold: config.failover_threshold,
rebranded_api: true
rebranded_api: true,
});
hive.config.set('address_prefix', config.address_prefix);
hive.config.set('rebranded_api', true);
......@@ -75,9 +78,7 @@ function runApp(initial_state) {
};
}
const location = `${window.location.pathname}${window.location.search}${
window.location.hash
}`;
const location = `${window.location.pathname}${window.location.search}${window.location.hash}`;
try {
clientRender(initial_state);
......@@ -90,7 +91,7 @@ function runApp(initial_state) {
if (!window.Intl) {
require.ensure(
['intl/dist/Intl'],
require => {
(require) => {
window.IntlPolyfill = window.Intl = require('intl/dist/Intl');
require('intl/locale-data/jsonp/en-US.js');
require('intl/locale-data/jsonp/es.js');
......
......@@ -4,6 +4,7 @@ import tt from 'counterpart';
import * as appActions from 'app/redux/AppReducer';
import o2j from 'shared/clash/object2json';
import * as hive from '@hiveio/hive-js';
import Cookies from 'universal-cookie';
class Settings extends React.Component {
handleLanguageChange = (event) => {
......@@ -15,16 +16,11 @@ class Settings extends React.Component {
getPreferredApiEndpoint = () => {
let preferred_api_endpoint = $STM_Config.hived_connection_client;
if (
typeof window !== 'undefined' &&
localStorage.getItem('user_preferred_api_endpoint')
) {
preferred_api_endpoint = localStorage.getItem(
'user_preferred_api_endpoint'
);
}
return preferred_api_endpoint;
let cookies = new Cookies();
let cookie_endpoint = cookies.get('user_preferred_api_endpoint');
return cookie_endpoint === null || cookie_endpoint === undefined
? 'https://api.hive.blog'
: cookie_endpoint;
};
generateAPIEndpointOptions = () => {
......@@ -53,13 +49,9 @@ class Settings extends React.Component {
};
handlePreferredAPIEndpointChange = (event) => {
if (typeof window !== 'undefined') {
localStorage.setItem(
'user_preferred_api_endpoint',
event.target.value
);
hive.api.setOptions({ url: event.target.value });
}
let cookies = new Cookies();
cookies.set('user_preferred_api_endpoint', event.target.value);
hive.api.setOptions({ url: event.target.value });
};
render() {
......
......@@ -182,6 +182,11 @@
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==
"@types/cookie@^0.3.3":
version "0.3.3"
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.3.3.tgz#85bc74ba782fb7aa3a514d11767832b0e3bc6803"
integrity sha512-LKVP3cgXBT9RYj+t+9FDKwS5tdI+rPBXaNSkma7hvqy35lc7mAokC2zsqWJH0LaqIt3B962nuYI77hsJoT1gow==
"@types/node@*":
version "12.6.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.6.8.tgz#e469b4bf9d1c9832aee4907ba8a051494357c12c"
......@@ -2605,6 +2610,11 @@ cookie@0.4.0:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
integrity sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==
cookie@^0.4.0:
version "0.4.1"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.1.tgz#afd713fe26ebd21ba95ceb61f9a8116e50a537d1"
integrity sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==
cookiejar@2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/cookiejar/-/cookiejar-2.0.6.tgz#0abf356ad00d1c5a219d88d44518046dd026acfe"
......@@ -10533,6 +10543,14 @@ uniq@^1.0.1:
resolved "https://registry.yarnpkg.com/uniq/-/uniq-1.0.1.tgz#b31c5ae8254844a3a8281541ce2b04b865a734ff"
integrity sha1-sxxa6CVIRKOoKBVBzisEuGWnNP8=
universal-cookie@^4.0.4:
version "4.0.4"
resolved "https://registry.yarnpkg.com/universal-cookie/-/universal-cookie-4.0.4.tgz#06e8b3625bf9af049569ef97109b4bb226ad798d"
integrity sha512-lbRVHoOMtItjWbM7TwDLdl8wug7izB0tq3/YVKhT/ahB4VDvWMyvnADfnJI8y6fSvsjh51Ix7lTGC6Tn4rMPhw==
dependencies:
"@types/cookie" "^0.3.3"
cookie "^0.4.0"
universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment