Skip to content

Commit

Permalink
refactor validCardTypes
Browse files Browse the repository at this point in the history
- use class getter and setter to store the validCardTypes value instead of using the component state
- refactor config function name to be more explicit about its implementation
  • Loading branch information
cassiocardoso committed May 3, 2020
1 parent d19400c commit b5f07b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
34 changes: 16 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import React from 'react';
import PropTypes from 'prop-types';

import { cardTypesMap, configure, getCardType, validateLuhn } from './utils/cardHelpers';
import { cardTypesMap, getCardType, setInitialValidCardTypes, validateLuhn } from './utils/cardHelpers';

class ReactCreditCards extends React.Component {
constructor(props) {
super(props);

const validCardTypes = configure();

this.state = {
validCardTypes,
};

this.setCards();
}

componentDidUpdate(prevProps) {
const { acceptedCards, callback, number } = this.props;

Expand All @@ -27,7 +15,7 @@ class ReactCreditCards extends React.Component {
}

if (prevProps.acceptedCards.toString() !== acceptedCards.toString()) {
this.setCards();
this.validCardTypes = acceptedCards;
}
}

Expand Down Expand Up @@ -110,13 +98,12 @@ class ReactCreditCards extends React.Component {

get options() {
const { number } = this.props;
const { validCardTypes } = this.state;
let updatedIssuer = 'unknown';

if (number) {
const validatedIssuer = getCardType(number);

if (validCardTypes.includes(validatedIssuer)) {
if (this.validCardTypes.includes(validatedIssuer)) {
updatedIssuer = validatedIssuer;
}
}
Expand All @@ -139,12 +126,23 @@ class ReactCreditCards extends React.Component {
};
}

setCards() {
get validCardTypes() {
const { acceptedCards } = this.props;
const initialValidCardTypes = setInitialValidCardTypes();

if (acceptedCards.length) {
this.setState((prevState) => ({ validCardTypes: prevState.validCardTypes.filter(card => acceptedCards.includes(card)) }));
return initialValidCardTypes.filter(card => acceptedCards.includes(card));
}

return initialValidCardTypes;
}

set validCardTypes(acceptedCards) {
if (acceptedCards.length) {
return this.validCardTypes.filter(card => acceptedCards.includes(card));
}

return this.validCardTypes;
}

render() {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/cardHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export const getCardType = (cardNumber) => {
};

/**
* Configure credit cards and return an array of valid types
* Configure the credit card types supported and return an array of valid types
* @returns {string[]} validCardTypes
*/
export const configure = () => {
export const setInitialValidCardTypes = () => {
creditCardType.updateCard(cardTypes.MAESTRO, {
patterns: [
493698,
Expand Down

0 comments on commit b5f07b4

Please sign in to comment.