Skip to content

Commit

Permalink
[購買頁面] 信用卡圖示 (#1035)
Browse files Browse the repository at this point in the history
* Add credit cards

* Add opacity to active card types

* useCallback

* Highlight all for unknown card types

* alt card type
  • Loading branch information
peteranny authored Dec 14, 2022
1 parent 4e327bb commit facab47
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/components/Buy/PaymentSection/CreditCards/amex.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions src/components/Buy/PaymentSection/CreditCards/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import PropTypes from 'prop-types';
import cn from 'classnames';
import { keys } from 'ramda';
import Amex from './amex.svg';
import Jcb from './jcb.svg';
import MasterCard from './mastercard.svg';
import UnionPay from './union_pay.8271208.png';
import Visa from './visa.svg';
import styles from './styles.module.css';

const cardTypeSrc = {
visa: Visa,
mastercard: MasterCard,
jcb: Jcb,
amex: Amex,
unionpay: UnionPay,
};

const cardTypes = keys(cardTypeSrc);

const isUnknownCardType = cardType => cardTypes.includes(cardType) === false;

const CreditCards = ({ activeCardType }) => (
<React.Fragment>
{cardTypes.map(cardType => (
<img
key={cardType}
className={cn(styles.creditCard, {
[styles.active]:
activeCardType === cardType || isUnknownCardType(activeCardType),
})}
src={cardTypeSrc[cardType]}
alt={cardType}
/>
))}
</React.Fragment>
);

CreditCards.propTypes = {
activeCardType: PropTypes.string,
};

export default CreditCards;
15 changes: 15 additions & 0 deletions src/components/Buy/PaymentSection/CreditCards/jcb.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/components/Buy/PaymentSection/CreditCards/mastercard.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions src/components/Buy/PaymentSection/CreditCards/styles.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.creditCard {
width: auto;
height: 22px;

& + .creditCard {
margin-left: 8px;
}

opacity: 0.3;
transition: opacity 0.3s;

&.active {
opacity: 1;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/components/Buy/PaymentSection/CreditCards/visa.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/components/Buy/PaymentSection/PaymentSection.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@
font-size: 13px;
}
}

.cardNumberGroup {
position: relative;

.cardIcons {
display: flex;
align-items: center;
position: absolute;
height: 100%;
left: 100%;
margin-left: 8.75px;
}
}
22 changes: 17 additions & 5 deletions src/components/Buy/PaymentSection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ import Label from 'common/form/Label';
import useTappay from 'hooks/tappay/useTappay';
import { CardCCV, CardExpirationDate, CardNumber } from './TappayElement';
import Row from './Row';
import CreditCards from './CreditCards';
import styles from './PaymentSection.module.css';

const PaymentSection = ({ ...props }) => {
const [isPrimary, setPrimary] = useState(false);
const [activeCardType, setActiveCardType] = useState('unknown');
const handleUpdate = useCallback(update => {
setActiveCardType(update.cardType);
}, []);
const handlePrime = useCallback(prime => {
alert('get prime 成功,prime: ' + prime);
alert('creditCard', { prime });
}, []);
const submit = useTappay({
handlePrime: prime => {
alert('get prime 成功,prime: ' + prime);
alert('creditCard', { prime });
},
handleUpdate,
handlePrime,
});
const onSubmit = useCallback(
e => {
Expand All @@ -37,7 +44,12 @@ const PaymentSection = ({ ...props }) => {
<Label className={styles.label} isRequired>
卡號
</Label>
<CardNumber />
<div className={styles.cardNumberGroup}>
<div className={styles.cardIcons}>
<CreditCards activeCardType={activeCardType} />
</div>
<CardNumber />
</div>
</Row>
<Row half>
<Label className={styles.label} isRequired>
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/tappay/useTappay.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const styles = {
},
};

const useTappay = ({ handlePrime }) => {
const useTappay = ({ handleUpdate, handlePrime }) => {
const [tappaySDKLoaded, setTappySDKLoad] = useState(false);
const [directCardSetup, setDirectCardSetup] = useState(false);

Expand Down Expand Up @@ -73,10 +73,10 @@ const useTappay = ({ handlePrime }) => {
if (tappaySDKLoaded) {
TPDirect.card.onUpdate(update => {
// 即時反應每個行為
console.log(update);
handleUpdate(update);
});
}
}, [tappaySDKLoaded]);
}, [handleUpdate, tappaySDKLoaded]);

// 送出 Tappay 表單
const submit = useCallback(() => {
Expand Down

0 comments on commit facab47

Please sign in to comment.