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

Eslint Refactor #46

Merged
merged 11 commits into from
Apr 29, 2016
17 changes: 12 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{ "extends": "eslint-config-airbnb",
{ "extends": [
"plugin:react/recommended",
"plugin:import/errors",
"plugin:import/warnings",
"eslint-config-airbnb/base",
"eslint-config-airbnb/rules/react"
],
"env": {
"browser": true,
"node": true,
Expand All @@ -13,6 +19,7 @@
"import/no-unresolved": 0,
"import/no-named-as-default": 2,
"comma-dangle": 0, // not sure why airbnb turned this on. gross!
"id-length": 0,
"indent": [2, 2, {"SwitchCase": 1}],
"no-console": 0,
"no-alert": 0,
Expand All @@ -21,17 +28,17 @@
"plugins": [
"react", "import"
],
"parser": "babel-eslint",
"settings": {
"import/parser": "babel-eslint",
"import/resolve": {
moduleDirectory: ["node_modules", "src"]
}
},
"import/ignore": [".scss$", ".css$", ".svg$"]
},
"globals": {
"__DEVELOPMENT__": true,
"__PRODUCTION__": true,
"__CLIENT__": true,
"__SERVER__": true,
"__DISABLE_SSR__": true,
"__DEVTOOLS__": true,
"socket": true,
"webpackIsomorphicTools": true
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"dev": "NODE_ENV=development node ./src/server/server.babel.js",
"build": "rimraf build && concurrently \"npm run build:client\" \"npm run build:server\"",
"bs": "rimraf build && concurrently \"npm run build:client\" \"npm run build:server\" \"npm run start\"",
"quickstart": "rimraf build && concurrently \"npm run build:db\" \"npm run build:client\" \"npm run build:server\" \"npm run prod\"",
"quickstart": "rimraf build && concurrently \"npm run build:db\" \"npm run build:client\" \"npm run build:server\" \"npm run start\"",
"build:client": "NODE_ENV=production webpack --config ./webpack/prod.babel.js",
"build:server": "NODE_ENV=production webpack --config ./webpack/server.babel.js",
"build:db": "node ./src/server/database/setupDB.babel.js",
Expand All @@ -27,11 +27,13 @@
"lint": "eslint 'src/**/*.js' 'tests/*.js' 'webpack/*.js'",
"profile": "NODE_ENV=production webpack --config ./webpack/prod.babel.js --profile --json > stats.json"
},
"pre-commit": [],
"devDependencies": {
"assets-webpack-plugin": "3.4.0",
"ava": "0.13.0",
"babel-cli": "6.6.5",
"babel-core": "6.7.4",
"babel-eslint": "^6.0.2",
"babel-loader": "6.2.4",
"babel-plugin-add-module-exports": "0.1.2",
"babel-plugin-react-transform": "2.0.2",
Expand All @@ -56,6 +58,7 @@
"null-loader": "0.1.1",
"postcss-loader": "0.8.2",
"postcss-modules-values": "1.1.2",
"pre-commit": "^1.1.2",
"raw-loader": "0.5.1",
"react-addons-test-utils": "0.14.8",
"react-transform-catch-errors": "1.0.2",
Expand Down Expand Up @@ -132,7 +135,7 @@
"socketcluster-client": "^4.3.13"
},
"engines": {
"node": "5.10.1",
"npm": "3.8.5"
"node": "5.10.1",
"npm": "3.8.5"
}
}
32 changes: 15 additions & 17 deletions src/client/Root.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import React, {Component} from 'react';
import React from 'react';
Copy link
Member

@mattkrick mattkrick Apr 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can you extend Component if you don't import it? Seriously, what is this magic? Do I not have to import Component to use it? Is that a thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's all functions now!

Copy link
Member

@mattkrick mattkrick Apr 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhhh! didn't see the change to stateless functions. nice!
@jordanh per dan abramov's medium post, i think the winds are changing to using webpack's stock HMR solution instead of proxying all of the react component to achieve HMR. This will be especially true since detecting stateless components in babel is error prone. Just something to put on the radar when we start focusing on the front end build. see: mattkrick/meatier#114

import {Router, browserHistory} from 'react-router';
import {Provider} from 'react-redux';
import routes from '../universal/routes/index';
import {syncHistoryWithStore} from 'react-router-redux';
import {ensureState} from 'redux-optimistic-ui';

export default class Root extends Component {
static propTypes = {
store: React.PropTypes.object.isRequired
}

render() {
const {store} = this.props;
const history = syncHistoryWithStore(browserHistory, store, {selectLocationState: state => ensureState(state).get('routing')});
return (
<Provider store={store}>
<div>
<Router history={history} routes={routes(store)}/>
</div>
</Provider>
);
}
export default function Root({store}) {
const history = syncHistoryWithStore(browserHistory, store,
{selectLocationState: state => ensureState(state).get('routing')});
return (
<Provider store={store}>
<div>
<Router history={history} routes={routes(store)} />
</div>
</Provider>
);
}

Root.propTypes = {
store: React.PropTypes.object.isRequired
};
2 changes: 1 addition & 1 deletion src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ const initialState = iMap([
]);

const store = makeStore(initialState);
render(<Root store={store}/>, document.getElementById('root'));
render(<Root store={store} />, document.getElementById('root'));
72 changes: 36 additions & 36 deletions src/server/Html.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
/* eslint react/no-danger:0 */
import React, {Component, PropTypes} from 'react';
import React, {PropTypes} from 'react';
import {Provider} from 'react-redux';
import {RouterContext} from 'react-router';
import {renderToString} from 'react-dom-stream/server';

// Injects the server rendered state and app into a basic html template
export default class Html extends Component {
static propTypes = {
store: PropTypes.object.isRequired,
title: PropTypes.string.isRequired,
assets: PropTypes.object,
renderProps: PropTypes.object
}

render() {
const PROD = process.env.NODE_ENV === 'production';
const {title, store, assets, renderProps} = this.props;
const {manifest, app, vendor} = assets || {};
const initialState = `window.__INITIAL_STATE__ = ${JSON.stringify(store.getState())}`;
const root = PROD && renderToString(
<Provider store={store}>
<RouterContext {...renderProps}/>
</Provider>);
return (
<html>
<head>
<meta charSet="utf-8"/>
<meta property="description" content="Team transparency, made easy." />
{PROD && <link rel="stylesheet" href="/static/prerender.css" type="text/css"/>}
<title>{title}</title>
</head>
<body>
<script dangerouslySetInnerHTML={{__html: initialState}}/>
{PROD ? <div id="root" dangerouslySetInnerHTML={{__html: root}}></div> : <div id="root"></div>}
{PROD && <script dangerouslySetInnerHTML={{__html: manifest.text}}/>}
{PROD && <script src={vendor.js}/>}
<script src={PROD ? app.js : '/static/app.js'}/>
</body>
</html>
);
}
export default function Html({title, store, assets, renderProps}) {
const PROD = process.env.NODE_ENV === 'production';
const {manifest, app, vendor} = assets || {};
const initialState = `window.__INITIAL_STATE__ = ${JSON.stringify(store.getState())}`;
const root = PROD && renderToString(
<Provider store={store}>
<RouterContext {...renderProps} />
</Provider>);
return (
<html>
<head>
<meta charSet="utf-8" />
<meta property="description" content="Team transparency, made easy." />
{PROD && <link rel="stylesheet" href="/static/prerender.css" type="text/css" />}
<title>{title}</title>
</head>
<body>
<script dangerouslySetInnerHTML={{__html: initialState}} />
{PROD ?
<div id="root" dangerouslySetInnerHTML={{__html: root}}></div> :
<div id="root"></div>
}
{PROD && <script dangerouslySetInnerHTML={{__html: manifest.text}} />}
{PROD && <script src={vendor.js} />}
<script src={PROD ? app.js : '/static/app.js'} />
</body>
</html>
);
}

Html.propTypes = {
store: PropTypes.object.isRequired,
title: PropTypes.string.isRequired,
assets: PropTypes.object,
renderProps: PropTypes.object
};
3 changes: 1 addition & 2 deletions src/server/broker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

module.exports.run = function () {
module.exports.run = () => {
console.log(' >> Broker PID:', process.pid);
};
7 changes: 4 additions & 3 deletions src/server/createSSR.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function renderApp(res, store, assets, renderProps) {
store={store}
assets={assets}
renderProps={renderProps}
/>);
/>);
res.write('<!DOCTYPE html>');
htmlStream.pipe(res, {end: false});
htmlStream.on('end', () => res.end());
Expand All @@ -35,8 +35,9 @@ export default async function createSSR(req, res) {
const makeRoutes = require('../../build/prerender');
const assets = require('../../build/assets.json');
const readFile = promisify(fs.readFile);
assets.manifest.text = await readFile(join(__dirname, '..', '..', 'build', basename(assets.manifest.js)), 'utf-8');
const routes = makeRoutes(store);
assets.manifest.text = await readFile(join(__dirname, '..', '..', 'build',
basename(assets.manifest.js)), 'utf-8');
const routes = makeRoutes(store);
match({routes, location: req.url}, (error, redirectLocation, renderProps) => {
if (error) {
res.status(500).send(error.message);
Expand Down
22 changes: 11 additions & 11 deletions src/server/database/setupDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ const database = [
{name: 'Meeting', indices: []}
];

export default async function setupDB(isUpdate = false) {
await Promise.all(databases.map(db => ({db, isUpdate})).map(reset));
await r.getPool().drain();
console.log(`>>Database setup complete!`);
}

async function reset({db, isUpdate}) {
const dbList = await r.dbList();
if (dbList.indexOf(db) === -1) {
Expand All @@ -36,11 +30,11 @@ async function reset({db, isUpdate}) {
return Promise.resolve(false);
}));
console.log(`>>Adding table indices on: ${db}`);
const tableIndicies = await Promise.all(database.map(table => {
return r.db(db).table(table.name).indexList().run();
}));
await Promise.all([...database.map((table, i) => {
const indicies = tableIndicies[i] || [];
const tableIndicies = await Promise.all(database.map(table =>
r.db(db).table(table.name).indexList().run()
));
await Promise.all([...database.map((table, ind) => {
const indicies = tableIndicies[ind] || [];
return table.indices.map(index => {
if (indicies.indexOf(index) === -1) {
return r.db(db).table(table.name).indexCreate(index).run();
Expand All @@ -50,3 +44,9 @@ async function reset({db, isUpdate}) {
})]);
console.log(`>>Setup complete for: ${db}`);
}

export default async function setupDB(isUpdate = false) {
await Promise.all(databases.map(db => ({db, isUpdate})).map(reset));
await r.getPool().drain();
console.log('>>Database setup complete!');
}
16 changes: 8 additions & 8 deletions src/server/graphql/models/CachedUser/cachedUserMutation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import r from '../../../database/rethinkdriver';
import {GraphQLString} from 'graphql';
import {CachedUser} from './cachedUserSchema';
import {getUserByUserId} from './helpers';
import { AuthenticationClient } from 'auth0';
import {auth0} from '../../../../universal/utils/clientOptions';

//TODO this stuff is no good, we need the good server stuff so we don't 401
// TODO this stuff is no good, we need the good server stuff so we don't 401
const auth0Client = new AuthenticationClient({
domain: auth0.account,
clientId: auth0.clientId
Expand All @@ -22,15 +21,16 @@ export default {
},
async resolve(source, {idToken}) {
const userInfo = await auth0Client.tokens.getInfo(idToken);
//TODO add the userId to the JWT to eliminate call to DB? JWT.sub is the userId, not id, maybe it'll do
//TODO loginsCount and blockedFor are not a part of this API response
const user = await getUserByUserId(userInfo.user_id); //eslint-disable-line camelcase
const id = user && user.id;
// TODO add the userId to the JWT to eliminate call to DB?
// JWT.sub is the userId, not id, maybe it'll do
// TODO loginsCount and blockedFor are not a part of this API response
// const user = await getUserByUserId(userInfo.user_id); // eslint-disable-line camelcase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...(cont'd) as in, none of these constants were populated on the below object?

// const id = user && user.id;
const newUserObj = {
cachedAt: new Date(),
//TODO set expiry here
// TODO set expiry here
cacheExpiresAt: new Date(),
//from auth0
// from auth0
createdAt: userInfo.created_at,
updatedAt: userInfo.updated_at,
userId: userInfo.user_id,
Expand Down
7 changes: 5 additions & 2 deletions src/server/graphql/models/CachedUser/cachedUserQuery.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {GraphQLNonNull, GraphQLID} from 'graphql';
import {CachedUser} from './cachedUserSchema';
import {getUserByUserId} from './helpers';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! Did you fix a bug by adding this line?

import {errorObj} from '../utils';

export default {
getUserByUserId: {
type: CachedUser,
args: {
userId: {type: new GraphQLNonNull(GraphQLID), description: 'The user ID for the desired profile'}
userId: {
type: new GraphQLNonNull(GraphQLID),
description: 'The user ID for the desired profile'
}
},
async resolve(source, {userId}) {
const user = await getUserByUserId(userId);
Expand All @@ -17,4 +21,3 @@ export default {
}
}
};

Loading