From a8afb53407ea784f4aa95d9d2757ba30b503b369 Mon Sep 17 00:00:00 2001 From: Malar Kannan Date: Wed, 12 Jul 2017 17:36:09 +0530 Subject: [PATCH] 1. search text/type is now redux store state --- src/App.tsx | 4 +--- src/LexComponents.tsx | 23 +++++++++++++++++------ src/LexEdit.tsx | 8 ++++---- src/WallEStore.tsx | 2 +- src/actions/actionCreators.tsx | 12 ++++++++++-- src/reducers/index.tsx | 20 +++++++++++++++++--- 6 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 92b180c..a79941b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,9 +7,7 @@ import { walleStore } from './WallEStore'; import { Header, Icon, Segment } from 'semantic-ui-react'; function mapStateToProps(state: any) { - return { - lexicon: state.lexicon - }; + return { searchProp: state.search, lexicon: state.lexicon }; } function mapDispachToProps(dispatch: any) { diff --git a/src/LexComponents.tsx b/src/LexComponents.tsx index 2a4bb06..70bcf52 100644 --- a/src/LexComponents.tsx +++ b/src/LexComponents.tsx @@ -66,6 +66,7 @@ export class LexEditor extends React.Component { })); return [s, selectOptions]; })); + this.forceUpdate(); }); }) .catch((e) => { @@ -74,18 +75,26 @@ export class LexEditor extends React.Component { } public render() { + let searchLens = this.fieldMetaMap[this.props.searchProp.searchType].lens; + let searchText = this.props.searchProp.searchValue; + const matchedEntries = _.chain(this.allEntries) + .filter((q: any) => _.get(q, searchLens, '') === searchText) + .take(10) + .value(); return (
this.handleOnSearch(e)} + searchValue={searchText} + searchType={this.props.searchProp.searchType} />
); @@ -93,12 +102,13 @@ export class LexEditor extends React.Component { private handleOnSearch(searchEvent: any): void { let searchText = searchEvent.searchValue; + let searchType = searchEvent.searchType; let searchLens = this.fieldMetaMap[searchEvent.searchType].lens; let matchedEntries = _.chain(this.allEntries) .filter((q: any) => _.get(q, searchLens, '') === searchText) .take(10) .value(); - this.props.search(searchText); + this.props.search(searchText, searchType); this.setState({ ...this.state, matchedEntries, searchText, searchLens }); } } @@ -117,7 +127,7 @@ class LexSearch extends React.Component { this.handleChange(d, true)} - defaultValue={dropOptions[0].value} + value={this.props.searchType} compact={true} selection={true} /> @@ -125,6 +135,7 @@ class LexSearch extends React.Component { type="text" placeholder="Search input" dir="auto" + value={this.props.searchValue} onChange={(e, d) => this.handleChange(d, false)} /> diff --git a/src/LexEdit.tsx b/src/LexEdit.tsx index 2ac2e04..cb1db58 100644 --- a/src/LexEdit.tsx +++ b/src/LexEdit.tsx @@ -15,14 +15,13 @@ export class LexEdit extends React.Component { public render() { let li = this.props.lexItem; let lexFields = _.keys(this.props.fieldMetaMap).map(field => { - let defaultText = _.get(li, this.props.fieldMetaMap[field].lens, ''); + let lens = this.props.fieldMetaMap[field].lens; + let defaultText = _.get(li, lens, ''); let sh = (e: any) => { let eventData = {}; eventData[field] = e.target.value; this.handleOnChange(eventData); }; - // let pred = (x: any) => _.isEqual(fieldMetaMap[ft].type, x); - // _.findIndex(['text', 'number'], pred) !== -1 if (this.props.fieldMetaMap[field].type === 'text') { return ( @@ -37,6 +36,7 @@ export class LexEdit extends React.Component { ); } else if (this.props.fieldMetaMap[field].type === 'select') { + let selOpts = _.get(this.props.selectionMeta, field, []); return ( diff --git a/src/WallEStore.tsx b/src/WallEStore.tsx index 667fa2a..b35b2dc 100644 --- a/src/WallEStore.tsx +++ b/src/WallEStore.tsx @@ -2,7 +2,7 @@ import { createStore } from 'redux'; import rootReducer from './reducers/index'; const defaultState = { - search: 'world' + search: { searchValue: 'just', searchType: 'label' } }; const devToolsKey = '__REDUX_DEVTOOLS_EXTENSION__'; diff --git a/src/actions/actionCreators.tsx b/src/actions/actionCreators.tsx index 16b9bde..a5e3df1 100644 --- a/src/actions/actionCreators.tsx +++ b/src/actions/actionCreators.tsx @@ -1,7 +1,15 @@ -export function search(searchKey: any) { +export function search(searchValue: any, searchType: any) { return { type: 'ACTION_SEARCH_TEXT', - searchKey + searchType, + searchValue + }; +} + +export function save(lexItem: any) { + return { + type: 'ACTION_SAVE_LEXITEM', + lexItem, }; } diff --git a/src/reducers/index.tsx b/src/reducers/index.tsx index 2b6fe60..42fb2a5 100644 --- a/src/reducers/index.tsx +++ b/src/reducers/index.tsx @@ -1,12 +1,26 @@ import { combineReducers } from 'redux'; -function search(state: any = [], action: any) { +function search(state: any = {}, action: any) { if (action.type === 'ACTION_SEARCH_TEXT') { - console.log(state, action); + return { + ...state, + searchValue: action.searchValue, + searchType: action.searchType + }; } return state; } -const rootReducer = combineReducers({ search }); +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;