-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1015 from cozy/feat/ver-772_2
feat: Add favorite button
- Loading branch information
Showing
20 changed files
with
214 additions
and
91 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
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,50 @@ | ||
import minilog from 'cozy-minilog' | ||
|
||
import { updateContact } from '../../connections/allContacts' | ||
|
||
const log = minilog('connections/allContacts') | ||
|
||
/** | ||
* Action to favorite a contact | ||
* @param {object} options.client - CozyClient instance | ||
* @param {array} options.selection - Array of selected contacts | ||
* @param {object} options.t - Translation function | ||
*/ | ||
export const favorite = ({ client, selection, clearSelection, t }) => { | ||
const noFavoriteSelected = selection.filter( | ||
contact => !contact.cozyMetadata?.favorite | ||
) | ||
const isAllFavorite = noFavoriteSelected.length === 0 | ||
const icon = isAllFavorite ? 'star' : 'star-outline' | ||
const label = isAllFavorite | ||
? t('SelectionBar.remove_favorite') | ||
: t('SelectionBar.add_favorite') | ||
|
||
return { | ||
name: 'favorite', | ||
label, | ||
icon, | ||
action: async () => { | ||
const contactToUpdate = isAllFavorite ? selection : noFavoriteSelected | ||
const favorite = isAllFavorite ? false : true | ||
|
||
try { | ||
await Promise.all( | ||
contactToUpdate.map(contact => | ||
updateContact({ | ||
client, | ||
contact, | ||
attributes: { | ||
cozyMetadata: { favorite } | ||
} | ||
}) | ||
) | ||
) | ||
} catch (error) { | ||
log.error('Error updating contact', error) | ||
} finally { | ||
clearSelection() | ||
} | ||
} | ||
} | ||
} |
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,85 @@ | ||
import { favorite } from './favorite' | ||
import { updateContact } from '../../connections/allContacts' | ||
|
||
jest.mock('../../connections/allContacts', () => ({ | ||
updateContact: jest.fn() | ||
})) | ||
|
||
describe('favorite', () => { | ||
const clearSelection = jest.fn() | ||
|
||
const favoriteSelection = [ | ||
{ | ||
_id: '000', | ||
cozyMetadata: { | ||
favorite: true | ||
} | ||
}, | ||
{ | ||
_id: '001', | ||
cozyMetadata: { | ||
favorite: true | ||
} | ||
} | ||
] | ||
const unfavoriteSelection = [ | ||
{ | ||
_id: '100', | ||
cozyMetadata: { | ||
favorite: false | ||
} | ||
}, | ||
{ | ||
_id: '101', | ||
cozyMetadata: { | ||
favorite: false | ||
} | ||
} | ||
] | ||
const mixedSelection = [favoriteSelection[0], unfavoriteSelection[0]] | ||
|
||
it('should return action with "add_favorite" label & "star-outline" icon', async () => { | ||
const action = favorite({ | ||
selection: unfavoriteSelection, | ||
clearSelection, | ||
t: jest.fn(k => k) | ||
}) | ||
|
||
expect(action.label).toBe('SelectionBar.add_favorite') | ||
expect(action.icon).toBe('star-outline') | ||
|
||
const action2 = favorite({ | ||
selection: mixedSelection, | ||
clearSelection, | ||
t: jest.fn(k => k) | ||
}) | ||
|
||
expect(action2.label).toBe('SelectionBar.add_favorite') | ||
expect(action2.icon).toBe('star-outline') | ||
}) | ||
|
||
it('should return action with "remove_favorite" label & "star" icon', async () => { | ||
const action = favorite({ | ||
selection: favoriteSelection, | ||
clearSelection, | ||
t: jest.fn(k => k) | ||
}) | ||
|
||
expect(action.label).toBe('SelectionBar.remove_favorite') | ||
expect(action.icon).toBe('star') | ||
}) | ||
|
||
it('Should only update not favorite contacts', async () => { | ||
const action = favorite({ | ||
selection: mixedSelection, | ||
clearSelection, | ||
t: jest.fn(k => k) | ||
}) | ||
|
||
expect(action.label).toBe('SelectionBar.add_favorite') | ||
expect(action.icon).toBe('star-outline') | ||
await action.action() | ||
expect(updateContact).toHaveBeenCalledTimes(1) | ||
expect(clearSelection).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.