# @cjsx React.DOM
@Posts = React.createClass(
propTypes:
site: React.PropTypes.object.isRequired
getInitialState: ->
{ alert: null, loading: false, posts: null }
fetchPosts: ->
return if @state.loading
@setState(loading: true)
$.get("/api/posts").always(@handleResponse).done(@handleSuccess).fail(@handleError)
handleResponse: ->
@setState(loading: false) if @isMounted()
handleSuccess: (response) ->
@setState(alert: null, posts: response.posts) if @isMounted()
handleError: (error) ->
@setState(alert: "Could not load posts: #{error.statusText} (#{error.status})") if @isMounted()
componentWillMount: ->
@fetchPosts()
$(document).on("fetchPosts", @fetchPosts)
@timer = setInterval(@fetchPosts, 5000)
componentWillUnmount: ->
$(document).off("fetchPosts", @fetchPosts)
clearInterval(@timer)
render: ->
{
{@state.alert}
if @state.alert}
{@state.posts?.map(((post) ->
).bind(this))}
)
@NewPostPartial = React.createClass(
mixins: [ReactRouter.Navigation]
propTypes:
site: React.PropTypes.object.isRequired
getInitialState: ->
{ alert: null, loading: false }
form: ->
$(@refs.form.getDOMNode())
handleSubmit: (event) ->
event.preventDefault()
return if @state.loading
@setState(loading: true)
$.post("/api/posts", @form().serialize()).always(@handleResponse).done(@handleSuccess).fail(@handleError)
handleResponse: ->
@setState(loading: false)
handleSuccess: (response) ->
@setState(alert: null)
@form()[0].reset()
$(document).trigger("fetchPosts")
@transitionTo("post_edit", post_id: response.posts.identifier)
handleError: (error) ->
@setState(alert: error.responseJSON?.errors.join(", "))
render: ->
{
{@state.alert}
if @state.alert}
)
@PostPartial = React.createClass(
mixins: [ReactRouter.Navigation]
propTypes:
site: React.PropTypes.object.isRequired
post: React.PropTypes.object.isRequired
getInitialState: ->
{ alert: null, loading: false }
panelClass: ->
if @props.post.added
"success"
else if @props.post.changed
"warning"
else if @props.post.deleted
"danger"
else
"default"
handleEdit: ->
@transitionTo("post_edit", post_id: @props.post.identifier)
handleRevert: ->
return if @state.loading
@setState(loading: true)
$.ajax(type: "PATCH", url: "/api/posts/#{@props.post.identifier}/revert").
always(@handleResponse).
done(@handleSuccess).
fail(@handleError)
handleDelete: ->
if @props.post.added
return unless confirm("Are you sure? This can't be undone")
return if @state.loading
@setState(loading: true)
$.ajax(type: "DELETE", url: "/api/posts/#{@props.post.identifier}").
always(@handleResponse).
done(@handleSuccess).
fail(@handleError)
handleRestore: ->
return if @state.loading
@setState(loading: true)
$.ajax(type: "PATCH", url: "/api/posts/#{@props.post.identifier}/restore").
always(@handleResponse).
done(@handleSuccess).
fail(@handleError)
handleResponse: ->
@setState(loading: false)
handleSuccess: (response) ->
@setState(alert: null)
$(document).trigger("fetchPosts")
handleError: (error) ->
@setState(alert: "Could not load post: #{error.statusText} (#{error.status})")
render: ->
{@props.post.title}
{moment((new Date(@props.post.date)).toISOString()).format("LLL")}
{
{@state.alert}
if @state.alert}
{if @props.post.deleted
else
{if @props.post.changed
}
}
)
@PostEdit = React.createClass(
mixins: [ReactRouter.State, ReactRouter.Navigation]
propTypes:
site: React.PropTypes.object.isRequired
getInitialState: ->
{ alert: null, success: null, loading: false, post: null }
form: ->
$(@refs.form.getDOMNode())
frontMatter: ->
@props.site.octodmin.front_matter
fetchPost: ->
return if @state.loading
@setState(loading: true)
$.get("/api/posts/#{@getParams().post_id}").always(@handleResponse).done(@handleSuccess).fail(@handleError)
handleBack: (event) ->
event.preventDefault()
@transitionTo("app")
handleSubmit: (event) ->
event.preventDefault()
return if @state.loading
@setState(loading: true)
data = @form().serializeObject()
$.ajax(type: "PATCH", url: "/api/posts/#{@state.post.identifier}", data: data).
always(@handleResponse).
done(@handleFormSuccess).
fail(@handleError)
handleResponse: ->
@setState(loading: false)
handleSuccess: (response) ->
@setState(alert: null, post: response.posts)
handleFormSuccess: (response) ->
@setState(alert: null, success: "Post is updated")
setTimeout(@removeSuccess, 5000)
@transitionTo("post_edit", post_id: response.posts.identifier)
handleError: (error) ->
@setState(alert: error.responseJSON?.errors.join(", "))
removeSuccess: ->
@setState(success: null) if @isMounted()
componentWillMount: ->
@fetchPost()
componentDidUpdate: ->
return if !@state.post || !@refs.editor
return if !/markdown$/.test(@state.post.path) && !/md$/.test(@state.post.path)
$(@refs.editor.getDOMNode()).markdown(
autofocus: false
savable: false
iconlibrary: "fa"
resize: "vertical"
fullscreen:
enable: false
)
render: ->
{{@state.alert}
if @state.alert}
{{@state.success}
if @state.success}
{if @state.post
}
)