Redux middleware for Algolia Search.
- Install the package via npm
npm install redux-algolia --save
or yarn
yarn add redux-algolia
- Combine with store in
configureStore.js
Make sure you havealgoliasearch
installed
import { createStore, applyMiddleware, combineReducers } from 'redux'
import createAlgoliaMiddleware from 'redux-algolia'
import reducers from './reducers'
import algoliasearch from 'algoliasearch'
const algoliaClient = algoliasearch(
config.algoliaAppId,
config.algoliaSearchApiKey
)
const reducer = combineReducers(reducers)
const middlewares = [
// ...other middlewares,
createAlgoliaMiddleware({
algoliaClient, // required
transformIndexName: (name) => name,
transformResponse: (res) => res
})
]
const createStoreWithMiddleware = applyMiddleware(
...middlewares
)(createStore)
export default function configureStore(initialState) {
return createStoreWithMiddleware(reducer, initialState)
}
import { ALGOLIA } from 'redux-algolia'
export const SEARCH_REQUEST = '@@algolia/SEARCH_REQUEST'
export const SEARCH_SUCCESS = '@@algolia/SEARCH_SUCCESS'
export const SEARCH_FAILURE = '@@algolia/SEARCH_FAILURE'
export const queryAlgolia = ({ query, filters }) => {
return {
[ALGOLIA]: {
indexName: 'YOUR_ALGOLIA_INDEX_NAME',
method: 'search',
options: {
query,
filters
},
requestType: SEARCH_REQUEST,
successType: SEARCH_SUCCESS,
failureType: SEARCH_FAILURE
}
}
}