Skip to content

Instantly share code, notes, and snippets.

@jacek213
Created June 6, 2017 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacek213/ca3b4fdd1c0651e847e61a2bfacf6a21 to your computer and use it in GitHub Desktop.
Save jacek213/ca3b4fdd1c0651e847e61a2bfacf6a21 to your computer and use it in GitHub Desktop.
# @cjsx React.DOM
@ArticlesSection = React.createClass
displayName: 'ArticlesSection'
getInitialState: ->
didFetchData: false
articles: []
meta:
total_pages: 0
current_page: 1
total_count: 0
fetchParams:
search: {}
page: 1
componentWillMount: ->
@_fetchFilterable()
componentDidMount: ->
@_fetchArticles()
_fetchArticles: ->
$.ajax
url: 'api/v1/articles'
dataType: 'json'
data: @state.fetchParams
.done @_fetchArticlesDone
.fail @_fetchDataFail
_fetchArticlesDone: (data, textStatus, jqXHR) ->
return false unless @isMounted()
@setState
didFetchData: true
articles: data.articles
meta: data.meta
_fetchFilterable: ->
$.ajax
url: 'api/v1/articles/filterable'
dataType: 'json'
.done @_fetchFilterableDone
.fail @_fetchDataFail
_fetchFilterableDone: (data, textStatus, jqXHR) ->
@authors = data.authors
@keywords = data.keywords
@statuses = data.statuses
_fetchDataFail: (xhr, status, err) =>
console.error status, err.toString()
_handleOnSearchSubmit: (search) ->
@state.fetchParams =
search: search
page: 1
@_fetchArticles()
_handleResetForm: () ->
@state = @getInitialState()
@_fetchArticles()
_handleOnPaginate: (pageNumber) ->
@state.fetchParams.page = pageNumber
@_fetchArticles()
render: ->
<div id="list">
<ArticlesSearch onFormReset={@_handleResetForm} onFormSubmit={@_handleOnSearchSubmit} keywords={@keywords} authors={@authors} statuses={@statuses} />
{
if @state.articles.length
<span>
<ArticlesList data={@state.articles}/>
<PaginatorSection totalPages={@state.meta.total_pages} currentPage={@state.meta.current_page} onPaginate={@_handleOnPaginate} />
</span>
else if @state.didFetchData
<strong>
<i>No records matching criteria found.</i>
</strong>
}
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment