Newer
Older
import React, { useCallback, useState } from 'react'
import classNames from 'classnames'
import { useI18n } from 'cozy-ui/transpiled/react/I18n'
import { useDispatch, useSelector } from 'react-redux'
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'
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)
}
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(),
})
)
}
history.push('/consumption')
}, [dispatch, client, history])
return (
<div className="terms-wrapper">
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
{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>
</>
)}
<CGUModal open={openCGUModal} handleCloseClick={toggleCGUModal} />
<LegalNoticeModal
open={openLegalNoticeModal}
handleCloseClick={toggleLegalNoticeModal}
/>