From 6e4bca42fbaadee1d44d868e7d80ba7887a1a0e1 Mon Sep 17 00:00:00 2001 From: JF-Cozy Date: Tue, 21 Jan 2025 15:12:04 +0100 Subject: [PATCH] feat: Add Download and Add/Remove favorites actions --- react/ActionsMenu/Actions/addToFavorites.js | 66 +++++++++++++++++++ react/ActionsMenu/Actions/download.js | 42 ++++++++++++ react/ActionsMenu/Actions/index.js | 3 + react/ActionsMenu/Actions/locales/en.json | 12 ++++ react/ActionsMenu/Actions/locales/fr.json | 12 ++++ .../Actions/removeFromFavorites.js | 66 +++++++++++++++++++ 6 files changed, 201 insertions(+) create mode 100644 react/ActionsMenu/Actions/addToFavorites.js create mode 100644 react/ActionsMenu/Actions/download.js create mode 100644 react/ActionsMenu/Actions/removeFromFavorites.js diff --git a/react/ActionsMenu/Actions/addToFavorites.js b/react/ActionsMenu/Actions/addToFavorites.js new file mode 100644 index 000000000..905f75e5b --- /dev/null +++ b/react/ActionsMenu/Actions/addToFavorites.js @@ -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 ( + + + + + + + ) + }) + + 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' }) + } + } + } +} diff --git a/react/ActionsMenu/Actions/download.js b/react/ActionsMenu/Actions/download.js new file mode 100644 index 000000000..679dad26e --- /dev/null +++ b/react/ActionsMenu/Actions/download.js @@ -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 ( + + + + + + + ) + }) + + 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 }) + } +} diff --git a/react/ActionsMenu/Actions/index.js b/react/ActionsMenu/Actions/index.js index 6859b9544..b76942984 100644 --- a/react/ActionsMenu/Actions/index.js +++ b/react/ActionsMenu/Actions/index.js @@ -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' diff --git a/react/ActionsMenu/Actions/locales/en.json b/react/ActionsMenu/Actions/locales/en.json index a8277e675..4eda97732 100644 --- a/react/ActionsMenu/Actions/locales/en.json +++ b/react/ActionsMenu/Actions/locales/en.json @@ -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": { diff --git a/react/ActionsMenu/Actions/locales/fr.json b/react/ActionsMenu/Actions/locales/fr.json index 44b8898aa..cbd36e53d 100644 --- a/react/ActionsMenu/Actions/locales/fr.json +++ b/react/ActionsMenu/Actions/locales/fr.json @@ -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": { diff --git a/react/ActionsMenu/Actions/removeFromFavorites.js b/react/ActionsMenu/Actions/removeFromFavorites.js new file mode 100644 index 000000000..eda3962de --- /dev/null +++ b/react/ActionsMenu/Actions/removeFromFavorites.js @@ -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 ( + + + + + + + ) + }) + + 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' }) + } + } + } +}