Transfer the logic from the backend

时间:2018-08-22 13:56:29

标签: reactjs react-redux ruby-on-rails-5 rails-api

I need to transfer the logic of displaying pages from the backend to the frontend, that is, I need to transfer this has_more_items, that is, that on the frontend it would be possible to receive data without has_more_items.

Ruby on rails, controller.rb

def index
  props = {
    years: params[:year],
    months: params[:month]
  }

  unless props_valid?(props)
    return render_api({ years: registers.years }, :ok)
  end

  items = registers.extract_by_date(props).by_page(params[:page]) 

  next_page_registers = registers.extract_by_date(props).by_page(params[:page].to_i)

  has_more_items = next_page_registers.empty? 

  render_api({ items: items, years: registers.years, has_more_items: has_more_items },
         :ok, each_serializer: RegistersSerializer)
end

React list.js

fetchRegistersOnScroll() {
  const { actions, current, dispatch } = this.props
  page++

  actions.fetchRegisters(current, page)
    .then(res => this.setState({ hasMoreItems: res.data.has_more_items }))
}

0 个答案:

没有答案