1 import type { RefObject } from 'react';
2 import { useEffect, useState } from 'react';
4 import { c } from 'ttag';
6 import { Button, Input } from '@proton/atoms';
7 import UnderlineButton from '@proton/components/components/button/UnderlineButton';
8 import Icon from '@proton/components/components/icon/Icon';
9 import Info from '@proton/components/components/link/Info';
10 import useToggle from '@proton/components/hooks/useToggle';
15 onApply: (value: string) => void;
16 giftCodeRef: RefObject<HTMLInputElement>;
19 const getFormattedGiftCode = (giftCode: string) => {
20 const splittedGiftCode = giftCode.replace(/-/g, '').match(/.{1,4}/g) || [''];
21 return splittedGiftCode.join('-').toUpperCase();
24 const PaymentGiftCode = ({ giftCodeRef, giftCode = '', onApply, loading }: Props) => {
25 const { state, toggle, set } = useToggle();
26 const [code, setCode] = useState('');
28 const handleCancel = () => {
34 // When we remove the gift code
42 <div className="flex items-center">
43 <span className="flex-1 flex-nowrap items-center">
44 <Icon name="gift" className="mr-2 mb-1 shrink-0" />
45 <code>{getFormattedGiftCode(giftCode)}</code>
50 className="flex items-center ml-1"
51 onClick={() => onApply('')}
52 title={c('Action').t`Remove gift code`}
54 <Icon name="trash" alt={c('Action').t`Remove gift code`} />
61 const handleSubmit = () => {
69 <div className="flex flex-nowrap items-center items-start mb-2">
70 <div className="pr-2 flex-1">
74 placeholder={c('Placeholder').t`Gift code`}
77 data-testid="gift-code-input"
78 onKeyDown={(event) => {
79 if (event.key === 'Enter') {
80 event.preventDefault();
88 title={c('Title').t`Apply gift code`}
91 onClick={handleSubmit}
92 data-testid="apply-gift-code"
93 >{c('Action').t`Apply`}</Button>
100 <UnderlineButton className="mr-2" onClick={toggle} data-testid="add-gift-code">{c('Link')
101 .t`Add a gift code`}</UnderlineButton>
104 .t`If you purchased a gift code or received one from our support team, you can enter it here.`}
110 export default PaymentGiftCode;