-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
239 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {Dispatch} from 'redux'; | ||
|
||
import fetchLayerApi from '../api/fetch-layer'; | ||
|
||
import {Layer} from '../types/layer'; | ||
|
||
export const FETCH_LAYER_SUCCESS = 'FETCH_LAYER_SUCCESS'; | ||
export const FETCH_LAYER_ERROR = 'FETCH_LAYER_ERROR'; | ||
|
||
export interface FetchLayerSuccessAction { | ||
type: typeof FETCH_LAYER_SUCCESS; | ||
id: string; | ||
layer: Layer; | ||
} | ||
|
||
interface FetchLayerErrorAction { | ||
type: typeof FETCH_LAYER_ERROR; | ||
message: string; | ||
} | ||
|
||
export type FetchLayerActions = FetchLayerSuccessAction | FetchLayerErrorAction; | ||
|
||
function fetchLayerSuccessAction(id: string, layer: Layer) { | ||
return { | ||
type: FETCH_LAYER_SUCCESS, | ||
id, | ||
layer | ||
}; | ||
} | ||
|
||
function fetchLayerErrorAction(message: string) { | ||
return { | ||
type: FETCH_LAYER_ERROR, | ||
message | ||
}; | ||
} | ||
|
||
const fetchLayer = (id: string) => (dispatch: Dispatch) => | ||
fetchLayerApi(id) | ||
.then(layer => dispatch(fetchLayerSuccessAction(id, layer))) | ||
.catch(error => dispatch(fetchLayerErrorAction(error.message))); | ||
|
||
export default fetchLayer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import config from '../config/main'; | ||
import {replaceUrlPlaceholders} from '../libs/replace-url-placeholders'; | ||
|
||
export default function fetchLayer(id: string) { | ||
const url = replaceUrlPlaceholders(config.api.layer, {id}); | ||
return fetch(url).then(res => res.json()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import config from '../config/main'; | ||
import {replaceUrlPlaceholders} from '../libs/replace-url-placeholders'; | ||
|
||
import {Language} from '../types/language'; | ||
|
||
export default function fetchLayers(language: Language) { | ||
const url = `${config.api.layers}-${language.toLowerCase()}.json`; | ||
const url = replaceUrlPlaceholders(config.api.layers, { | ||
lang: language.toLowerCase() | ||
}); | ||
|
||
return fetch(url).then(res => res.json()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {FunctionComponent, useEffect} from 'react'; | ||
import {useSelector, useDispatch} from 'react-redux'; | ||
|
||
import fetchLayers from '../../actions/fetch-layers'; | ||
import fetchLayerAction from '../../actions/fetch-layer'; | ||
import {selectedLayersSelector} from '../../reducers/layers/selected'; | ||
import {detailedLayersSelector} from '../../reducers/layers/details'; | ||
|
||
/** | ||
* Handles loading of layer list and layer details data | ||
*/ | ||
const LayerLoader: FunctionComponent = () => { | ||
const dispatch = useDispatch(); | ||
const selectedLayers = useSelector(selectedLayersSelector); | ||
const detailedLayers = useSelector(detailedLayersSelector); | ||
const {main, compare} = selectedLayers; | ||
|
||
// load layer list on mount | ||
useEffect(() => { | ||
dispatch(fetchLayers()); | ||
}, [dispatch]); | ||
|
||
// fetch layer if it is selected and not already downloaded | ||
useEffect(() => { | ||
if (main && !detailedLayers[main]) { | ||
dispatch(fetchLayerAction(main)); | ||
} | ||
|
||
if (compare && !detailedLayers[compare]) { | ||
dispatch(fetchLayerAction(compare)); | ||
} | ||
}, [dispatch, selectedLayers, detailedLayers]); | ||
|
||
return null; | ||
}; | ||
|
||
export default LayerLoader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
type ValueMap = { | ||
[key: string]: string; | ||
}; | ||
|
||
const MATCH_PLACEHOLDERS = /{\w+}/g; | ||
|
||
/** | ||
* Replaces all url occurences of '{key}' with the provided values | ||
*/ | ||
export function replaceUrlPlaceholders(url: string, values: ValueMap): string { | ||
const matches = url.match(MATCH_PLACEHOLDERS); | ||
|
||
if (!matches) { | ||
return url; | ||
} | ||
|
||
matches.forEach(match => { | ||
// remove {} to get key | ||
const key = match.replace(/{|}/g, ''); | ||
// get value for this key | ||
const value = values[key]; | ||
|
||
// do nothing if no value for this key is provided | ||
if (!value) { | ||
return; | ||
} | ||
|
||
// replace all placeholders for this key in the url with the value | ||
const regex = new RegExp(`{${key}}`, 'g'); | ||
url = url.replace(regex, value); | ||
}); | ||
|
||
return url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { | ||
FETCH_LAYER_SUCCESS, | ||
FetchLayerSuccessAction | ||
} from '../../actions/fetch-layer'; | ||
|
||
import {State} from '../index'; | ||
import {Layer} from '../../types/layer'; | ||
|
||
type DetailsById = {[id: string]: Layer}; | ||
|
||
function detailsReducer( | ||
state: DetailsById = {}, | ||
action: FetchLayerSuccessAction | ||
): DetailsById { | ||
switch (action.type) { | ||
case FETCH_LAYER_SUCCESS: | ||
return { | ||
...state, | ||
[action.id]: action.layer | ||
}; | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
export function detailedLayersSelector(state: State): DetailsById { | ||
return state.layers.details; | ||
} | ||
|
||
export default detailsReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {combineReducers} from 'redux'; | ||
|
||
import listReducer from './list'; | ||
import detailsReducer from './details'; | ||
import selectedReducer from './selected'; | ||
|
||
import {State} from '../index'; | ||
|
||
const layersReducer = combineReducers({ | ||
list: listReducer, | ||
details: detailsReducer, | ||
selected: selectedReducer | ||
}); | ||
|
||
export default layersReducer; | ||
|
||
export type LayersState = ReturnType<typeof layersReducer>; | ||
|
||
export const layersStateSelector = (state: State): LayersState => state.layers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { | ||
FETCH_LAYERS_SUCCESS, | ||
FetchLayersActions | ||
} from '../../actions/fetch-layers'; | ||
|
||
import {State} from '../index'; | ||
import {LayerList} from '../../types/layer-list'; | ||
|
||
const initialState: LayerList = []; | ||
|
||
function layersReducer( | ||
layersState: LayerList = initialState, | ||
action: FetchLayersActions | ||
): LayerList { | ||
switch (action.type) { | ||
case FETCH_LAYERS_SUCCESS: | ||
return action.layers; | ||
default: | ||
return layersState; | ||
} | ||
} | ||
|
||
export function layersSelector(state: State): LayerList { | ||
return state.layers.list; | ||
} | ||
|
||
export default layersReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
interface LayerListItem { | ||
id: string; | ||
name: string; | ||
description: string; | ||
subLayers: LayerListItem[]; | ||
} | ||
|
||
export type LayerList = LayerListItem[]; |
Oops, something went wrong.