-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Download and Add/Remove favorites actions
- Loading branch information
Showing
6 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) | ||
} | ||
} | ||
} | ||
} |