You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.9 KiB
74 lines
1.9 KiB
7 years ago
|
import { createSelector } from 'reselect'
|
||
|
|
||
|
import filter from 'lodash/filter'
|
||
|
|
||
|
// Initial State
|
||
|
const initialState = {
|
||
|
isOpen: false,
|
||
7 years ago
|
searchQuery: ''
|
||
7 years ago
|
}
|
||
|
|
||
|
// Constants
|
||
|
// ------------------------------------
|
||
7 years ago
|
export const OPEN_CONTACTS_FORM = 'OPEN_CONTACTS_FORM'
|
||
|
export const CLOSE_CONTACTS_FORM = 'CLOSE_CONTACTS_FORM'
|
||
7 years ago
|
|
||
7 years ago
|
export const UPDATE_CONTACT_FORM_SEARCH_QUERY = 'UPDATE_CONTACT_FORM_SEARCH_QUERY'
|
||
7 years ago
|
|
||
|
// ------------------------------------
|
||
|
// Actions
|
||
|
// ------------------------------------
|
||
7 years ago
|
export function openContactsForm() {
|
||
7 years ago
|
return {
|
||
7 years ago
|
type: OPEN_CONTACTS_FORM
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
export function closeContactsForm() {
|
||
7 years ago
|
return {
|
||
7 years ago
|
type: CLOSE_CONTACTS_FORM
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
export function updateContactFormSearchQuery(searchQuery) {
|
||
7 years ago
|
return {
|
||
7 years ago
|
type: UPDATE_CONTACT_FORM_SEARCH_QUERY,
|
||
7 years ago
|
searchQuery
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// ------------------------------------
|
||
|
// Action Handlers
|
||
|
// ------------------------------------
|
||
|
const ACTION_HANDLERS = {
|
||
7 years ago
|
[OPEN_CONTACTS_FORM]: state => ({ ...state, isOpen: true }),
|
||
|
[CLOSE_CONTACTS_FORM]: state => ({ ...state, isOpen: false }),
|
||
7 years ago
|
|
||
7 years ago
|
[UPDATE_CONTACT_FORM_SEARCH_QUERY]: (state, { searchQuery }) => ({ ...state, searchQuery })
|
||
7 years ago
|
}
|
||
|
|
||
|
// ------------------------------------
|
||
|
// Selector
|
||
|
// ------------------------------------
|
||
7 years ago
|
const contactFormSelectors = {}
|
||
7 years ago
|
const networkNodesSelector = state => state.network.nodes
|
||
7 years ago
|
const searchQuerySelector = state => state.contactsform.searchQuery
|
||
7 years ago
|
|
||
|
|
||
7 years ago
|
contactFormSelectors.filteredNetworkNodes = createSelector(
|
||
7 years ago
|
networkNodesSelector,
|
||
|
searchQuerySelector,
|
||
|
(nodes, searchQuery) => filter(nodes, node => node.alias.includes(searchQuery) || node.pub_key.includes(searchQuery))
|
||
|
)
|
||
|
|
||
7 years ago
|
export { contactFormSelectors }
|
||
7 years ago
|
|
||
|
// ------------------------------------
|
||
|
// Reducer
|
||
|
// ------------------------------------
|
||
7 years ago
|
export default function contactFormReducer(state = initialState, action) {
|
||
7 years ago
|
const handler = ACTION_HANDLERS[action.type]
|
||
|
|
||
|
return handler ? handler(state, action) : state
|
||
|
}
|