Skip to content

Commit 9793851

Browse files
committed
feat: Add an ExpirationAlert component
1 parent a48496d commit 9793851

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import React, { useState } from 'react'
2+
import PropTypes from 'prop-types'
3+
4+
import { useClient, models } from 'cozy-client'
5+
6+
import Alert from '../../Alert'
7+
import Button from '../../Buttons'
8+
import Link from '../../Link'
9+
import Typography from '../../Typography'
10+
import { withViewerLocales } from '../hoc/withViewerLocales'
11+
import { useI18n } from '../../I18n'
12+
import { formatLocallyDistanceToNow } from '../../I18n/format'
13+
14+
const FILES_DOCTYPE = 'io.cozy.files'
15+
16+
const { computeExpirationDate, computeExpirationNoticeLink } = models.paper
17+
18+
const ExpirationAlert = ({ file }) => {
19+
const { t } = useI18n()
20+
const client = useClient()
21+
const [isBusy, setIsBusy] = useState(false)
22+
23+
const handleClose = async () => {
24+
setIsBusy(true)
25+
await client.collection(FILES_DOCTYPE).updateMetadataAttribute(file.id, {
26+
...file.metadata,
27+
hideExpirationAlert: true
28+
})
29+
setIsBusy(false)
30+
}
31+
32+
const expirationDate = computeExpirationDate(file)
33+
const expirationNoticeLink = computeExpirationNoticeLink(file)
34+
35+
return (
36+
<Alert
37+
severity="warning"
38+
block
39+
action={
40+
<Button
41+
color="warning"
42+
variant="text"
43+
size="small"
44+
busy={isBusy}
45+
label={t('Viewer.panel.expiration.dismiss')}
46+
onClick={handleClose}
47+
/>
48+
}
49+
className="u-mt-1 u-mh-1"
50+
>
51+
<Typography component="span" variant="inherit">
52+
<Typography component="span" variant="inherit">
53+
{t('Viewer.panel.expiration.description', {
54+
duration: formatLocallyDistanceToNow(expirationDate)
55+
})}
56+
</Typography>
57+
{expirationNoticeLink && (
58+
<>
59+
<Typography component="span" variant="inherit">
60+
{' : '}
61+
</Typography>
62+
<Link
63+
href={expirationNoticeLink}
64+
rel="noreferrer"
65+
target="_blank"
66+
className="u-warning"
67+
>
68+
{new URL(expirationNoticeLink).hostname}
69+
</Link>
70+
</>
71+
)}
72+
</Typography>
73+
</Alert>
74+
)
75+
}
76+
77+
ExpirationAlert.propTypes = {
78+
file: PropTypes.object.isRequired
79+
}
80+
81+
export default withViewerLocales(ExpirationAlert)

react/Viewer/locales/en.json

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
"expired": "Expired",
7777
"expiresIn": "Expires in %{duration}"
7878
},
79+
"expiration": {
80+
"description": "This document will expire in %{duration}, consider renewing it",
81+
"dismiss": "I understood"
82+
},
7983
"title": "Useful information"
8084
},
8185
"previous": "Previous",

react/Viewer/locales/fr.json

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
"expired": "Expiré",
7777
"expiresIn": "Expire dans %{duration}"
7878
},
79+
"expiration": {
80+
"description": "Ce document expirera dans %{duration}, pensez à le renouveler",
81+
"dismiss": "J'ai compris"
82+
},
7983
"title": "Informations utiles"
8084
},
8185
"previous": "Précédente",

0 commit comments

Comments
 (0)