# @cjsx React.DOM
$ = require('jquery')
React = require('react/addons')
Modal = require('react-bootstrap/lib/Modal')
Button = require('react-bootstrap/lib/Button')
TagsInput = require('react-tagsinput')
CardModel = require('../models/card')
module.exports = React.createClass
mixins: [React.addons.LinkedStateMixin]
displayName: 'Edit'
getInitialState: ()->
updating: false
title: @props.card.get('title')
content: @props.card.get('content')
tags: @props.card.get('tags') || []
errors: false
shaking: false
dontCloseDialog: false
createSuccess: false
onChangeTitle: (event)->
@setState title: event.target.value
onChangeContent: (event)->
@setState content: event.target.value
onChangeDontCloseDialog: (event)->
@setState dontCloseDialog: !@state.dontCloseDialog
onClickOk: (event)->
event.preventDefault()
@setState updating: true
if @props.card.isNew()
attributes = {title: @state.title, content: @state.content, tags: @state.tags}
else
attributes = {new_title: @state.title, content: @state.content, tags: @state.tags}
@props.card.save attributes,
success: ()=>
@setState updating: false
if @props.card.isNew()
if @state.dontCloseDialog
@setState
createSuccess: true
title: ''
content: ''
tags: []
@props.card = new CardModel()
@props.card._isNew = true
else
@props.onRequestHide()
@props.card.set 'title', @state.title
location.hash = '/' + @state.title
error: (model, response, options)=>
console.log response.responseJSON
@setState errors: response.responseJSON.card.errors
@setState updating: false
@setState shaking: true
setTimeout (=> @setState shaking: false), 300
render: ->