Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mklemme/e027244d76cd898c132659dd054dceb1 to your computer and use it in GitHub Desktop.

Select an option

Save mklemme/e027244d76cd898c132659dd054dceb1 to your computer and use it in GitHub Desktop.

Revisions

  1. Marko Pavlovic revised this gist Apr 12, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions (1) firebaseMiddleware.js
    Original file line number Diff line number Diff line change
    @@ -26,8 +26,8 @@ export default function firebaseMiddleware(fireRef) {

    const firebasePromise = firebase(fireRef);
    firebasePromise.then(
    (result) => console.log('result', result),
    (error) => console.log('error', error)
    (result) => next({...rest, result, type: SUCCESS}),
    (error) => next({...rest, error, type: FAILURE})
    ).catch((error)=> {
    console.error('MIDDLEWARE ERROR:', error);
    next({...rest, error, type: FAILURE});
  2. Marko Pavlovic renamed this gist Apr 11, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Marko Pavlovic renamed this gist Apr 11, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. Marko Pavlovic renamed this gist Apr 11, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. Marko Pavlovic renamed this gist Apr 11, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. Marko Pavlovic revised this gist Apr 11, 2016. 5 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  7. Marko Pavlovic revised this gist Apr 11, 2016. 2 changed files with 34 additions and 2 deletions.
    30 changes: 30 additions & 0 deletions auth.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    /**
    * @path src/redux/modules
    *
    * Finally lets change old action using the client API
    * into a Firebase login.
    * Be sure to pass over a new 'password' param from containers/Login.
    *
    * Replace.
    */

    export function login(name, password) {
    return {
    types: [LOGIN, LOGIN_SUCCESS, LOGIN_FAIL],
    firebase: (fireRef) => fireRef.authWithPassword({password: password, email: name})
    }
    }

    /**
    -- OLD --
    export function login(name) {
    return {
    types: [LOGIN, LOGIN_SUCCESS, LOGIN_FAIL],
    promise: (client) => client.post('/login', {
    data: {
    name: name
    }
    })
    };
    }
    */
    6 changes: 4 additions & 2 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    /**
    * @path src
    *
    * Make the server.js adopt to the store changes.
    * Replace the lines.
    * Replace.
    */

    const store = createStore(history, {client});
    //const store = createStore(history, client);
    const store = createStore(history, {client});
  8. Marko Pavlovic created this gist Apr 11, 2016.
    28 changes: 28 additions & 0 deletions client.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    /**
    * @path src
    *
    * @diff - Differences with "react-redux-universal-hot-example"
    */
    import 'babel-polyfill';
    import React from 'react';
    import ReactDOM from 'react-dom';
    import createStore from './redux/create';
    import ApiClient from './helpers/ApiClient';
    // @diff include Firebase, this is my helper, you can just use firebase package
    import Firebase from './helpers/Firebase.js';
    import io from 'socket.io-client';
    import {Provider} from 'react-redux';
    import { Router, browserHistory } from 'react-router';
    import { ReduxAsyncConnect } from 'redux-async-connect';
    import useScroll from 'scroll-behavior/lib/useStandardScroll';

    import getRoutes from './routes';

    // @diff fire it up
    const firebase = new Firebase();
    const client = new ApiClient();
    const history = useScroll(() => browserHistory)();
    const dest = document.getElementById('content');
    // @diff param
    const store = createStore(history, {client, firebase}, window.__data);
    // ...
    48 changes: 48 additions & 0 deletions create.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    /**
    * @path src/redux
    *
    * Simple store binding change
    *
    * @diff - Differences with "react-redux-universal-hot-example"
    */

    import { createStore as _createStore, applyMiddleware, compose } from 'redux';
    import createMiddleware from './middleware/clientMiddleware';
    // @diff firebaseMiddleware
    import firebaseMiddleware from './middleware/firebaseMiddleware';
    import { syncHistory } from 'react-router-redux';

    // @diff accept firebase reference
    export default function createStore(history, {client, firebase}, data) {
    // Sync dispatched route actions to the history
    const reduxRouterMiddleware = syncHistory(history);

    // @diff line up firebase middleware
    const middleware = [createMiddleware(client), firebaseMiddleware(firebase), reduxRouterMiddleware];

    let finalCreateStore;
    if (__DEVELOPMENT__ && __CLIENT__ && __DEVTOOLS__) {
    const { persistState } = require('redux-devtools');
    const DevTools = require('../containers/DevTools/DevTools');
    finalCreateStore = compose(
    applyMiddleware(...middleware),
    window.devToolsExtension ? window.devToolsExtension() : DevTools.instrument(),
    persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/))
    )(_createStore);
    } else {
    finalCreateStore = applyMiddleware(...middleware)(_createStore);
    }

    const reducer = require('./modules/reducer');
    const store = finalCreateStore(reducer, data);

    reduxRouterMiddleware.listenForReplays(store);

    if (__DEVELOPMENT__ && module.hot) {
    module.hot.accept('./modules/reducer', () => {
    store.replaceReducer(require('./modules/reducer'));
    });
    }

    return store;
    }
    39 changes: 39 additions & 0 deletions firebaseMiddleware.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    /**
    * @path src/redux/middleware
    *
    * Extract from clientMiddleware example in order to showcase
    * the difference and help in understanding the same.
    *
    * @diff - Differences with "react-redux-universal-hot-example"
    */
    export default function firebaseMiddleware(fireRef) {
    return ({dispatch, getState}) => {
    return next => action => {
    if (typeof action === 'function') {
    return action(dispatch, getState);
    }

    // @diff Look for the firebase related actions instead of promise/client ones
    const { firebase, types, ...rest } = action; // eslint-disable-line no-redeclare

    // @diff if there is none, move on, same as promise/client
    if (!firebase) {
    return next(action);
    }

    const [REQUEST, SUCCESS, FAILURE] = types;
    next({...rest, type: REQUEST});

    const firebasePromise = firebase(fireRef);
    firebasePromise.then(
    (result) => console.log('result', result),
    (error) => console.log('error', error)
    ).catch((error)=> {
    console.error('MIDDLEWARE ERROR:', error);
    next({...rest, error, type: FAILURE});
    });

    return firebasePromise;
    };
    };
    }
    7 changes: 7 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    /**
    * Make the server.js adopt to the store changes.
    * Replace the lines.
    */

    //const store = createStore(history, client);
    const store = createStore(history, {client});