2017-12-24 22 views
1
import { 
    SEARCH_CHAT_FROM_RECENT_CHAT_CONTAT_LIST, 
    GET_RECENT_CHAT_CONTAT_LIST_REQUEST, 
    GET_RECENT_CHAT_CONTAT_LIST_SUCCESS, 
    GET_RECENT_CHAT_CONTAT_LIST_FAILURE 
} from "../actions/action-types"; 

const INTIAL_STATE = { 
    response: null, 
    error: null, 
    loading: false, 
    searchResults: null, 
}; 

searchChatFromRecentChatContactList = (state, text) => { 
    if(state.response && state.response.length > 0) { 
     const response = [...state.response]; 
     const searchResults = response.filter(item => item.displayName.includes(text)); 
     return searchResults; 
    } 
    return []; 
} 

export default (state = INTIAL_STATE, action) => { 
    switch (action.type) { 
    case GET_RECENT_CHAT_CONTAT_LIST_REQUEST: 
     return { ...state, loading: true, response: null, error: null, }; 
    case GET_RECENT_CHAT_CONTAT_LIST_SUCCESS: 
     return { ...state, response: action.payload, loading: false}; 
    case GET_RECENT_CHAT_CONTAT_LIST_FAILURE: 
     return { ...state, response: null, error: action.payload, loading: false }; 
     case SEARCH_CHAT_FROM_RECENT_CHAT_CONTAT_LIST: 
      return {...state, searchResults: searchChatFromRecentChatContactList(state, action.payload)}; 
    default: 
     return state; 
    } 
}; 

state.response이 아니라 내 아래의 방법은 항상 [] 반환 어떤 이유로 문자열의 배열을 가지고;Array.prototype.filter()는 출력을 예상 제공되지

state.response = [{displayName: 'someText'}, {displayName: 'someText otherText']; 

입력 :

searchChatFromRecentChatContactList(state, 'SomeText') 

출력 :

[]; 
+1

새로 만든 때문에 Array.prototype.filter의 특성 여기 반환 필요한 당신이 전달하는 텍스트의 값을 확인하지 않습니다 searchChatFromRecentChatContactList 함수 – stack26

+0

감사합니다 @ stack26 :-) –

답변

0

내가했던 어리석은 실수 :(

searchChatFromRecentChatContactList = (state, text) => { 
    if(state.response && state.response.length > 0) { 
     const searchText = text.toLowerCase(); 
     const response = [...state.response]; 
     const searchResults = response.filter(item => { 
      if(item.displayName.includes(searchText)) { 
       return true; 
      } else { 
       return false; 
      } 
     }); 
     return searchResults; 
    } 
    return []; 
} 

text.toLowerCase(); //I should have done this. :-)

1

당신은 아직도 그것을 향상시킬 수 파괴은 배열

searchChatFromRecentChatContactList = (state, text) => { 
     const searchText = text.toLowerCase(); 
     return state.response && state.response.length ? 
     state.response.filter(item => item.displayName.includes(searchText)) : []; 
    } 
+0

당신은 바로 감사합니다. –

+0

반갑습니다. –