Skip to content

Commit

Permalink
move all jet logic to store.js
Browse files Browse the repository at this point in the history
  • Loading branch information
lipp committed Dec 21, 2016
1 parent c8c426e commit 1d40a26
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -14,7 +14,7 @@ The canonical TodoApp using [node-jet](https://github.com/lipp/node-jet) as real

It's note worthy, that the [redux store](https://github.com/lipp/next-todos/blob/master/store.js#L16) is identical for server and client js code! The only difference is that the store population on the server is triggered by the "finite" [redux-jet get](https://github.com/lipp/next-todos/blob/master/pages/index.js#L49) whereas the client issues an "infinite / streaming" [redux-jet fetch](https://github.com/lipp/next-todos/blob/master/pages/index.js#L56)

The [components](https://github.com/lipp/next-todos/blob/master/components/) are jet-agnostic. All the jet related stuff are located in [pages/index.js](https://github.com/lipp/next-todos/blob/master/pages/index.js) and [store.js](https://github.com/lipp/next-todos/blob/master/store.js).
The [components](https://github.com/lipp/next-todos/blob/master/components/) are jet-agnostic. All the jet related stuff is located in [containers dir](https://github.com/lipp/next-todos/blob/master/containers) and [store.js](https://github.com/lipp/next-todos/blob/master/store.js).

# Server

Expand Down
13 changes: 2 additions & 11 deletions pages/index.js
@@ -1,31 +1,22 @@
import React from 'react'
import Head from 'next/head'
import {Provider} from 'react-redux'
import {get, fetch} from 'redux-jet'
import connection from '../connection'
import initStore from '../store.js'
import Todos from '../containers/Todos'
import AddTodoForm from '../containers/AddTodoForm'
import Footer from '../containers/Footer'

const todoExpression = {
path: {startsWith: 'todo/#'},
sort: {byValueField: {id: 'number'}, from: 1, to: 100}
}

export default class App extends React.Component {

static getInitialProps ({req}) {
if (req) {
const store = initStore()
return get(connection, todoExpression, 'todos')(store.dispatch).then(() => {
return {initialState: store.getState(), store}
})
return store.getInitialState().then(initialState => ({initialState, store}))
}
}

componentDidMount () {
fetch(connection, todoExpression, 'todos')(this.store.dispatch)
this.store.resume()
}

constructor (props) {
Expand Down
20 changes: 16 additions & 4 deletions store.js
@@ -1,14 +1,26 @@
import {createStore, applyMiddleware, combineReducers} from 'redux'
import {sorted} from 'redux-jet'
import {sorted, get, fetch} from 'redux-jet'
import thunk from 'redux-thunk'
import connection from './connection'

export default (initialState) => {
const initialTodos = initialState ? initialState.todos : []
const query = {
path: {startsWith: 'todo/#'},
sort: {byValueField: {id: 'number'}, from: 1, to: 100}
}

export default (initialState) => {
const store = createStore(combineReducers({
todos: sorted('todos', initialTodos),
todos: sorted('todos', initialState ? initialState.todos : []),
filter: (state = 'all', action) => action.type === 'SET_FILTER' ? action.filter : state,
setAllValue: (state = true, action) => action.type === 'TOGGLE_SET_ALL_VALUE' ? !state : state
}), applyMiddleware(thunk))

if (initialState) {
store.resume = () => fetch(connection, query, 'todos')(store.dispatch)
} else {
store.getInitialState = () => {
return get(connection, query, 'todos')(store.dispatch).then(() => store.getState())
}
}
return store
}

0 comments on commit 1d40a26

Please sign in to comment.