Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expiration panel #2294

Merged
merged 7 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Add an ExpirationAlert component
  • Loading branch information
PolariTOON committed Dec 7, 2022
commit 36d019d33655aa8ce2f81a2187b5e1d0f6ac0ef4
81 changes: 81 additions & 0 deletions react/Viewer/components/ExpirationAlert.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { useState } from 'react'
import PropTypes from 'prop-types'

import { useClient, models } from 'cozy-client'

import Alert from '../../Alert'
import Button from '../../Buttons'
import Link from '../../Link'
import Typography from '../../Typography'
import { withViewerLocales } from '../hoc/withViewerLocales'
import { useI18n } from '../../I18n'
import { formatLocallyDistanceToNow } from '../../I18n/format'

const FILES_DOCTYPE = 'io.cozy.files'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

à voir si on a n'a pas déjà des constantes quelque part, ici ou dans cozy-client...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je ne vois pas d'endroit en particulier d'où il serait exporté, mais si un tel endroit n'existe pas, il faut avouer que ça serait sans doute bien de tout y centraliser plutôt que de redéfinir les constantes à chaque fois dans chaque lib / app.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N'allait pas trop loin dans la mutualisation, y a des trucs qui font sens, d'autres non. Le KISS impose de temps en temps de dupliquer du code, et y a rien de mal à ça. Importer une constante depuis un autre package qui définit déjà une autre constante (car on peut considérer io.cozy.files comme une constante déjà), ça n'a que très très peu d'intérêt. Voir aucun en fait ? 🤔


const { computeExpirationDate, computeExpirationNoticeLink } = models.paper

const ExpirationAlert = ({ file }) => {
const { t } = useI18n()
const client = useClient()
const [isBusy, setIsBusy] = useState(false)

const handleClose = async () => {
setIsBusy(true)
await client.collection(FILES_DOCTYPE).updateMetadataAttribute(file.id, {
...file.metadata,
hideExpirationAlert: true
})
setIsBusy(false)
}

const expirationDate = computeExpirationDate(file)
const expirationNoticeLink = computeExpirationNoticeLink(file)

return (
<Alert
severity="warning"
block
action={
<Button
color="warning"
variant="text"
size="small"
busy={isBusy}
label={t('Viewer.panel.expiration.dismiss')}
onClick={handleClose}
/>
}
className="u-mt-1 u-mh-1"
>
<Typography component="span" variant="inherit">
<Typography component="span" variant="inherit">
{t('Viewer.panel.expiration.description', {
duration: formatLocallyDistanceToNow(expirationDate)
})}
</Typography>
{expirationNoticeLink && (
<>
<Typography component="span" variant="inherit">
{' : '}
</Typography>
<Link
href={expirationNoticeLink}
rel="noreferrer"
target="_blank"
className="u-warning"
>
{new URL(expirationNoticeLink).hostname}
</Link>
</>
)}
</Typography>
</Alert>
)
}

ExpirationAlert.propTypes = {
file: PropTypes.object.isRequired
}

export default withViewerLocales(ExpirationAlert)
4 changes: 4 additions & 0 deletions react/Viewer/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
"expired": "Expired",
"expiresIn": "Expires in %{duration}"
},
"expiration": {
"description": "This document will expire in %{duration}, consider renewing it",
"dismiss": "I understood"
},
"title": "Useful information"
},
"previous": "Previous",
Expand Down
4 changes: 4 additions & 0 deletions react/Viewer/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
"expired": "Expiré",
"expiresIn": "Expire dans %{duration}"
},
"expiration": {
"description": "Ce document expirera dans %{duration}, pensez à le renouveler",
"dismiss": "J'ai compris"
},
"title": "Informations utiles"
},
"previous": "Précédente",
Expand Down