<commit_before>immutable = require 'app/util/immutable'
{ createSelector } = require 'reselect'
customer = require './customer'

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

schema = defineSchema 'subscription'

withNamespace = makeNamespace 'koding', 'payment', 'subscription'

LOAD = expandActionType withNamespace 'LOAD'
CREATE = expandActionType withNamespace 'CREATE'
REMOVE = expandActionType withNamespace 'REMOVE'

reducer = (state = null, action) ->

  switch action.type

    when LOAD.SUCCESS, CREATE.SUCCESS
      normalized = normalize action.result, schema
      return immutable normalized.first 'subscription'

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

      if subscription = normalized.first 'subscriptions'
        return immutable subscription

      return null

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

    else
      return state


load = ->
  return {
    types: [LOAD.BEGIN, LOAD.SUCCESS, LOAD.FAIL]
    payment: (service) -> service.fetchSubscription()
  }


create = (customerId, planId) ->
  return {
    types: [CREATE.BEGIN, CREATE.SUCCESS, CREATE.FAIL]
    payment: (service) ->
      service.createSubscription
        customer: customerId
        plan: planId
  }


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


##
# Selectors
##

plan = (state) -> state.subscription.plan

isTrial = (state) -> state.subscription.status is 'trialing'

endsAt = (state) -> state.subscription.current_period_end

pricePerSeat = (state) -> state.subscription.plan.amount / 100

trialDays = (state) -> plan(state).trial_period_days


module.exports = _.assign reducer, {
  namespace: withNamespace()
  schema
  reducer
  load, create, remove
  LOAD, CREATE, REMOVE

  # Selectors
  plan, isTrial, endsAt, pricePerSeat
}
<commit_msg>client/redux: update subscription module selectors<commit_after>immutable = require 'app/util/immutable'
{ createSelector } = require 'reselect'
dateDiffInDays = require 'app/util/dateDiffInDays'

customer = require './customer'

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

schema = defineSchema 'subscription'

withNamespace = makeNamespace 'koding', 'payment', 'subscription'

LOAD = expandActionType withNamespace 'LOAD'
CREATE = expandActionType withNamespace 'CREATE'
REMOVE = expandActionType withNamespace 'REMOVE'

reducer = (state = null, action) ->

  switch action.type

    when LOAD.SUCCESS, CREATE.SUCCESS
      normalized = normalize action.result, schema
      return immutable normalized.first 'subscription'

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

      if subscription = normalized.first 'subscriptions'
        return immutable subscription

      return null

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

    else
      return state


load = ->
  return {
    types: [LOAD.BEGIN, LOAD.SUCCESS, LOAD.FAIL]
    payment: (service) -> service.fetchSubscription()
  }


create = (customerId, planId) ->
  return {
    types: [CREATE.BEGIN, CREATE.SUCCESS, CREATE.FAIL]
    payment: (service) ->
      service.createSubscription
        customer: customerId
        plan: planId
  }


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


##
# Selectors
##

plan = (state) -> state.subscription?.plan

isTrialing = (state) -> state.subscription?.status is 'trialing'

hasNoCard = (state) -> not state.creditCard

isTrial = createSelector(
  isTrialing
  hasNoCard
  (trialing, noCard) -> noCard and trialing
)


endsAt = (state) ->
  if sub = state.subscription
  then Number(sub.current_period_end) * 1000
  else Date.now()

pricePerSeat = (state) ->

  if p = plan(state) then p.amount / 100 else 0

trialDays = (state) -> plan(state)?.trial_period_days

daysLeft = createSelector(
  endsAt
  (end) -> dateDiffInDays(new Date(Number end), new Date)
)


module.exports = _.assign reducer, {
  namespace: withNamespace()
  schema
  reducer
  load, create, remove
  LOAD, CREATE, REMOVE

  # Selectors
  plan, isTrial, endsAt, pricePerSeat, trialDays, daysLeft
}

{'fraction_non_alphanumeric': 0.10651685393258427, 'fraction_numerical': 0.0024719101123595504, 'mean_word_length': 3.71005291005291, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 30, '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': '2765088', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1321, 'n_words': 338}