Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redux devtools and process concurrent #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ npm install
cd server && npm install
cd ..

# Run the server
npm run server

# New terminal window
# Run app
npm start
```

Expand Down Expand Up @@ -95,13 +92,13 @@ function receiveLogout() {
// Calls the API to get a token and
// dispatches actions along the way
export function loginUser(creds) {

let config = {
method: 'POST',
headers: { 'Content-Type':'application/x-www-form-urlencoded' },
body: `username=${creds.username}&password=${creds.password}`
}

return dispatch => {
// We dispatch requestLogin to kickoff the call to the API
dispatch(requestLogin(creds))
Expand All @@ -119,7 +116,7 @@ export function loginUser(creds) {
else {
// If login was successful, set the token in local storage
localStorage.setItem('id_token', user.id_token)

// Dispatch the success action
dispatch(receiveLogin(user))
}
Expand All @@ -145,10 +142,10 @@ We also have actions for retreiving the quotes that uses an API middleware.
const BASE_URL = 'http://localhost:3001/api/'

function callApi(endpoint, authenticated) {

let token = localStorage.getItem('id_token') || null
let config = {}

if(authenticated) {
if(token) {
config = {
Expand All @@ -158,7 +155,7 @@ function callApi(endpoint, authenticated) {
throw "No token saved!"
}
}

return fetch(BASE_URL + endpoint, config)
.then(response =>
response.text()
Expand All @@ -167,26 +164,26 @@ function callApi(endpoint, authenticated) {
if (!response.ok) {
return Promise.reject(text)
}

return text
}).catch(err => console.log(err))
}

export const CALL_API = Symbol('Call API')

export default store => next => action => {

const callAPI = action[CALL_API]

// So the middleware doesn't get applied to every single action
if (typeof callAPI === 'undefined') {
return next(action)
}

let { endpoint, types, authenticated } = callAPI

const [ requestType, successType, errorType ] = types

// Passing the authenticated boolean back in our data will let us distinguish between normal and secret quotes
return callApi(endpoint, authenticated).then(
response =>
Expand Down Expand Up @@ -216,7 +213,7 @@ export function fetchQuote() {
}
}

// Same API middlware is used to get a
// Same API middlware is used to get a
// secret quote, but we set authenticated
// to true so that the auth header is sent
export function fetchSecretQuote() {
Expand All @@ -236,7 +233,7 @@ The reducers return new objects with the data carried by the actions.
// reducers.js

import { combineReducers } from 'redux'
import {
import {
LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE, LOGOUT_SUCCESS,
QUOTE_REQUEST, QUOTE_SUCCESS, QUOTE_FAILURE
} from './actions'
Expand Down
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,29 @@ import quotesApp from './reducers'
import thunkMiddleware from 'redux-thunk'
import api from './middleware/api'


import { createDevTools } from 'redux-devtools'
import LogMonitor from 'redux-devtools-log-monitor'
import DockMonitor from 'redux-devtools-dock-monitor'

const DevTools = createDevTools(
<DockMonitor toggleVisibilityKey="ctrl-h" changePositionKey="ctrl-q">
<LogMonitor theme="tomorrow" preserveScrollTop={false} />
</DockMonitor>
)

let createStoreWithMiddleware = applyMiddleware(thunkMiddleware, api)(createStore)

let store = createStoreWithMiddleware(quotesApp)
let store = createStoreWithMiddleware(quotesApp, DevTools.instrument())

let rootElement = document.getElementById('root')

render(
<Provider store={store}>
<App />
<div>
<App />
<DevTools />
</div>
</Provider>,
rootElement
)

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"main": "index.js",
"scripts": {
"server": "node server/server.js",
"start": "node server.js"
"client": "node server.js",
"start": "concurrently --kill-others \"npm run server\" \"npm run client\""
},
"author": "Auth0",
"license": "MIT",
Expand All @@ -19,7 +20,11 @@
"babel-core": "^5.6.18",
"babel-loader": "^5.1.4",
"babel-plugin-react-transform": "^1.1.0",
"concurrently": "^2.0.0",
"express": "^4.13.3",
"redux-devtools": "^3.1.1",
"redux-devtools-dock-monitor": "^1.1.0",
"redux-devtools-log-monitor": "^1.0.5",
"webpack": "^1.9.11",
"webpack-dev-middleware": "^1.2.0",
"webpack-hot-middleware": "^2.2.0"
Expand Down