Search code examples
reactjsreduxmomentjsreact-dates

My react-dates DateRangePicker is not working/rendering when clicked


I'm building a react redux expense tracking app and use moment.js and react-dates to set time and filter time range. However I am getting the error when I select anywhere, or the "x" to clear date, then nothing renders to screen.

expenses.js:9 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
    at expenses.js:9
    at Array.filter (<anonymous>)
    at expenses.js:5
    at Function.mapStateToProps [as mapToProps] (ExpenseList.js:17)
    at mapToPropsProxy (wrapMapToProps.js:41)
    at handleNewState (selectorFactory.js:44)
    at handleSubsequentCalls (selectorFactory.js:58)
    at pureFinalPropsSelector (selectorFactory.js:63)
    at Subscription.checkForUpdates [as onStateChange] (connectAdvanced.js:247)
    at Subscription.handleChangeWrapper (Subscription.js:70)

I have double checked and still couldn't find what's wrong...

reducers/filters:

const filterReducerDefaultState = {
    text: '',
    sortBy: 'date',
    startDate: moment().startOf('month'),
    endDate: moment().endOf('month')
}

Here is the selectors/expenses.js:

import moment from 'moment'

//Get visible expenses
export default (expenses, { text, sortBy, startDate, endDate }) => {
    return expenses.filter((expense) => {
        const createdAtMoment = moment(expense.createdAt)
        const startDateMatch = startDate ? startDate.isSameOrBefore(createdAtMoment, 'day') : true
        const endDateMatch = endDate? endDate.isSameOrAfter(createdAtMoment, 'day'): true
        const textMatch = expense.description.toLowerCase().includes(text.toLowerCase())

        return startDateMatch && endDateMatch && textMatch
    }).sort((a, b) => {
        if (sortBy === 'date') {
            return a.createdAt < b.createdAt ? 1 : -1
        } else if (sortBy === 'amount') {
            return a.amount < b.amount ?  1 : -1
        }
    })
}

Here is the components/ExpenseListFilters.js:

import React from 'react'
import { connect } from 'react-redux'
import { DateRangePicker } from 'react-dates'
import { setTextFilter, sortByAmount, sortByDate, setStartDate, setEndDate } from '../actions/filters'

class ExpenseListFilters extends React.Component{
    state={
        calendarFocused: null
    }
    onDatesChange = ({startDate, endDate}) => {
        this.props.dispatch(setStartDate(startDate))
        this.props.dispatch(setEndDate(endDate))
    }
    onFocusChange = (calendarFocused) => {
        this.setState(() => ({ calendarFocused}))
    }
    render(){
        return (
            <div>
        <input 
        type="text" 
        value={this.props.filters.text} 
        onChange={(e) => {
            this.props.dispatch(setTextFilter(e.target.value))
        }} />
        <select 
        value ={this.props.filters.sortBy}
        onChange={(e)=> {
            if (e.target.value === 'date'){
                this.props.dispatch(sortByDate())
            } else if (e.target.value === 'amount') {
                this.props.dispatch(sortByAmount())
            }
        }}>
            <option value="date">Date</option>
            <option value="amount">Amount</option>
        </select>
        <DateRangePicker 
        startDate={this.props.filters.startDate}
        startDateId='startDateId'
        endDate={this.props.filters.endDate}
        endDateId='endDateId'
        onDatesChange={this.onDatesChange}
        focusedInput={this.state.calendarFocused}
        onFocusChange={this.onFocusChange}
        showClearDates={true}
        numberOfMonths={1}
        isOutsideRange={()=> false}
        />
    </div>
        )
    }
}

const mapStateToProps = (state) => {
    return {
        filters: state.filters
    }
}
export default connect(mapStateToProps)(ExpenseListFilters)

I expect to be able to clear the date by clicking "X" on calender so my demo data can show up, or select certain range to filter. How can I fix that?


Solution

  • Hi I solved my problem now: I forget to include ...state in my reducers/filters in the :

            case 'SET_START_DATE':
                return {
                    startDate: action.startDate
                }
            case 'SET_END_DATE':
                return {
                    endDate: action.endDate
                }
    

    and it should be:

            case 'SET_START_DATE':
                return {
                    ...state,
                    startDate: action.startDate
                }
            case 'SET_END_DATE':
                return {
                    ...state,
                    endDate: action.endDate
                }