Skip to content
Snippets Groups Projects
TermsView.tsx 4.51 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Button } from '@material-ui/core'
    import { useClient } from 'cozy-client'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { useI18n } from 'cozy-ui/transpiled/react/I18n'
    
    import React, { useCallback, useState } from 'react'
    
    import { useNavigate } from 'react-router-dom'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import TermsService from 'services/terms.service'
    
    import { updateTermsStatus } from 'store/global/global.slice'
    
    import { useAppDispatch, useAppSelector } from 'store/hooks'
    
    import CGUModal from './CGUModal'
    
    import DataShareConsentContent from './DataShareConsentContent'
    
    import LegalNoticeModal from './LegalNoticeModal'
    
    import MinorUpdateContent from './MinorUpdateContent'
    
    import './termsView.scss'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    
    const TermsView = () => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const { t } = useI18n()
      const client = useClient()
    
      const navigate = useNavigate()
    
      const dispatch = useAppDispatch()
      const { termsStatus } = useAppSelector(state => state.ecolyo.global)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      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 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) {
    
            updateTermsStatus({
    
              accepted: true,
              versionType: await termsService.getTermsVersionType(),
            })
          )
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
    
        navigate('/consumption')
      }, [dispatch, client, navigate])
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
      return (
        <div className="terms-wrapper">
    
          {termsStatus.versionType === 'major' ||
          termsStatus.versionType === 'init' ? (
            <>
              <div className="terms-content">
                <DataShareConsentContent />
    
                <label className="inline">
    
                    name="Data-consent-validation"
    
                    className="inputCheckbox"
    
                    onChange={handleDataConsentValidation}
                    checked={dataConsentValidation}
                  />
                  {t('dataShare.validDataConsent')}
                </label>
    
                <label className="inline">
    
                    name="GCU-validation"
    
                    className="inputCheckbox"
    
                    onChange={handleGCUValidate}
                    checked={GCUValidation}
                  />
    
                    <span>{t('dataShare.validCGU')}</span>
                    <Button
                      size="small"
                      className="btnText"
                      onClick={toggleCGUModal}
                    >
                      {t('dataShare.validCGU_button')}
                    </Button>
    
                    <span>{t('dataShare.validLegal')}</span>
                    <Button
                      size="small"
                      className="btnText"
                      onClick={toggleLegalNoticeModal}
                    >
                      {t('dataShare.validLegal_button')}
                    </Button>
                    <span>{t('dataShare.validLegal2')}</span>
    
                </label>
              </div>
              <div className="terms-footer">
                <Button
                  aria-label={t('dataShare.accessibility.button_accept')}
                  onClick={handleTermValidate}
                  disabled={!GCUValidation || !dataConsentValidation}
    
                  className="btnPrimary"
    
                >
                  {t('dataShare.button_accept')}
                </Button>
              </div>
            </>
          ) : (
            <>
              <div className="terms-content">
    
                <MinorUpdateContent />
    
              </div>
              <div className="terms-footer">
                <Button
                  aria-label={t('minorUpdate.button')}
                  onClick={handleTermValidate}
    
                  className="btnPrimary"
    
                >
                  {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