summaryrefslogtreecommitdiff
path: root/src/redux
diff options
context:
space:
mode:
authorKamran Ahmed <kamranahmed.se@gmail.com>2018-08-26 11:41:34 +0500
committerKamran Ahmed <kamranahmed.se@gmail.com>2018-08-26 11:41:34 +0500
commitabb2994fcf4c41fc9c7e45030d7e7e66b3b6e791 (patch)
treefcb36751682c5e2c47cd7297a0fa099608314edb /src/redux
parentLoading indicator in feed (diff)
downloadgithunt-abb2994fcf4c41fc9c7e45030d7e7e66b3b6e791.tar.gz
Cache and reset cache after an hour
Diffstat (limited to 'src/redux')
-rw-r--r--src/redux/github/actions.js6
-rw-r--r--src/redux/github/reducer.js12
-rw-r--r--src/redux/github/transform.js48
3 files changed, 64 insertions, 2 deletions
diff --git a/src/redux/github/actions.js b/src/redux/github/actions.js
index a22612b..032f991 100644
--- a/src/redux/github/actions.js
+++ b/src/redux/github/actions.js
@@ -39,7 +39,11 @@ export const fetchTrending = function (filters) {
}).then(response => {
dispatch({
type: FETCH_TRENDING_SUCCESS,
- payload: response.data
+ payload: {
+ start: filters.dateRange.start,
+ end: filters.dateRange.end,
+ data: response.data
+ }
});
}).catch(error => {
dispatch({
diff --git a/src/redux/github/reducer.js b/src/redux/github/reducer.js
index 8f6c037..1cb7d71 100644
--- a/src/redux/github/reducer.js
+++ b/src/redux/github/reducer.js
@@ -1,4 +1,5 @@
import { FETCH_TRENDING_FAILED, FETCH_TRENDING_SUCCESS, PROCESS_FETCH_TRENDING, UPDATE_FILTERS } from './types';
+import { UPDATE_DATE_TYPE } from '../preference/types';
export const initialState = {
processing: false,
@@ -20,10 +21,19 @@ export default function reducer(state = initialState, action) {
processing: true,
error: null
};
+ case UPDATE_DATE_TYPE:
+ return {
+ ...state,
+ ...initialState
+ };
case FETCH_TRENDING_SUCCESS:
return {
...state,
- repositories: action.payload,
+ // Append the fetched repositories to existing list
+ repositories: [
+ ...state.repositories,
+ action.payload
+ ],
processing: false,
error: null
};
diff --git a/src/redux/github/transform.js b/src/redux/github/transform.js
new file mode 100644
index 0000000..af788a6
--- /dev/null
+++ b/src/redux/github/transform.js
@@ -0,0 +1,48 @@
+import { createTransform } from 'redux-persist';
+import moment from 'moment';
+import { initialState } from './reducer';
+
+const GithubTransform = createTransform(
+ // transform state on its way to being serialized and persisted.
+ (inboundState, key) => {
+ inboundState = inboundState || {};
+
+ // If given data does not have persistence configuration before
+ if (!inboundState.__persisted_at) {
+ inboundState = {
+ ...inboundState,
+ __persisted_at: moment()
+ };
+ }
+
+ return inboundState;
+ },
+ // 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;
+ },
+ // define which reducers this transform gets called for.
+ {
+ whitelist: ['github']
+ }
+);
+
+export default GithubTransform;