import * as React from 'react'; import { Store } from 'redux'; import * as Types from '../Types'; import * as Rx from 'rxjs/Rx'; declare var fetch: any; interface AboutPageState { data: any; } function aboutReducer(state: AboutPageState = {data: {}}, action: any) { switch (action.type) { case 'STUFF_FROM_WORLD_BANK': return { data: action.data }; default: return state; } } function aboutPageFactory(store: Store) { var fetching = false; return class AboutPage extends React.Component { render() { var data = store.getState().about.data; return (
{data['user-agent'] ? data['user-agent'] : 'Loading...'}
); } constructor() { super(); } componentWillMount() { if (fetching) { return; }; fetching = true; store.dispatch({type: 'FETCH_DATA'}); } }; } let AboutPage: Types.Page = { // redux reducer http://redux.js.org/docs/basics/Reducers.html reducer: aboutReducer, // function that returns the React component for the page pageFactory: aboutPageFactory, epic: function (action$: Rx.Observable) { return action$.filter(a => a.type === 'FETCH_DATA') .mergeMap(action => Rx.Observable.ajax.getJSON('https://httpbin.org/user-agent') .map(function (response: any) { return { type: 'STUFF_FROM_WORLD_BANK', data: response }; }) ); }, // the route to match to this page route: 'about' }; export default AboutPage;