diff --git a/lib/components/user/terms-of-use-pane.tsx b/lib/components/user/terms-of-use-pane.tsx index e4d4dc445..74db697ac 100644 --- a/lib/components/user/terms-of-use-pane.tsx +++ b/lib/components/user/terms-of-use-pane.tsx @@ -1,7 +1,8 @@ import { Checkbox, ControlLabel, FormGroup } from 'react-bootstrap' import { connect } from 'react-redux' import { FormattedMessage, useIntl } from 'react-intl' -import React, { FormEventHandler } from 'react' +import { FormikProps } from 'formik' +import React, { useEffect } from 'react' import { AppReduxState } from '../../util/state-types' import { CheckboxDefaultState } from '../../util/config-types' @@ -11,6 +12,15 @@ import { TERMS_OF_STORAGE_PATH } from '../../util/constants' +import { EditedUser } from './types' + +interface Props extends FormikProps { + disableCheckTerms: boolean + locale: string + termsOfServiceLink?: string + termsOfStorageDefault?: CheckboxDefaultState +} + /** * User terms of use pane. */ @@ -19,23 +29,13 @@ const TermsOfUsePane = ({ handleBlur, handleChange, locale, + setFieldValue, termsOfServiceLink, termsOfStorageDefault = CheckboxDefaultState.VISIBLE_UNCHECKED, values: userData -}: { - disableCheckTerms: boolean - handleBlur: () => void - handleChange: FormEventHandler - locale: string - termsOfServiceLink?: string - termsOfStorageDefault?: CheckboxDefaultState - values: { - hasConsentedToTerms: boolean - storeTripHistory: boolean - } -}) => { +}: Props) => { const intl = useIntl() - const { hasConsentedToTerms, storeTripHistory } = userData + const { hasConsentedToTerms, id, storeTripHistory } = userData const TOSLinkWithI18n = termsOfServiceLink?.replace('{locale}', locale) @@ -43,6 +43,17 @@ const TermsOfUsePane = ({ const showStorageTerms = termsOfStorageDefault.startsWith('visible-') + useEffect(() => { + // When creating a new account, pre-check the storage field per defaults. + if ( + !id && + (termsOfStorageDefault === CheckboxDefaultState.HIDDEN_CHECKED || + termsOfStorageDefault === CheckboxDefaultState.VISIBLE_CHECKED) + ) { + setFieldValue('storeTripHistory', true) + } + }, [id, setFieldValue, termsOfStorageDefault]) + return (