/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2019
 */

import currency from 'currency.js';

import { ROUTES } from '../navigation';
import { PAYMENT_ACTIONS } from '../actions/PaymentActions';
import { WIZARD_ACTIONS } from '../actions/WizardActions';
import { selectPrescriptionTotal } from '../selectors/payment';

const initialState = () => ({
  transaction: null,
  patient: null,
  paymentAmount: currency(0),
  policy: null,
  paymentValid: false,
});

export const PaymentReducer = (state = initialState(), action) => {
  const { type } = action;

  switch (type) {
    case 'Navigation/NAVIGATE': {
      const { routeName, params } = action;

      if (routeName !== ROUTES.PRESCRIPTION) return state;
      const { transaction } = params;
      return { ...state, transaction };
    }

    case WIZARD_ACTIONS.NEXT_TAB: {
      const { transaction } = state;
      if (!transaction) return state;
      const paymentAmount = selectPrescriptionTotal({ payment: state });
      return { ...state, paymentAmount };
    }

    case PAYMENT_ACTIONS.CHOOSE_POLICY: {
      const { payload } = action;
      const { policy } = payload;

      return { ...state, policy };
    }

    case PAYMENT_ACTIONS.CHOOSE_PAYMENT_TYPE: {
      const { payload } = action;
      const { paymentType } = payload;

      return { ...state, paymentType };
    }

    case PAYMENT_ACTIONS.UPDATE_PAYMENT: {
      const { payload } = action;
      const { amount } = payload;

      return { ...state, paymentAmount: amount, creditOverflow: false };
    }

    case PAYMENT_ACTIONS.CREDIT_OVERFLOW: {
      return { ...state, creditOverflow: true };
    }

    default:
      return state;
  }
};

@@ -452,20 +452,19 @@
 tValid: 
-fals
+tru
 e,%0A%7D);%0A%0A
@@ -1536,16 +1536,36 @@
 w: false
+, paymentValid: true
  %7D;%0A    
@@ -1652,24 +1652,45 @@
 erflow: true
+, paymentValid: false
  %7D;%0A    %7D%0A%0A 

Add use of validPayment field