From bbddb7ae279faf14a3ec3c3ad1490430ae8b1d6d Mon Sep 17 00:00:00 2001 From: Kimmo Puputti Date: Mon, 20 Feb 2017 13:47:05 +0200 Subject: [PATCH] Inject the SDK instance to the root saga --- src/index.js | 13 +++++++++++-- src/sagas.js | 42 ++++-------------------------------------- 2 files changed, 15 insertions(+), 40 deletions(-) diff --git a/src/index.js b/src/index.js index 62052893..9a1bec46 100644 --- a/src/index.js +++ b/src/index.js @@ -13,10 +13,12 @@ import React from 'react'; import ReactDOM from 'react-dom'; +import { createInstance, types } from 'sharetribe-sdk'; import { ClientApp, renderApp } from './app'; import configureStore from './store'; import { matchPathname } from './util/routes'; -import rootSaga from './sagas'; +import createRootSaga from './sagas'; +import config from './config'; import './index.css'; @@ -25,9 +27,16 @@ if (typeof window !== 'undefined') { // eslint-disable-next-line no-underscore-dangle const preloadedState = window.__PRELOADED_STATE__ || {}; const store = configureStore(preloadedState); - store.runSaga(rootSaga); + const sdk = createInstance({ clientId: config.sdk.clientId, baseUrl: config.sdk.baseUrl }); + + store.runSaga(createRootSaga(sdk)); ReactDOM.render(, document.getElementById('root')); + + // Expose stuff for the browser REPL + if (config.dev) { + window.app = { config, sdk, sdkTypes: types }; + } } // Export the function for server side rendering. diff --git a/src/sagas.js b/src/sagas.js index dd56cfef..41af2d2b 100644 --- a/src/sagas.js +++ b/src/sagas.js @@ -1,42 +1,8 @@ -import { createInstance } from 'sharetribe-sdk'; - import { watchAuth } from './ducks/Auth.duck'; import { watchLoadListings } from './containers/SearchPage/SearchPage.duck'; -// eslint-disable-next-line no-unused-vars -import fakeSdk from './util/fakeSDK'; -const clientId = '08ec69f6-d37e-414d-83eb-324e94afddf0'; -const baseUrl = 'http://localhost:8088'; +const createRootSaga = sdk => function* rootSaga() { + yield [watchAuth(sdk), watchLoadListings(sdk)]; +}; -const sdk = createInstance({ clientId, baseUrl }); - -const mockLogin = (email, password) => new Promise((resolve, reject) => { - window.setTimeout( - () => { - if (email && password) { - // any non-empty email and password is ok - resolve({ email, password }); - } else { - reject(new Error('Invalid credentials, try a valid email and a non-empty password')); - } - }, - 1000, - ); -}); - -const mockLogout = () => new Promise(resolve => { - window.setTimeout(resolve, 1000); -}); - -// Temporary mock SDK -const mockSdk = { login: mockLogin, logout: mockLogout }; - -export default function* rootSaga() { - // TODO: inject sdk in function arguments - - // FakeSDK - // yield [watchAuth(mockSdk), watchLoadListings(fakeSdk)]; - - // Real SDK - yield [watchAuth(mockSdk), watchLoadListings(sdk)]; -} +export default createRootSaga;