Skip to content

Commit

Permalink
Use process.browser; inline props
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam committed Jan 22, 2017
1 parent e2efe97 commit e6d777a
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 27 deletions.
4 changes: 2 additions & 2 deletions examples/with-apollo/components/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default (props) => (
export default ({ children }) => (
<main>
{props.children}
{children}
<style jsx global>{`
* {
font-family: Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, serif;
Expand Down
6 changes: 3 additions & 3 deletions examples/with-apollo/components/Header.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Link from 'next/prefetch'

export default (props) => (
export default ({ pathname }) => (
<header>
<Link href='/'>
<a className={props.pathname === '/' && 'is-active'}>Home</a>
<a className={pathname === '/' && 'is-active'}>Home</a>
</Link>

<Link href='/about'>
<a className={props.pathname === '/about' && 'is-active'}>About</a>
<a className={pathname === '/about' && 'is-active'}>About</a>
</Link>

<style jsx>{`
Expand Down
10 changes: 5 additions & 5 deletions examples/with-apollo/components/PostList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import PostUpvoter from './PostUpvoter'

const POSTS_PER_PAGE = 10

function PostList (props) {
if (props.data.loading) {
function PostList ({ data: { allPosts, loading, _allPostsMeta }, loadMorePosts }) {
if (loading) {
return <div>Loading</div>
}

const areMorePosts = props.data.allPosts.length < props.data._allPostsMeta.count
const areMorePosts = allPosts.length < _allPostsMeta.count

return (
<section>
<ul>
{props.data.allPosts
{allPosts
.sort((x, y) => new Date(y.createdAt) - new Date(x.createdAt))
.map((post, index) =>
<li key={post.id}>
Expand All @@ -26,7 +26,7 @@ function PostList (props) {
</li>
)}
</ul>
{areMorePosts ? <button onClick={() => props.loadMorePosts()}><span />Show More</button> : ''}
{areMorePosts ? <button onClick={() => loadMorePosts()}><span />Show More</button> : ''}
<style jsx>{`
section {
padding-bottom: 20px;
Expand Down
6 changes: 3 additions & 3 deletions examples/with-apollo/components/PostUpvoter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from 'react'
import gql from 'graphql-tag'
import { graphql } from 'react-apollo'

function PostUpvoter (props) {
function PostUpvoter ({ upvote, votes, id }) {
return (
<button onClick={() => props.upvote(props.id, props.votes + 1)}>
{props.votes}
<button onClick={() => upvote(id, votes + 1)}>
{votes}
<style jsx>{`
button {
background-color: transparent;
Expand Down
7 changes: 5 additions & 2 deletions examples/with-apollo/components/Submit.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import gql from 'graphql-tag'
import { graphql } from 'react-apollo'

function Submit (props) {
function Submit ({ createPost }) {
function handleSubmit (e) {
e.preventDefault()

let title = e.target.elements.title.value
let url = e.target.elements.url.value

if (title === '' || url === '') {
window.alert('Both fields are required.')
return false
Expand All @@ -15,7 +17,8 @@ function Submit (props) {
if (!url.match(/^[a-zA-Z]+:\/\//)) {
url = `http://${url}`
}
props.createPost(title, url)

createPost(title, url)

// reset form
e.target.elements.title.value = ''
Expand Down
2 changes: 0 additions & 2 deletions examples/with-apollo/lib/exenv.js

This file was deleted.

5 changes: 2 additions & 3 deletions examples/with-apollo/lib/initClient.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import ApolloClient, { createNetworkInterface } from 'apollo-client'
import { IS_SERVER } from './exenv'

export const initClient = (headers) => {
const client = new ApolloClient({
ssrMode: IS_SERVER,
ssrMode: !process.browser,
headers,
dataIdFromObject: result => result.id || null,
networkInterface: createNetworkInterface({
Expand All @@ -13,7 +12,7 @@ export const initClient = (headers) => {
}
})
})
if (IS_SERVER) {
if (!process.browser) {
return client
}
if (!window.APOLLO_CLIENT) {
Expand Down
5 changes: 2 additions & 3 deletions examples/with-apollo/lib/initStore.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { createStore } from 'redux'
import { IS_SERVER } from './exenv'
import getReducer from './reducer'
import createMiddleware from './middleware'

export const initStore = (client, initialState) => {
let store
if (IS_SERVER || !window.REDUX_STORE) {
if (!process.browser || !window.REDUX_STORE) {
const middleware = createMiddleware(client.middleware())
store = createStore(getReducer(client), initialState, middleware)
if (IS_SERVER) {
if (!process.browser) {
return store
}
window.REDUX_STORE = store
Expand Down
3 changes: 1 addition & 2 deletions examples/with-apollo/lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { applyMiddleware, compose } from 'redux'
import { IS_BROWSER } from './exenv'

export default function createMiddleware (clientMiddleware) {
const middleware = applyMiddleware(clientMiddleware)
if (IS_BROWSER && window.devToolsExtension) {
if (process.browser && window.devToolsExtension) {
return compose(middleware, window.devToolsExtension())
}
return middleware
Expand Down
3 changes: 1 addition & 2 deletions examples/with-apollo/lib/withData.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ApolloProvider, getDataFromTree } from 'react-apollo'
import React from 'react'
import 'isomorphic-fetch'
import { IS_SERVER } from './exenv'
import { initClient } from './initClient'
import { initStore } from './initStore'

Expand All @@ -12,7 +11,7 @@ export default (Component) => (
const client = initClient(headers)
const store = initStore(client, client.initialState)

if (IS_SERVER) {
if (!process.browser) {
const app = (
<ApolloProvider client={client} store={store}>
<Component url={{ query: ctx.query, pathname: ctx.pathname }} />
Expand Down

0 comments on commit e6d777a

Please sign in to comment.