Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Narratives bug #865

Merged
merged 7 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/actions/recomputeReduxState.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const modifyStateViaURLQuery = (state, query) => {
const restoreQueryableStateToDefaults = (state) => {
for (const key of Object.keys(state.defaults)) {
switch (typeof state.defaults[key]) {
case "boolean": // fallthrough
case "string": {
state[key] = state.defaults[key];
break;
Expand All @@ -115,7 +116,7 @@ const restoreQueryableStateToDefaults = (state) => {
break;
}
default: {
console.error("unknown typeof for default state of ", key);
console.error("unknown typeof for default state of ", key, state.defaults[key]);
}
}
}
Expand Down Expand Up @@ -612,12 +613,27 @@ export const createStateFromQueryOrJSONs = ({
controls = restoreQueryableStateToDefaults(controls);
}


/* For the creation of state, we want to parse out URL query parameters
(e.g. ?c=country means we want to color-by country) and modify the state
accordingly. For narratives, we _don't_ display these in the URL, instead
only displaying the page number (e.g. ?n=3), but we can look up what (hidden)
URL query this page defines via this information */
if (narrativeBlocks) {
narrative = narrativeBlocks;
const n = parseInt(query.n, 10) || 0;
let n = parseInt(query.n, 10) || 0;
/* If the query has defined a block which doesn't exist then default to n=0 */
if (n >= narrative.length) {
console.warn(`Attempted to go to narrative page ${n} which doesn't exist`);
n=0;
}
controls = modifyStateViaURLQuery(controls, queryString.parse(narrative[n].query));
query = {n}; // eslint-disable-line
console.log("redux state changed to relfect n of", n);
query = n===0 ? {} : {n}; // eslint-disable-line
/* If the narrative block in view defines a `mainDisplayMarkdown` section, we
update `controls.panelsToDisplay` so this is displayed */
if (narrative[n].mainDisplayMarkdown) {
controls.panelsToDisplay = ["EXPERIMENTAL_MainDisplayMarkdown"];
}
} else {
controls = modifyStateViaURLQuery(controls, query);
}
Expand Down
5 changes: 3 additions & 2 deletions src/components/narrative/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable react/no-danger */
/* eslint-disable react/no-array-index-key */
import React from "react";
import { connect } from "react-redux";
import queryString from "query-string";
Expand Down Expand Up @@ -114,7 +115,7 @@ class Narrative extends React.Component {
const d = (!this.state.showingEndOfNarrativePage) && this.props.currentInFocusBlockIdx === i ?
"14px" : "6px";
return (<div
key={b.__html.slice(0, 30)}
key={i}
style={{width: d, height: d, background: "#74a9cf", borderRadius: "50%", cursor: "pointer"}}
onClick={() => this.reactPageScroller.goToPage(i)}
/>);
Expand All @@ -126,7 +127,7 @@ class Narrative extends React.Component {
const ret = this.props.blocks.map((b, i) => (
<div
id={`NarrativeBlock_${i}`}
key={b.__html.slice(0, 50)}
key={i}
style={{
padding: "10px 20px",
height: "inherit",
Expand Down
1 change: 1 addition & 0 deletions src/middleware/changeURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const changeURLMiddleware = (store) => (next) => (action) => {
case types.URL_QUERY_CHANGE_WITH_COMPUTED_STATE: // fallthrough
case types.CHANGE_URL_QUERY_BUT_NOT_REDUX_STATE:
query = action.query;
if (query.n === 0) delete query.n;
if (query.tt) delete query.tt;
break;
case types.CHANGE_ZOOM:
Expand Down
3 changes: 1 addition & 2 deletions src/reducers/narrative.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import queryString from "query-string";
import * as types from "../actions/types";

const explanationParagraph=`
Expand Down Expand Up @@ -32,7 +31,7 @@ const narrative = (state = {
blocks,
title: blocks[0].__html.match(/>(.+?)</)[1],
pathname: window.location.pathname,
blockIdx: parseInt(queryString.parse(window.location.search).n, 10) || 0
blockIdx: action.query.n || 0
};
}
return state;
Expand Down