Skip to content

Commit

Permalink
bright/darkmode cookie fix (#1564)
Browse files Browse the repository at this point in the history
* bright/darkmode cookie fix

* Fix cookies

* Use observer pattern
  • Loading branch information
ovbm authored and corradio committed Sep 6, 2018
1 parent 1d1ba1e commit 20cb288
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
33 changes: 13 additions & 20 deletions web/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,24 +727,15 @@ window.retryFetch = () => {
// cause events to be emitted

// BrightMode
const electricityMapHeader = d3.select('#header-content');
const tmrowWatermark = d3.select('#watermark');

function toggleBright() {
dispatchApplication('brightModeEnabled', !getState().application.brightModeEnabled);
electricityMapHeader.classed('brightmode', !electricityMapHeader.classed('brightmode'));
tmrowWatermark.classed('brightmode', !tmrowWatermark.classed('brightmode'));
}

d3.select('.brightmode-button').on('click', toggleBright);

const brightModeButtonTooltip = d3.select('#brightmode-layer-button-tooltip');

if (!getState().application.isMobile) {
// Mouseovers will trigger on click on mobile and is therefore only set on desktop
d3.select('.brightmode-button').on('mouseover', () => {
brightModeButtonTooltip.classed('hidden', false);

});
d3.select('.brightmode-button').on('mouseout', () => {
brightModeButtonTooltip.classed('hidden', true);
Expand Down Expand Up @@ -791,7 +782,7 @@ if (!getState().application.isMobile) {
}


// Legend
// Legend
function toggleLegend() {
dispatchApplication('legendVisible', !getState().application.legendVisible);
}
Expand Down Expand Up @@ -952,7 +943,6 @@ function renderCountryTable(state) {


function renderOpenTooltips(state) {

const zoneData = getCurrentZoneData(state);
const tooltipMode = state.application.tooltipDisplayMode;
if (tooltipMode) {
Expand All @@ -962,9 +952,9 @@ function renderOpenTooltips(state) {
const ttp = isExchange ?
countryTableExchangeTooltip : countryTableProductionTooltip;
fun(ttp,
tooltipMode, zoneData, tableDisplayEmissions,
co2color, co2Colorbars,
);
tooltipMode, zoneData, tableDisplayEmissions,
co2color, co2Colorbars,
);
}

if (countryTooltip.isVisible) {
Expand All @@ -977,7 +967,6 @@ function renderOpenTooltips(state) {
tooltip.select('.currency').html(getSymbolFromCurrency((zoneData.price || {}).currency) || '?');
priceTooltip.show();
}

}


Expand Down Expand Up @@ -1141,9 +1130,8 @@ function renderLeftPanelCollapseButton(state) {
zoneMap.map.resize();
}
}
function routeToPage(pageName, state) {


function routeToPage(pageName, state) {
d3.selectAll('.left-panel .left-panel-zone-list').classed('small-screen-hidden', pageName !== 'highscore');

d3.selectAll('.left-panel .left-panel-zone-list').classed('large-screen-hidden', pageName === 'country' || pageName === 'faq');
Expand All @@ -1154,7 +1142,6 @@ function routeToPage(pageName, state) {

d3.selectAll('.left-panel .left-panel-zone-details').classed('all-screens-hidden', pageName !== 'country');


// Hide map on small screens
// It's important we show the map before rendering it to make sure
// sizes are set properly
Expand Down Expand Up @@ -1310,7 +1297,7 @@ observe(state => state.application.colorBlindModeEnabled, (colorBlindModeEnabled
// Observe for bright mode changes
observe(state => state.application.brightModeEnabled, (brightModeEnabled) => {
d3.selectAll('.brightmode-button').classed('active', brightModeEnabled);
Cookies.set('brightdModeEnabled', brightModeEnabled);
Cookies.set('brightModeEnabled', brightModeEnabled);
// update Theme
if (getState().application.brightModeEnabled) {
theme = themes.bright;
Expand Down Expand Up @@ -1385,11 +1372,17 @@ observe(state => state.application.legendVisible, (legendVisible) => {
// Observe for left panel collapse
observe(state => state.application.isLeftPanelCollapsed, (_, state) =>
renderLeftPanelCollapseButton(state));

// Observe for search query change
observe(state => state.application.searchQuery, (searchQuery, state) => {
zoneList.filterZonesByQuery(searchQuery);
});
// Observe for brightmode change
observe(state => state.application.brightModeEnabled, (brightModeEnabled, _) => {
const electricityMapHeader = d3.select('#header-content');
const tmrowWatermark = d3.select('#watermark');
electricityMapHeader.classed('brightmode', brightModeEnabled);
tmrowWatermark.classed('brightmode', brightModeEnabled);
});


// ** START
Expand Down
18 changes: 13 additions & 5 deletions web/src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ const dataReducer = require('./dataReducer');
const isLocalhost = window.location.href.indexOf('electricitymap') !== -1 ||
window.location.href.indexOf('192.') !== -1;

const cookieGetBool = (key, defaultValue) => {
const val = Cookies.get(key);
if (key == null) {
return defaultValue;
}
return val === 'true';
};

const initialApplicationState = {
// Here we will store non-data specific state (to be sent in analytics and crash reporting)
bundleHash: window.bundleHash,
callerLocation: null,
clientType: window.isCordova ? 'mobileapp' : 'web',
colorBlindModeEnabled: Cookies.get('colorBlindModeEnabled') === 'true' || false,
brightModeEnabled: Cookies.get('brightModeEnabled') === 'true' || true,
colorBlindModeEnabled: cookieGetBool('colorBlindModeEnabled', false),
brightModeEnabled: cookieGetBool('brightModeEnabled', true),
customDate: null,
isCordova: window.isCordova,
isEmbedded: window.top !== window.self,
Expand All @@ -23,15 +31,15 @@ const initialApplicationState = {
isLocalhost,
legendVisible: false,
locale: window.locale,
onboardingSeen: Cookies.get('onboardingSeen') === 'true' || false,
onboardingSeen: cookieGetBool('onboardingSeen', false),
tooltipDisplayMode: null,
searchQuery: null,
selectedZoneName: null,
selectedZoneTimeIndex: null,
previousSelectedZoneTimeIndex: null,
solarEnabled: Cookies.get('solarEnabled') === 'true' || false,
solarEnabled: cookieGetBool('solarEnabled', false),
useRemoteEndpoint: document.domain === '' || isLocalhost,
windEnabled: Cookies.get('windEnabled') === 'true' || false,
windEnabled: cookieGetBool('windEnabled', false),

// TODO(olc): refactor this state
showPageState: 'map',
Expand Down

0 comments on commit 20cb288

Please sign in to comment.