From fb142d6bab22a39d19f2c41b24f899b59371c407 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Mon, 15 Oct 2018 18:21:36 +0400 Subject: Add redux persist expire and update dependencies --- src/components/repository-grid/grid-item/index.js | 9 ++++--- src/components/repository-list/list-item/index.js | 8 ++++-- src/redux/github/transform.js | 30 +---------------------- src/store.js | 9 ++++++- 4 files changed, 21 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/components/repository-grid/grid-item/index.js b/src/components/repository-grid/grid-item/index.js index ac7c9c2..582d348 100644 --- a/src/components/repository-grid/grid-item/index.js +++ b/src/components/repository-grid/grid-item/index.js @@ -13,7 +13,7 @@ class GridItem extends React.Component {
- +
{ @@ -29,12 +29,12 @@ class GridItem extends React.Component {
- + { this.props.repository.name }

- Built by · { this.props.repository.owner.login } · { moment(this.props.repository.created_at).format('MMMM D, YYYY') } + Built by · { this.props.repository.owner.login } · { moment(this.props.repository.created_at).format('MMMM D, YYYY') }

@@ -53,18 +53,21 @@ class GridItem extends React.Component { } { this.props.repository.stargazers_count.toLocaleString() } { this.props.repository.forks.toLocaleString() } { this.props.repository.watchers.toLocaleString() } diff --git a/src/components/repository-list/list-item/index.js b/src/components/repository-list/list-item/index.js index 1d9bcac..0a93ae6 100644 --- a/src/components/repository-list/list-item/index.js +++ b/src/components/repository-list/list-item/index.js @@ -14,12 +14,12 @@ class ListItem extends React.Component {

- + { this.props.repository.owner.login } / { this.props.repository.name }

-

Built by · { this.props.repository.owner.login } · { moment(this.props.repository.created_at).format('MMMM D YYYY') }

+

Built by · { this.props.repository.owner.login } · { moment(this.props.repository.created_at).format('MMMM D YYYY') }

{ this.props.repository.description || 'No description given.' }

@@ -37,18 +37,21 @@ class ListItem extends React.Component { } { this.props.repository.stargazers_count.toLocaleString() } { this.props.repository.forks.toLocaleString() } { this.props.repository.watchers.toLocaleString() } @@ -58,6 +61,7 @@ class ListItem extends React.Component { { inboundState = inboundState || {}; - // If given data does not have persistence configuration before - if (!inboundState.__persisted_at) { - inboundState = { - ...inboundState, - __persisted_at: moment() - }; - } - // Keep the first group only to avoid overflowing the storage if (inboundState.repositories && inboundState.repositories.length > 1) { inboundState = { @@ -37,25 +27,7 @@ const GithubTransform = createTransform( }, // transform state being rehydrated (outboundState, key) => { - outboundState = outboundState || {}; - - if (outboundState.__persisted_at) { - const startTime = moment(outboundState.__persisted_at); - const endTime = moment(); - - const duration = moment.duration(endTime.diff(startTime)); - const seconds = duration.asSeconds(); - - // If the state is more than one hour old - // reset it to initial state - if (seconds > 3600) { - return { - ...initialState - }; - } - } - - return outboundState; + return { ...outboundState }; }, // define which reducers this transform gets called for. { diff --git a/src/store.js b/src/store.js index c8e72b1..cab211a 100644 --- a/src/store.js +++ b/src/store.js @@ -4,8 +4,10 @@ import { persistReducer, persistStore } from 'redux-persist'; import { applyMiddleware, createStore } from 'redux'; import storage from 'redux-persist/lib/storage'; import thunk from 'redux-thunk'; +import expireReducer from 'redux-persist-expire'; import GithubTransform from './redux/github/transform'; +import githubState from './redux/github/reducer'; import rootReducer from './redux/reducers'; const persistedReducers = persistReducer( @@ -14,7 +16,12 @@ const persistedReducers = persistReducer( storage: storage, stateReconciler: autoMergeLevel2, transforms: [ - GithubTransform + GithubTransform, + expireReducer('github', { + expireSeconds: 3600, + expiredState: { ...githubState }, + autoExpire: true + }) ] }, rootReducer, -- cgit v1.3