Skip to content

Commit

Permalink
feat: Add Download and Add/Remove favorites actions
Browse files Browse the repository at this point in the history
  • Loading branch information
JF-Cozy committed Jan 21, 2025
1 parent 10077d0 commit 6e4bca4
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 0 deletions.
66 changes: 66 additions & 0 deletions react/ActionsMenu/Actions/addToFavorites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { forwardRef } from 'react'

import { splitFilename } from 'cozy-client/dist/models/file'

import { getActionsI18n } from './locales/withActionsLocales'
import Icon from '../../Icon'
import StarOutlineIcon from '../../Icons/StarOutline'
import ListItemIcon from '../../ListItemIcon'
import ListItemText from '../../ListItemText'
import ActionsMenuItem from '../ActionsMenuItem'

const makeComponent = (label, icon) => {
const Component = forwardRef((props, ref) => {
return (
<ActionsMenuItem {...props} ref={ref}>
<ListItemIcon>
<Icon icon={icon} />
</ListItemIcon>
<ListItemText primary={label} />
</ActionsMenuItem>
)
})

Component.displayName = 'addToFavorites'

return Component
}

export const addToFavorites = ({ showAlert }) => {
const { t } = getActionsI18n()
const icon = StarOutlineIcon
const label = t('favorites.add.label')

return {
name: 'addToFavorites',
icon,
label,
displayCondition: docs =>
docs.length > 0 && docs.every(doc => !doc.cozyMetadata?.favorite),
Component: makeComponent(label, icon),
action: async (docs, { client }) => {
try {
for (const doc of docs) {
await client.save({
...doc,
cozyMetadata: {
...doc.cozyMetadata,
favorite: true
}
})
}

const { filename } = splitFilename(docs[0])
showAlert({
message: t('favorites.add.success', {
filename,
smart_count: docs.length
}),
severity: 'success'
})
} catch (error) {
showAlert({ message: t('favorites.error'), severity: 'error' })
}
}
}
}
42 changes: 42 additions & 0 deletions react/ActionsMenu/Actions/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { forwardRef } from 'react'

import { downloadFile } from 'cozy-client/dist/models/file'

import { getActionsI18n } from './locales/withActionsLocales'
import Icon from '../../Icon'
import DownloadIcon from '../../Icons/Download'
import ListItemIcon from '../../ListItemIcon'
import ListItemText from '../../ListItemText'
import ActionsMenuItem from '../ActionsMenuItem'

const makeComponent = (label, icon) => {
const Component = forwardRef((props, ref) => {
return (
<ActionsMenuItem {...props} ref={ref}>
<ListItemIcon>
<Icon icon={icon} />
</ListItemIcon>
<ListItemText primary={label} />
</ActionsMenuItem>
)
})

Component.displayName = 'download'

return Component
}

export const download = ({ encryptedUrl }) => {
const { t } = getActionsI18n()
const icon = DownloadIcon
const label = t('download')

return {
name: 'download',
icon,
label,
Component: makeComponent(label, icon),
action: (docs, { client, webviewIntent }) =>
downloadFile({ client, file: docs[0], url: encryptedUrl, webviewIntent })
}
}
3 changes: 3 additions & 0 deletions react/ActionsMenu/Actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export { smsTo } from './smsTo'
export { call } from './call'
export { emailTo } from './emailTo'
export { print } from './print'
export { download } from './download'
export { addToFavorites } from './addToFavorites'
export { removeFromFavorites } from './removeFromFavorites'
export { viewInContacts } from './viewInContacts'
export { viewInDrive } from './viewInDrive'
export { copyToClipboard } from './copyToClipboard'
Expand Down
12 changes: 12 additions & 0 deletions react/ActionsMenu/Actions/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
"emailTo": "Send an email",
"smsTo": "Send a message",
"print": "Print",
"download": "Download",
"favorites": {
"add": {
"label": "Add to favorites",
"success": "%{filename} has been added to favorites |||| These items have been added to favorites"
},
"remove": {
"label": "Remove from favorites",
"success": "%{filename} has been removed from favorites |||| These items have been removed from favorites"
},
"error": "An error occurred, please try again."
},
"others": "Others",
"editAttribute": "Edit attribute",
"copyToClipboard": {
Expand Down
12 changes: 12 additions & 0 deletions react/ActionsMenu/Actions/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
"emailTo": "Envoyer un e-mail",
"smsTo": "Envoyer un message",
"print": "Imprimer",
"download": "Télécharger",
"favorites": {
"add": {
"label": "Ajouter aux favoris",
"success": "%{filename} a été ajouté aux favoris |||| Ces éléments ont été ajoutés aux favoris"
},
"remove": {
"label": "Retirer des favoris",
"success": "%{filename} a été retiré des favoris |||| Ces éléments ont été retirés des favoris"
},
"error": "Une erreur est survenue, merci de réessayer."
},
"others": "Autres",
"editAttribute": "Editer l'attribut",
"copyToClipboard": {
Expand Down
66 changes: 66 additions & 0 deletions react/ActionsMenu/Actions/removeFromFavorites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { forwardRef } from 'react'

import { splitFilename } from 'cozy-client/dist/models/file'

import { getActionsI18n } from './locales/withActionsLocales'
import Icon from '../../Icon'
import StarIcon from '../../Icons/Star'
import ListItemIcon from '../../ListItemIcon'
import ListItemText from '../../ListItemText'
import ActionsMenuItem from '../ActionsMenuItem'

const makeComponent = (label, icon) => {
const Component = forwardRef((props, ref) => {
return (
<ActionsMenuItem {...props} ref={ref}>
<ListItemIcon>
<Icon icon={icon} />
</ListItemIcon>
<ListItemText primary={label} />
</ActionsMenuItem>
)
})

Component.displayName = 'removeFromFavorites'

return Component
}

export const removeFromFavorites = ({ showAlert }) => {
const { t } = getActionsI18n()
const icon = StarIcon
const label = t('favorites.remove.label')

return {
name: 'removeFromFavorites',
icon,
label,
displayCondition: docs =>
docs.length > 0 && docs.every(doc => doc.cozyMetadata?.favorite),
Component: makeComponent(label, icon),
action: async (docs, { client }) => {
try {
for (const doc of docs) {
await client.save({
...doc,
cozyMetadata: {
...doc.cozyMetadata,
favorite: false
}
})
}

const { filename } = splitFilename(docs[0])
showAlert({
message: t('favorites.success.remove', {
filename,
smart_count: docs.length
}),
severity: 'success'
})
} catch (error) {
showAlert({ message: t('favorites.error'), severity: 'error' })
}
}
}
}

0 comments on commit 6e4bca4

Please sign in to comment.