<commit_before>_ = require 'lodash'
immutable = require 'app/util/immutable'

{ makeNamespace, expandActionType,
  normalize, defineSchema } = require 'app/redux/helper'

withNamespace = makeNamespace 'koding', 'payment', 'creditcard'

REMOVE = expandActionType withNamespace 'REMOVE'

customer = require './customer'

reducer = (state = null, action) ->

  switch action.type

    when customer.LOAD.SUCCESS, customer.CREATE.SUCCESS, customer.UPDATE.SUCCESS
      normalized = normalize action.result, customer.schema
      c = normalized.first 'customer'

      card = normalized.entities.sources[c.default_source]
      return immutable card

    when REMOVE.SUCCESS, customer.REMOVE.SUCCESS
      return null

    else
      return state


remove = ->
  return {
    types: [ REMOVE.BEGIN, REMOVE.SUCCESS, REMOVE.FAIL ]
    payment: (service) -> service.deleteCreditCard()
  }


module.exports = _.assign reducer, {
  namespace: withNamespace()
  reducer
  remove
  REMOVE
}

<commit_msg>client/redux: check cc existence before saving it<commit_after>_ = require 'lodash'
immutable = require 'app/util/immutable'

{ makeNamespace, expandActionType,
  normalize, defineSchema } = require 'app/redux/helper'

withNamespace = makeNamespace 'koding', 'payment', 'creditcard'

REMOVE = expandActionType withNamespace 'REMOVE'

customer = require './customer'

reducer = (state = null, action) ->

  switch action.type

    when customer.LOAD.SUCCESS, customer.CREATE.SUCCESS, customer.UPDATE.SUCCESS
      normalized = normalize action.result, customer.schema
      c = normalized.first 'customer'

      if c.default_source
        return normalized.entities.sources[c.default_source]

      return null

    when REMOVE.SUCCESS, customer.REMOVE.SUCCESS
      return null

    else
      return state


remove = ->
  return {
    types: [ REMOVE.BEGIN, REMOVE.SUCCESS, REMOVE.FAIL ]
    payment: (service) -> service.deleteCreditCard()
  }


module.exports = _.assign reducer, {
  namespace: withNamespace()
  reducer
  remove
  REMOVE
}


{'fraction_non_alphanumeric': 0.10102991662579695, 'fraction_numerical': 0.0, 'mean_word_length': 3.5945945945945947, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6956383', 'n_tokens_mistral': 609, 'n_tokens_neox': 590, 'n_words': 149}