Skip to content

Commit

Permalink
feat(ActionsMenu): Add print action
Browse files Browse the repository at this point in the history
  • Loading branch information
JF-Cozy committed Dec 7, 2023
1 parent 8dd4613 commit 82d918d
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 3 deletions.
20 changes: 20 additions & 0 deletions react/ActionsMenu/Actions/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,23 @@ export const getOnlyNeededActions = (actions, file) => {
})
)
}

/**
* Make a base64 string from a File object
* @param {File} file - File object
* @returns {Promise<string | null>} base64 string
*/
export const makeBase64FromFile = async file => {
const reader = new FileReader()
reader.readAsDataURL(file)

return new Promise((resolve, reject) => {
reader.onload = () => {
const base64 = reader.result
resolve(base64)
}
reader.onerror = err => {
reject(err)
}
})
}
20 changes: 19 additions & 1 deletion react/ActionsMenu/Actions/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { makeActions } from './helpers'
import { makeActions, makeBase64FromFile } from './helpers'

describe('makeActions', () => {
it('should have empty actions array', () => {
Expand Down Expand Up @@ -60,3 +60,21 @@ describe('makeActions', () => {
expect(actions).toStrictEqual([{ mockConstructor: { propA: 0, propB: 1 } }])
})
})

describe('makeBase64FromFile', () => {
it('returns a base64 string for a given file', async () => {
const file = new File(['test'], 'test.txt', { type: 'text/plain' })
const base64String = await makeBase64FromFile(file)

expect(base64String).toMatch(
/^data:text\/plain;base64,[a-zA-Z0-9+/]+={0,2}$/
)
})

it('rejects with an error if the file cannot be read', async () => {
const file = new File(['test'], 'test.txt', { type: 'text/plain' })
const invalidFile = { ...file, size: -1 }

await expect(makeBase64FromFile(invalidFile)).rejects.toThrow()
})
})
1 change: 1 addition & 0 deletions react/ActionsMenu/Actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export { modify } from './modify'
export { smsTo } from './smsTo'
export { call } from './call'
export { emailTo } from './emailTo'
export { print } from './print'
export { viewInContacts } from './viewInContacts'
67 changes: 67 additions & 0 deletions react/ActionsMenu/Actions/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React, { forwardRef } from 'react'

import logger from 'cozy-logger'
import { useWebviewIntent } from 'cozy-intent'
import { fetchBlobFileById } from 'cozy-client/dist/models/file'

import { makeBase64FromFile } from './helpers'
import PrinterIcon from '../../Icons/Printer'
import withActionsLocales from './locales/withActionsLocales'
import ActionsMenuItem from '../ActionsMenuItem'
import ListItemIcon from '../../ListItemIcon'
import Icon from '../../Icon'
import ListItemText from '../../ListItemText'
import { useI18n } from '../../providers/I18n'

export const print = () => {
return {
name: 'print',
action: async (doc, { client, webviewIntent }) => {
if (webviewIntent) {
try {
const blob = await fetchBlobFileById(client, doc._id)
const base64 = await makeBase64FromFile(blob)

return webviewIntent.call('print', base64)
} catch (error) {
logger.error(
`Error trying to print document with Flagship App: ${JSON.stringify(
error
)}`
)
}
}

try {
const downloadURL = await client
.collection('io.cozy.files')
.getDownloadLinkById(doc._id, doc.name)

window.open(downloadURL, '_blank')
} catch (error) {
logger.error(`Error trying to print document: ${JSON.stringify(error)}`)
}
},
Component: withActionsLocales(
forwardRef(({ onClick, ...props }, ref) => {
const { t } = useI18n()
const webviewIntent = useWebviewIntent()

return (
<ActionsMenuItem
{...props}
ref={ref}
onClick={() => {
onClick({ webviewIntent })
}}
>
<ListItemIcon>
<Icon icon={PrinterIcon} />
</ListItemIcon>
<ListItemText primary={t('print')} />
</ActionsMenuItem>
)
})
)
}
}
4 changes: 2 additions & 2 deletions react/ActionsMenu/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import Icon from 'cozy-ui/transpiled/react/Icon'
import FileTypeText from 'cozy-ui/transpiled/react/Icons/FileTypeText'
import FileIcon from 'cozy-ui/transpiled/react/Icons/File'
import { makeActions, modify, emailTo, viewInContacts, divider, smsTo, call } from 'cozy-ui/transpiled/react/ActionsMenu/Actions'
import { makeActions, modify, emailTo, print, viewInContacts, divider, smsTo, call } from 'cozy-ui/transpiled/react/ActionsMenu/Actions'
initialState = { showMenu: isTesting() }
Expand Down Expand Up @@ -87,7 +87,7 @@ const customAction = () => ({
})
})
const actions = makeActions([ modify, viewInContacts, divider, call, smsTo, emailTo, divider, customAction ])
const actions = makeActions([ modify, viewInContacts, divider, call, smsTo, emailTo, print, divider, customAction ])
;
Expand Down
1 change: 1 addition & 0 deletions react/deprecated/ActionMenu/Actions/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"modify": "Modify",
"emailTo": "Send an email",
"smsTo": "Send a message",
"print": "Print",
"call": "Call"
}
1 change: 1 addition & 0 deletions react/deprecated/ActionMenu/Actions/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"modify": "Modifier",
"emailTo": "Envoyer un e-mail",
"smsTo": "Envoyer un message",
"print": "Imprimer",
"call": "Appeler"
}

0 comments on commit 82d918d

Please sign in to comment.