Skip to content
Snippets Groups Projects
TermsView.tsx 4.78 KiB
Newer Older
  • Learn to ignore specific revisions
  • Guilhem CARRON's avatar
    Guilhem CARRON committed
    import React, { useCallback, useState } from 'react'
    import classNames from 'classnames'
    import { useI18n } from 'cozy-ui/transpiled/react/I18n'
    
    import { useDispatch, useSelector } from 'react-redux'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { Button } from '@material-ui/core'
    import TermsService from 'services/terms.service'
    import { useClient } from 'cozy-client'
    import DataShareConsentContent from './DataShareConsentContent'
    import './termsView.scss'
    import { useHistory } from 'react-router-dom'
    import { decoreText } from 'utils/decoreText'
    import { updateTermValidation } from 'store/global/global.actions'
    
    import CGUModal from './CGUModal'
    import LegalNoticeModal from './LegalNoticeModal'
    
    import { AppStore } from 'store'
    import MinorUpdateContent from './MinorUpdateContent'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    const TermsView: React.FC = () => {
      const { t } = useI18n()
      const client = useClient()
      const dispatch = useDispatch()
      const history = useHistory()
      const [GCUValidation, setGCUValidation] = useState<boolean>(false)
    
      const [dataConsentValidation, setDataConsentValidation] =
        useState<boolean>(false)
    
      const [openCGUModal, setOpenCGUModal] = useState<boolean>(false)
    
      const [openLegalNoticeModal, setOpenLegalNoticeModal] =
        useState<boolean>(false)
    
      const { termsStatus } = useSelector((state: AppStore) => state.ecolyo.global)
    
    
      const toggleCGUModal = () => {
        setOpenCGUModal(prev => !prev)
      }
    
      const toggleLegalNoticeModal = () => {
        setOpenLegalNoticeModal(prev => !prev)
      }
    
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const handleGCUValidate = useCallback(() => {
        setGCUValidation(prev => !prev)
      }, [])
      const handleDataConsentValidation = useCallback(() => {
        setDataConsentValidation(prev => !prev)
      }, [])
    
      const handleTermValidate = useCallback(async () => {
        const termsService = new TermsService(client)
        const createdTerm = await termsService.createTerm()
        if (createdTerm) {
    
          dispatch(
            updateTermValidation({
              accepted: true,
              versionType: await termsService.getTermsVersionType(),
            })
          )
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
        history.push('/consumption')
      }, [dispatch, client, history])
    
      return (
        <div className="terms-wrapper">
    
          {termsStatus.versionType === 'major' ||
          termsStatus.versionType === 'init' ? (
            <>
              <div className="terms-content">
                <DataShareConsentContent />
                <label
                  className={classNames('checkbox', {
                    ['answer-checked']: dataConsentValidation,
                  })}
                >
                  <input
                    type={'checkbox'}
                    name="Data-consent-validation"
                    onChange={handleDataConsentValidation}
                    checked={dataConsentValidation}
                  />
                  {t('dataShare.validDataConsent')}
                </label>
                <label
                  className={classNames('checkbox', {
                    ['answer-checked']: GCUValidation,
                  })}
                >
                  <input
                    type={'checkbox'}
                    name="GCU-validation"
                    onChange={handleGCUValidate}
                    checked={GCUValidation}
                  />
                  <span>
                    {decoreText(t('dataShare.validCGU'), () => toggleCGUModal())}
                    {decoreText(t('dataShare.validLegal'), () =>
                      toggleLegalNoticeModal()
                    )}
                  </span>
                </label>
              </div>
              <div className="terms-footer">
                <Button
                  aria-label={t('dataShare.accessibility.button_accept')}
                  onClick={handleTermValidate}
                  className={classNames('gcu-modal-button', {
                    ['disabled']: !GCUValidation || !dataConsentValidation,
                  })}
                  disabled={!GCUValidation || !dataConsentValidation}
                  classes={{
                    root: 'btn-profile-next rounded',
                    label: 'text-16-bold',
                  }}
                >
                  {t('dataShare.button_accept')}
                </Button>
              </div>
            </>
          ) : (
            <>
              <div className="terms-content">
                <MinorUpdateContent
                  toggleLegalNoticeModal={toggleLegalNoticeModal}
                />
              </div>
              <div className="terms-footer">
                <Button
                  aria-label={t('minorUpdate.button')}
                  onClick={handleTermValidate}
                  className={'gcu-modal-button'}
                  classes={{
                    root: 'btn-profile-next rounded',
                    label: 'text-16-bold',
                  }}
                >
                  {t('minorUpdate.button')}
                </Button>
              </div>
            </>
          )}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    
          <CGUModal open={openCGUModal} handleCloseClick={toggleCGUModal} />
          <LegalNoticeModal
            open={openLegalNoticeModal}
            handleCloseClick={toggleLegalNoticeModal}
          />
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        </div>
      )
    }
    
    export default TermsView