From 1d40a264975d66b9502f941f34ece70c0a21aa12 Mon Sep 17 00:00:00 2001 From: Gerhard Preuss Date: Wed, 21 Dec 2016 13:53:55 +0100 Subject: [PATCH] move all jet logic to store.js --- README.md | 2 +- pages/index.js | 13 ++----------- store.js | 20 ++++++++++++++++---- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a08ce85..e9c9b6a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pages/index.js b/pages/index.js index c5ffa75..be92026 100644 --- a/pages/index.js +++ b/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) { diff --git a/store.js b/store.js index 4513222..1c70347 100644 --- a/store.js +++ b/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 }