Skip to content

Commit fa877cf

Browse files
committed
working on new sources page
1 parent 5b9356e commit fa877cf

24 files changed

+1604
-15
lines changed

pages/maps/sources/+Page.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Provider } from "react-redux";
2+
import { createStore } from "redux";
3+
import reducers from "./app-state";
4+
import App from "./components/app";
5+
import h from "@macrostrat/hyper";
6+
7+
// We should probably make this a little less global
8+
// import "~/styles/global.styl";
9+
// import "./searchbar.styl";
10+
// import "./ui-components.styl";
11+
12+
// Create the data store
13+
let store = createStore(reducers);
14+
15+
// Render the application
16+
export function Page() {
17+
return h(Provider, { store }, [h(App)]);
18+
}

pages/maps/sources/+config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default {
2+
route: "/maps/sources*",
3+
meta: {
4+
Page: {
5+
/* Sift must be rendered as a single-page app, because that is its design.
6+
It must only use server-side links to other pages,
7+
because of its reliance on global styles that could leak to other pages with client routing
8+
*/
9+
env: {
10+
client: true,
11+
server: false,
12+
},
13+
},
14+
},
15+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { BurwellSourceActions as Action, BurwellState } from "./types";
2+
import axios from "axios";
3+
import { apiV2Prefix } from "@macrostrat-web/settings";
4+
5+
async function actionRunner(action: Action): Promise<Action | void> {
6+
switch (action.type) {
7+
case "fetch-data":
8+
const data = await fetchBurwellMapData();
9+
return { type: "recieve-data", maps: data };
10+
default:
11+
return action;
12+
}
13+
}
14+
15+
async function fetchBurwellMapData() {
16+
const url = `${apiV2Prefix}/defs/sources`;
17+
18+
const res = await axios.get(url, {
19+
responseType: "json",
20+
params: { all: true, format: "geojson_bare" },
21+
});
22+
return res.data.features;
23+
}
24+
25+
export default actionRunner;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useDispatch, useSelector } from "react-redux";
2+
import { BurwellSourceActions as Action, BurwellState } from "./types";
3+
import actionRunner from "./action-runner";
4+
5+
function useActionDispatch() {
6+
return useDispatch<React.Dispatch<Action>>();
7+
}
8+
9+
function useBurwellActions(): (action: Action) => Promise<void> {
10+
const dispatch = useActionDispatch();
11+
return async (action) => {
12+
const newAction = await actionRunner(action);
13+
if (newAction === undefined) return;
14+
dispatch(newAction as Action);
15+
};
16+
}
17+
function useBurwellState<T>(selectorFn: (state: BurwellState) => T): T {
18+
return useSelector<BurwellState>(selectorFn) as T;
19+
}
20+
21+
export { useBurwellActions, useBurwellState };
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import reducers from "./reducer";
2+
export default reducers;
3+
export * from "./hooks";
4+
export * from "./utils";
5+
export * from "./types";
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { BurwellSourceActions, BurwellState } from "./types";
2+
3+
const burwellDefaultState: BurwellState = {
4+
isFetching: false,
5+
msg: "",
6+
clicks: 0,
7+
maps: [],
8+
selectedScale: "all",
9+
selectedFeatures: [],
10+
activeFeature: {},
11+
menuOpen: false,
12+
flyTo: true,
13+
};
14+
15+
const reducer = (
16+
state: BurwellState = burwellDefaultState,
17+
action: BurwellSourceActions
18+
) => {
19+
switch (action.type) {
20+
case "request-data":
21+
return Object.assign({}, state, {
22+
isFetching: true,
23+
});
24+
case "recieve-data":
25+
// map ids to features to use mapbox effects appwide
26+
const maps = action.maps.map((d, i) => {
27+
d.id = i;
28+
return d;
29+
});
30+
return Object.assign({}, state, {
31+
isFetching: false,
32+
maps: maps,
33+
});
34+
case "select-scale":
35+
return Object.assign({}, state, {
36+
selectedScale: action.selectedScale,
37+
});
38+
case "select-features":
39+
return Object.assign({}, state, {
40+
selectedFeatures: action.selectedFeatures,
41+
});
42+
case "activate-feature":
43+
return Object.assign({}, state, {
44+
activeFeature: action.activeFeature,
45+
});
46+
case "toggle-menu":
47+
return Object.assign({}, state, {
48+
menuOpen: action.menuOpen,
49+
});
50+
case "toggle-fly-option":
51+
return Object.assign({}, state, {
52+
flyTo: action.flyTo,
53+
});
54+
default:
55+
return state;
56+
}
57+
};
58+
59+
export default reducer;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//////////////// Action Types //////////////////
2+
type FETCH_DATA = { type: "fetch-data" };
3+
type RECIEVE_DATA = { type: "recieve-data"; maps: object[] };
4+
type REQUEST_DATA = { type: "request-data" };
5+
type SELECT_SCALE = { type: "select-scale"; selectedScale: any };
6+
type SELECT_FEATURES = { type: "select-features"; selectedFeatures: any };
7+
type ACTIVATE_FEATURE = { type: "activate-feature"; activeFeature: any };
8+
type TOGGLE_MENU = { type: "toggle-menu"; menuOpen: boolean };
9+
type TOGGLE_FLY_OPTION = { type: "toggle-fly-option"; flyTo: boolean };
10+
11+
export type BurwellSourceActions =
12+
| FETCH_DATA
13+
| RECIEVE_DATA
14+
| REQUEST_DATA
15+
| SELECT_SCALE
16+
| SELECT_FEATURES
17+
| ACTIVATE_FEATURE
18+
| TOGGLE_MENU
19+
| TOGGLE_FLY_OPTION;
20+
21+
export interface BurwellState {
22+
isFetching: boolean;
23+
msg: string;
24+
clicks: number;
25+
maps: [];
26+
selectedScale: string;
27+
selectedFeatures: [];
28+
activeFeature: {};
29+
menuOpen: boolean;
30+
flyTo: boolean;
31+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
export const addCommas = (x) => {
2+
x = parseInt(x);
3+
var parts = x.toString().split(".");
4+
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
5+
return parts.join(".");
6+
};
7+
8+
export const getVisibleScale = (maps, filter) => {
9+
switch (filter) {
10+
case "all":
11+
return maps;
12+
case "large":
13+
return maps.filter((d) => {
14+
if (d.properties.scale === "large") return d;
15+
});
16+
case "medium":
17+
return maps.filter((d) => {
18+
if (d.properties.scale === "medium") return d;
19+
});
20+
case "small":
21+
return maps.filter((d) => {
22+
if (d.properties.scale === "small") return d;
23+
});
24+
case "tiny":
25+
return maps.filter((d) => {
26+
if (d.properties.scale === "tiny") return d;
27+
});
28+
default:
29+
return [];
30+
}
31+
};
32+
33+
export function getCenter(coordinates) {
34+
const lat = (coordinates[0][0][1] + coordinates[0][2][1]) / 2;
35+
const long = (coordinates[0][0][0] + coordinates[0][2][0]) / 2;
36+
return [long, lat];
37+
}
38+
39+
export const zoomMap = {
40+
tiny: 1,
41+
small: 3,
42+
medium: 4,
43+
large: 7,
44+
};
45+
46+
export const offsetMap = {
47+
tiny: 10,
48+
small: 10,
49+
medium: 4,
50+
large: 0.05,
51+
};
52+
53+
export function flyToData(feature) {
54+
const [long, lat] = getCenter(feature.geometry.coordinates);
55+
const zoom = zoomMap[feature.properties.scale];
56+
let offset = offsetMap[feature.properties.scale];
57+
58+
return { center: [long + offset, lat], zoom };
59+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import IndexMapContainer from "../components/map";
2+
import h from "@macrostrat/hyper";
3+
import { InfoDrawer } from "../components/info-drawer";
4+
5+
const Content = () => {
6+
return h("div.full-height", [h(IndexMapContainer), h(InfoDrawer)]);
7+
};
8+
9+
export default Content;

0 commit comments

Comments
 (0)