| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { createReduxStore, register } from '@wordpress/data'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Internal dependencies |
| 8 |
*/ |
| 9 |
import { PostFilters } from '../../common/utils/constants'; |
| 10 |
import { PostData } from '../../common/utils/post'; |
| 11 |
|
| 12 |
/** |
| 13 |
* Represents the state for the Related Posts sidebar component. |
| 14 |
* |
| 15 |
* @since 3.18.0 |
| 16 |
*/ |
| 17 |
interface PostsState { |
| 18 |
isFirstRun: boolean; |
| 19 |
isLoading: boolean; |
| 20 |
filters: PostFilters; |
| 21 |
posts: PostData[]; |
| 22 |
} |
| 23 |
|
| 24 |
interface SetFirstRunAction { |
| 25 |
type: 'SET_FIRST_RUN'; |
| 26 |
isFirstRun: boolean; |
| 27 |
} |
| 28 |
|
| 29 |
interface SetLoadingAction { |
| 30 |
type: 'SET_LOADING'; |
| 31 |
isLoading: boolean; |
| 32 |
} |
| 33 |
|
| 34 |
interface SetPostsAction { |
| 35 |
type: 'SET_POSTS'; |
| 36 |
posts: PostData[]; |
| 37 |
} |
| 38 |
|
| 39 |
interface SetFilterParamsAction { |
| 40 |
type: 'SET_FILTERS'; |
| 41 |
filters: PostFilters; |
| 42 |
} |
| 43 |
|
| 44 |
interface ResetAction { |
| 45 |
type: 'RESET'; |
| 46 |
} |
| 47 |
|
| 48 |
type ActionTypes = SetFirstRunAction | SetLoadingAction | SetPostsAction | SetFilterParamsAction | ResetAction; |
| 49 |
|
| 50 |
const defaultState: PostsState = { |
| 51 |
isFirstRun: true, |
| 52 |
isLoading: false, |
| 53 |
filters: { |
| 54 |
author: '', |
| 55 |
section: '', |
| 56 |
tags: [], |
| 57 |
}, |
| 58 |
posts: [], |
| 59 |
}; |
| 60 |
|
| 61 |
/** |
| 62 |
* Redux store for managing filter and posts state for the Related Posts sidebar component. |
| 63 |
* |
| 64 |
* @since 3.18.0 |
| 65 |
*/ |
| 66 |
export const RelatedPostsStore = createReduxStore( 'wp-parsely/related-posts', { |
| 67 |
initialState: defaultState, |
| 68 |
reducer( state: PostsState = defaultState, action: ActionTypes ): PostsState { |
| 69 |
switch ( action.type ) { |
| 70 |
case 'SET_FIRST_RUN': |
| 71 |
return { |
| 72 |
...state, |
| 73 |
isFirstRun: action.isFirstRun, |
| 74 |
}; |
| 75 |
case 'SET_LOADING': |
| 76 |
return { |
| 77 |
...state, |
| 78 |
isLoading: action.isLoading, |
| 79 |
}; |
| 80 |
case 'SET_POSTS': |
| 81 |
return { |
| 82 |
...state, |
| 83 |
posts: action.posts, |
| 84 |
}; |
| 85 |
case 'SET_FILTERS': |
| 86 |
return { |
| 87 |
...state, |
| 88 |
filters: action.filters, |
| 89 |
}; |
| 90 |
case 'RESET': |
| 91 |
return defaultState; |
| 92 |
default: |
| 93 |
return state; |
| 94 |
} |
| 95 |
}, |
| 96 |
actions: { |
| 97 |
setFirstRun( isFirstRun: boolean ): SetFirstRunAction { |
| 98 |
return { |
| 99 |
type: 'SET_FIRST_RUN', |
| 100 |
isFirstRun, |
| 101 |
}; |
| 102 |
}, |
| 103 |
setLoading( isLoading: boolean ): SetLoadingAction { |
| 104 |
return { |
| 105 |
type: 'SET_LOADING', |
| 106 |
isLoading, |
| 107 |
}; |
| 108 |
}, |
| 109 |
setPosts( posts: PostData[] ): SetPostsAction { |
| 110 |
return { |
| 111 |
type: 'SET_POSTS', |
| 112 |
posts, |
| 113 |
}; |
| 114 |
}, |
| 115 |
setFilters( filters: PostFilters ): SetFilterParamsAction { |
| 116 |
return { |
| 117 |
type: 'SET_FILTERS', |
| 118 |
filters, |
| 119 |
}; |
| 120 |
}, |
| 121 |
/** |
| 122 |
* Resets the state to the default state. Useful for testing. |
| 123 |
* |
| 124 |
* @since 3.18.0 |
| 125 |
* |
| 126 |
* @return {ResetAction} Action object for resetting the state. |
| 127 |
*/ |
| 128 |
reset(): ResetAction { |
| 129 |
return { |
| 130 |
type: 'RESET', |
| 131 |
}; |
| 132 |
}, |
| 133 |
}, |
| 134 |
selectors: { |
| 135 |
getState( state: PostsState ): PostsState { |
| 136 |
return state; |
| 137 |
}, |
| 138 |
isFirstRun( state: PostsState ): boolean { |
| 139 |
return state.isFirstRun; |
| 140 |
}, |
| 141 |
isLoading( state: PostsState ): boolean { |
| 142 |
return state.isLoading; |
| 143 |
}, |
| 144 |
getPosts( state: PostsState ): PostData[] { |
| 145 |
return state.posts; |
| 146 |
}, |
| 147 |
getFilters( state: PostsState ) { |
| 148 |
return state.filters; |
| 149 |
}, |
| 150 |
}, |
| 151 |
} ); |
| 152 |
|
| 153 |
register( RelatedPostsStore ); |
| 154 |
|