Skip to content

Commit

Permalink
Merge pull request #948 from cozy/feat/608-609
Browse files Browse the repository at this point in the history
Feat/Change the selected group when removing it
  • Loading branch information
Merkur39 committed Sep 1, 2023
2 parents da31fa8 + 30c0493 commit 088b80a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/components/ContentResult.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import SearchInput from './Search/SearchInput'
import Toolbar from './Toolbar'
import {
filterContactsByGroup,
hasSelectedGroup,
translatedDefaultSelectedGroup
} from '../helpers/groups'
import { filterContactsBySearch, delayedSetThreshold } from '../helpers/search'
Expand Down Expand Up @@ -64,6 +65,13 @@ export const ContentResult = ({ contacts, allGroups }) => {
delayedSetThreshold(thresholdValue)
}

// If the currently selected group is deleted, the default filter is set.
useEffect(() => {
if (hasSelectedGroup(selectedGroup) && !allGroups.includes(selectedGroup)) {
setSelectedGroup(translatedDefaultSelectedGroup(t))
}
}, [allGroups, selectedGroup, setSelectedGroup, t])

useEffect(() => {
const filteredContactsByGroup = filterContactsByGroup(
contacts,
Expand Down
21 changes: 18 additions & 3 deletions src/components/Modals/GroupDeleteConfirmationModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,32 @@ const GroupDeleteConfirmationModal = () => {
cleanTrashedGroupsAndATrashedContacts(client)
}, alertDuration)

let contactsTrashCount = 0
if (deleteAssociatedContacts) {
await trashedAllContactsByGroupId(client, groupId)
const contacts = await trashedAllContactsByGroupId(client, groupId)
contactsTrashCount = contacts.length
}
await trashedGroupById(client, groupId)

Alerter.info('groups.removed', {
const translationKey = deleteAssociatedContacts
? contactsTrashCount > 0
? 'groups.removed_with_contacts'
: 'groups.removed_without_contacts'
: 'groups.removed'

Alerter.info(translationKey, {
name: groupName,
smart_count: contactsTrashCount,
buttonText: t('cancel'),
buttonAction: () => {
clearTimeout(timeout)
cancelTrashGroupById(client, groupId)

cancelTrashGroupById(
client,
groupId,
deleteAssociatedContacts,
contactsTrashCount
)
if (deleteAssociatedContacts) {
cancelTrashContactsByGroupId(client, groupId)
}
Expand Down
13 changes: 7 additions & 6 deletions src/connections/allContacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,13 @@ export const trashedAllContactsByGroupId = async (client, groupId) => {
})

try {
await client.saveAll(contactsTrashed)
} catch (error) {
if (contactsTrashed.length === 0) {
log('info', 'No contacts to trash')
} else {
log('error', `Error saving contact to trash: ${error}`)
if (contactsTrashed.length > 0) {
const result = await client.saveAll(contactsTrashed)
return result.data
}
log('info', 'No contacts to trash')
return []
} catch (error) {
log('error', `Error saving contact to trash: ${error}`)
}
}
18 changes: 15 additions & 3 deletions src/connections/allGroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ export const trashedGroupById = async (client, groupId) => {
* @param {Object} client - CozyClient
* @param {String} groupId - Group id
*/
export const cancelTrashGroupById = async (client, groupId) => {
export const cancelTrashGroupById = async (
client,
groupId,
deleteAssociatedContacts,
contactsTrashCount
) => {
const groupQueryById = buildGroupQueryById(groupId)
const { data: group } = await client.query(
groupQueryById.definition(),
Expand All @@ -69,7 +74,14 @@ export const cancelTrashGroupById = async (client, groupId) => {
trashed: false
})

Alerter.info('groups.remove_canceled', {
name: group.name
const translationKey = deleteAssociatedContacts
? contactsTrashCount > 0
? 'groups.remove_cancel_with_contacts'
: 'groups.remove_cancel_without_contacts'
: 'groups.remove_canceled'

Alerter.info(translationKey, {
name: group.name,
smart_count: contactsTrashCount
})
}
5 changes: 4 additions & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@
},
"name": "Name of the group",
"removed": "The group %{name} has been removed.",
"remove_canceled": "The group %{name} has been restored. Contacts are still linked.",
"remove_canceled": "The group %{name} has been restored.",
"remove_cancel_with_contacts": "The group %{name} has been restored. Contact are still linked. |||| The group %{name} has been restored. Contacts are still linked.",
"removed_without_contacts": "The group %{name} has been removed. It had no associated contacts.",
"removed_with_contacts": "The group %{name} and its associated contact have been successfully removed. |||| The group %{name} and its associated contacts have been successfully removed.",
"already_exists": "The group %{name} already exists.",
"renamed": {
"success": "The group has been renamed correctly.",
Expand Down
5 changes: 4 additions & 1 deletion src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@
},
"name": "Nom du groupe",
"removed": "Le groupe %{name} a bien été supprimé.",
"remove_canceled": "Le groupe %{name} n'a pas été supprimé. Vous n'avez pas perdu la liaison avec ses contacts.",
"remove_canceled": "Le groupe %{name} n'a pas été supprimé",
"remove_cancel_with_contacts": "Le groupe %{name} n'a pas été supprimé. Vous n'avez pas perdu la liaison avec son contact. |||| Le groupe %{name} n'a pas été supprimé. Vous n'avez pas perdu la liaison avec ses contacts.",
"removed_without_contacts": "Le groupe %{name} a bien été supprimé. Il n'avait aucun contact associé.",
"removed_with_contacts": "Le groupe %{name} et son contact associé ont bien été supprimés. |||| Le groupe %{name} et ses contacts associés ont bien été supprimés.",
"already_exists": "Le groupe %{name} existe déjà.",
"renamed": {
"success": "Le groupe a bien été renommé.",
Expand Down

0 comments on commit 088b80a

Please sign in to comment.