decoupled search type/value reducers

master
Malar Kannan 2017-07-12 18:22:15 +05:30
parent a8afb53407
commit b3087c73cd
6 changed files with 54 additions and 40 deletions

View File

@ -2,7 +2,7 @@ import * as React from 'react';
import { bindActionCreators } from 'redux';
import { Provider, connect } from 'react-redux';
import { LexEditor } from './LexComponents';
import * as actionCreators from './actions/actionCreators';
import * as actionCreators from './actionCreators';
import { walleStore } from './WallEStore';
import { Header, Icon, Segment } from 'semantic-ui-react';

View File

@ -84,12 +84,14 @@ export class LexEditor extends React.Component<any, any> {
return (
<div>
<LexSearch
{...this.props}
{...{ fieldMetaMap: this.fieldMetaMap }}
handleOnSearch={(e: any) => this.handleOnSearch(e)}
searchValue={searchText}
searchType={this.props.searchProp.searchType}
// searchValue={searchText}
// searchType={this.props.searchProp.searchType}
/>
<LexMatches
{...this.props}
{...{ fieldMetaMap: this.fieldMetaMap }}
matchedEntries={matchedEntries}
selectionMeta={this.selectFields}
@ -116,8 +118,6 @@ export class LexEditor extends React.Component<any, any> {
// export const ReduxLexEditor = connect()(LexEditor);
class LexSearch extends React.Component<any, any> {
searchType: String = 'label';
searchValue: String = '';
public render() {
let dropOptions = _.keys(this.props.fieldMetaMap).map((k, i, c) => {
return { key: i, value: k, text: _.capitalize(k) };
@ -127,7 +127,7 @@ class LexSearch extends React.Component<any, any> {
<Dropdown
options={dropOptions}
onChange={(e, d) => this.handleChange(d, true)}
value={this.props.searchType}
value={this.props.searchProp.searchType}
compact={true}
selection={true}
/>
@ -135,7 +135,7 @@ class LexSearch extends React.Component<any, any> {
type="text"
placeholder="Search input"
dir="auto"
value={this.props.searchValue}
value={this.props.searchProp.searchValue}
onChange={(e, d) => this.handleChange(d, false)}
/>
</div>
@ -144,14 +144,10 @@ class LexSearch extends React.Component<any, any> {
private handleChange(e: any, t: boolean) {
if (t) {
this.searchType = e.value;
this.props.searchType(e.value);
} else {
this.searchValue = e.value;
this.props.searchValue(e.value);
}
this.props.handleOnSearch({
searchType: this.searchType,
searchValue: this.searchValue
});
}
}

View File

@ -1,5 +1,5 @@
import { createStore } from 'redux';
import rootReducer from './reducers/index';
import rootReducer from './reducers';
const defaultState = {
search: { searchValue: 'just', searchType: 'label' }

View File

@ -7,6 +7,20 @@ export function search(searchValue: any, searchType: any) {
};
}
export function searchType(searchType: any) {
return {
type: 'ACTION_SEARCH_TYPE_CHANGE',
searchType
};
}
export function searchValue(searchValue: any) {
return {
type: 'ACTION_SEARCH_VALUE_CHANGE',
searchValue
};
}
export function save(lexItem: any) {
return {
type: 'ACTION_SAVE_LEXITEM',

30
src/reducers.tsx Normal file
View File

@ -0,0 +1,30 @@
import { combineReducers } from 'redux';
function searchValue(state: any = {}, action: any) {
if (action.type === 'ACTION_SEARCH_VALUE_CHANGE') {
return action.searchValue;
}
return state;
}
function searchType(state: any = {}, action: any) {
if (action.type === 'ACTION_SEARCH_TYPE_CHANGE') {
return action.searchType;
}
return state;
}
function save(state: any = {}, action: any) {
if (action.type === 'ACTION_SAVE_LEXITEM') {
return {
...state,
lexItem: action.lexItem,
};
}
return state;
}
const searchReducer = combineReducers({ searchValue, searchType });
const rootReducer = combineReducers({ search: searchReducer, save });
export default rootReducer;

View File

@ -1,26 +0,0 @@
import { combineReducers } from 'redux';
function search(state: any = {}, action: any) {
if (action.type === 'ACTION_SEARCH_TEXT') {
return {
...state,
searchValue: action.searchValue,
searchType: action.searchType
};
}
return state;
}
function save(state: any = {}, action: any) {
if (action.type === 'ACTION_SAVE_LEXITEM') {
return {
...state,
lexItem: action.lexItem,
};
}
return state;
}
const rootReducer = combineReducers({ search, save });
export default rootReducer;