class @FlexibilityOrder
  constructor: (@element) ->
    # pass

  url: (path) ->
    "#{ App.scenario.url_path() }/flexibility_order/#{ path }"

  update: (order) =>
    $.ajax
      url: @url('set'),
      type: 'POST',
      data:
        flexibility_order:
          order: order,
          scenario_id: App.scenario.api_session_id()
      success: ->
        App.call_api()
      error: (e,f) ->
        console.log('Throw error')

  render: =>
    $.ajax
      url: @url('get')
      type: 'GET'
      success: (data) =>
        Sortable.create @element,
          ghostClass: 'ghost'
          animation: 150
          store:
            get: (sortable) ->
              data.order

            set: (sortable) =>
              @update(sortable.toArray().concat(['curtailment']))

class @FlexibilityOrder
  constructor: (@element) ->
    @lastGood = null

  url: (path) ->
    "#{ App.scenario.url_path() }/flexibility_order/#{ path }"

  update: (sortable) =>
    options = sortable.toArray()

    $.ajax
      url: @url('set'),
      type: 'POST',
      data:
        flexibility_order:
          order: options
      success: =>
        App.call_api()
        @lastGood = options
      error: (e,f) =>
        if @lastGood
          sortable.sort(@lastGood)

  render: =>
    $.ajax
      url: @url('get')
      type: 'GET'
      success: (data) =>
        Sortable.create @element,
          ghostClass: 'ghost'
          animation: 150
          store:
            get: (_sortable) =>
              @lastGood = data.order
              data.order

            set: (sortable) =>
              @update(sortable)

Improve error handling in FlexibilityOrder
Improve error handling in FlexibilityOrder

Restores the order to the last known good settings when an error is
returned by ETEngine. The user is most likely not able to correct the
error.
