All files / app/termsConditions TermsConditionsField.tsx

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 139 140 141 142  4x   4x 4x 4x 4x 4x   4x 4x 4x 4x                                           4x 1x 1x 1x   1x 1x   1x                                 1x               4x               4x 4x 4x   4x         4x               4x 2x 2x   2x               2x               4x 5x   5x                                   5x       5x     4x  
import { FieldProps } from 'formik';
import React, { memo, useCallback, useMemo, FunctionComponent } from 'react';
 
import { parseAnchor } from '../common/utility';
import { withLanguage, TranslatedHtml, TranslatedString, WithLanguageProps } from '../locale';
import { CheckboxFormField, Fieldset, FormField, Legend, TextArea } from '../ui/form';
import { ModalHeader, ModalLink } from '../ui/modal';
import { MultiLineText } from '../ui/text';
 
export enum TermsConditionsType {
    Link = 'link',
    TextArea = 'textarea',
    Modal = 'modal',
}
 
export type TermsConditionsFieldProps = TermsConditionsLinkFieldProps | TermsConditionsTextAreaFieldProps;
 
interface TermsConditionsLinkFieldProps {
    name: string;
    type: TermsConditionsType.Link;
    url: string;
}
 
interface TermsConditionsTextAreaFieldProps {
    name: string;
    terms: string;
    type: TermsConditionsType.TextArea | TermsConditionsType.Modal;
}
 
interface TermsConditionsTextFieldProps {
    name: string;
    terms: string;
}
 
const BaseTermsConditionsModalCheckboxField: FunctionComponent<TermsConditionsTextFieldProps & WithLanguageProps> = ({
    language,
    name,
    terms,
}) => {
    const translatedLabel = language.translate('terms_and_conditions.agreement_with_link_text', { url: '' });
    const parsedLabel = parseAnchor(translatedLabel);
 
    const labelContent = parsedLabel ?
        (<>
            { parsedLabel[0] }
            <ModalLink
                body={ <MultiLineText>{ terms }</MultiLineText> }
                header={
                    <ModalHeader>
                        <TranslatedString id="terms_and_conditions.heading" />
                    </ModalHeader>
                }
            >
                { parsedLabel[1] }
            </ModalLink>
            { parsedLabel[2] }
        </>) :
        translatedLabel;
 
    return (
        <CheckboxFormField
            labelContent={ labelContent }
            name={ name }
        />
    );
};
 
const TermsConditionsModalCheckboxField = withLanguage(BaseTermsConditionsModalCheckboxField);
 
interface TermsConditionsCheckboxFieldProps {
    name: string;
    type: TermsConditionsType;
    url?: string;
}
 
const TermsConditionsCheckboxField: FunctionComponent<TermsConditionsCheckboxFieldProps> = ({
    name,
    url,
}) => {
    const labelContent = useMemo(() => (url ?
        <TranslatedHtml data={ { url } } id="terms_and_conditions.agreement_with_link_text" /> :
        <TranslatedString id="terms_and_conditions.agreement_text" />
    ), [url]);
 
    return (
        <CheckboxFormField
            labelContent={ labelContent }
            name={ name }
        />
    );
};
 
const TermsConditionsTextField: FunctionComponent<TermsConditionsTextFieldProps> = ({
    name,
    terms,
}) => {
    const renderInput = useCallback(({ field }: FieldProps) => (
        <TextArea
            defaultValue={ terms }
            name={ field.name }
            readOnly
        />
    ), [terms]);
 
    return (
        <FormField
            input={ renderInput }
            name={ `${name}Text` }
        />
    );
};
 
const TermsConditionsFieldset: FunctionComponent<TermsConditionsFieldProps> = props => {
    const { type } = props;
 
    return (
        <Fieldset
            additionalClassName="checkout-terms"
            legend={ (
                <Legend>
                    <TranslatedString id="terms_and_conditions.terms_and_conditions_heading" />
                </Legend>
            ) }
        >
            { isTermsConditionsTextArea(props) && <TermsConditionsTextField { ...props } /> }
            { isTermsConditionModal(props) && type === TermsConditionsType.Modal ?
                <TermsConditionsModalCheckboxField { ...props } /> :
                <TermsConditionsCheckboxField { ...props } /> }
        </Fieldset>
    );
};
 
function isTermsConditionsTextArea(props: any): props is TermsConditionsTextFieldProps {
    return props.type === TermsConditionsType.TextArea;
}
 
function isTermsConditionModal(props: any): props is TermsConditionsTextFieldProps {
    return props.type === TermsConditionsType.Modal;
}
 
export default memo(TermsConditionsFieldset);