File size: 3,087 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import { Button } from '@automattic/composite-checkout';
import { Field, styled, joinClasses } from '@automattic/wpcom-checkout';
import { keyframes } from '@emotion/react';
import { useTranslate } from 'i18n-calypso';
import PropTypes from 'prop-types';
import type { CouponFieldStateProps } from '../hooks/use-coupon-field-state';
import type { CouponStatus } from '@automattic/shopping-cart';
/* eslint-disable @typescript-eslint/no-use-before-define */
export default function Coupon( {
id,
className,
disabled,
couponStatus,
couponFieldStateProps,
}: {
id: string;
className?: string;
disabled?: boolean;
couponStatus: CouponStatus;
couponFieldStateProps: CouponFieldStateProps;
} ) {
const translate = useTranslate();
const {
couponFieldValue,
setCouponFieldValue,
isApplyButtonActive,
isFreshOrEdited,
setIsFreshOrEdited,
handleCouponSubmit,
} = couponFieldStateProps;
const hasCouponError = couponStatus === 'rejected';
const isPending = couponStatus === 'pending';
const errorMessage = getCouponErrorMessageFromStatus( translate, couponStatus, isFreshOrEdited );
return (
<CouponWrapper
className={ joinClasses( [ className, 'coupon' ] ) }
onSubmit={ ( event ) => {
event.preventDefault();
setIsFreshOrEdited( false );
handleCouponSubmit();
} }
>
<Field
id={ id }
inputClassName="coupon-code"
value={ couponFieldValue }
disabled={ disabled || isPending }
placeholder={ String( translate( 'Enter your coupon code' ) ) }
isError={ hasCouponError && ! isFreshOrEdited }
errorMessage={ errorMessage }
onChange={ ( input ) => {
setIsFreshOrEdited( true );
setCouponFieldValue( input );
} }
/>
{ isApplyButtonActive && (
<ApplyButton disabled={ isPending } buttonType="secondary">
{ isPending ? translate( 'Processing…' ) : translate( 'Apply' ) }
</ApplyButton>
) }
</CouponWrapper>
);
}
Coupon.propTypes = {
id: PropTypes.string.isRequired,
couponAdded: PropTypes.func,
disabled: PropTypes.bool,
};
const animateIn = keyframes`
from {
opacity: 0;
transform: translateX(8px);
}
to {
opacity: 1;
transform: translateY(0);
}
`;
const animateInRTL = keyframes`
from {
opacity: 0;
transform: translateX(-8px);
}
to {
opacity: 1;
transform: translateY(0);
}
`;
const CouponWrapper = styled.form`
margin: 0;
padding-top: 0;
position: relative;
& input {
font-size: 14px;
}
`;
const ApplyButton = styled( Button )`
animation: ${ animateIn } 0.2s ease-out;
animation-fill-mode: backwards;
margin: 0;
position: absolute;
font-size: 14px;
padding: 8px;
top: 2px;
right: 2px;
.rtl & {
animation-name: ${ animateInRTL };
left: 4px;
right: auto;
}
`;
function getCouponErrorMessageFromStatus(
translate: ReturnType< typeof useTranslate >,
status: CouponStatus,
isFreshOrEdited: boolean
): string | undefined {
if ( status === 'rejected' && ! isFreshOrEdited ) {
return translate( 'There was a problem applying this coupon.', { textOnly: true } );
}
return undefined;
}
|