Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HS-1251563] Sort and refresh contact tags #1179

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Formik, FormikHelpers } from 'formik';
import { useSnackbar } from 'notistack';
import { useTranslation } from 'react-i18next';
import * as yup from 'yup';
import { ContactFiltersDocument } from 'pages/accountLists/[accountListId]/contacts/Contacts.generated';
import { ContactTagIcon, ContactTagInput } from 'src/components/Tags/Tags';
import {
useGetContactTagListQuery,
Expand Down Expand Up @@ -112,12 +113,19 @@ export const ContactTags: React.FC<ContactTagsProps> = ({
return;
}

// If a tag is in the list that isn't in contactTagList, it is a new tag, which means we need to
// refetch the contact filters
const newTag = tagList.some(
(tag) => !contactTagsList?.accountList.contactTagList.includes(tag),
);

const { data } = await updateContactTags({
variables: {
accountListId,
contactId,
tagList: [...contactTags, ...tagList],
},
refetchQueries: newTag ? [ContactFiltersDocument] : [],
optimisticResponse: {
updateContact: {
__typename: 'ContactUpdateMutationPayload',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import { Formik } from 'formik';
import { useSnackbar } from 'notistack';
import { useTranslation } from 'react-i18next';
import * as yup from 'yup';
import { ContactsDocument } from 'pages/accountLists/[accountListId]/contacts/Contacts.generated';
import {
ContactFiltersDocument,
ContactsDocument,
} from 'pages/accountLists/[accountListId]/contacts/Contacts.generated';
import { ContactTagIcon, ContactTagInput } from 'src/components/Tags/Tags';
import {
CancelButton,
Expand Down Expand Up @@ -100,6 +103,13 @@ export const MassActionsAddTagsModal: React.FC<
id: contact.id,
tagList: [...new Set([...tags, ...contact.tagList])],
})) ?? [];

// If a tag is in the list that isn't in contactTagList, it is a new tag, which means we need to
// refetch the contact filters
const newTag = tags.some(
(tag) => !contactTagsList?.accountList.contactTagList.includes(tag),
);

await contactsAddTags({
variables: {
accountListId,
Expand All @@ -110,6 +120,7 @@ export const MassActionsAddTagsModal: React.FC<
query: ContactsDocument,
variables: { accountListId },
},
...(newTag ? [ContactFiltersDocument] : []),
],
});
enqueueSnackbar(t('Tags added to contacts!'), {
Expand Down
19 changes: 12 additions & 7 deletions src/components/Shared/Filters/FilterPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';
import Close from '@mui/icons-material/Close';
import DeleteIcon from '@mui/icons-material/Delete';
import ExpandMore from '@mui/icons-material/ExpandMore';
Expand Down Expand Up @@ -37,7 +37,6 @@ import {
ContactFilterSetInput,
ContactFilterStatusEnum,
FilterGroup,
MultiselectFilter,
ReportContactFilterSetInput,
ResultEnum,
TaskFilterSetInput,
Expand Down Expand Up @@ -590,11 +589,17 @@ export const FilterPanel: React.FC<FilterPanelProps & BoxProps> = ({
clearSelectedFilter();
};

const tagsFilters =
(
filters.find((filter) => filter?.filters[0]?.filterKey === 'tags')
?.filters[0] as MultiselectFilter
)?.options ?? [];
const tagsFilters = useMemo(() => {
const tags = filters.find(
(filter) => filter?.filters[0]?.filterKey === 'tags',
)?.filters[0];
if (tags?.__typename === 'MultiselectFilter' && tags.options) {
return [...tags.options].sort((tag1, tag2) =>
tag1.name.localeCompare(tag2.name),
);
}
return [];
}, [filter]);
const noSelectedFilters =
Object.keys(sanitizeFilters(selectedFilters)).length === 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { ContactFiltersDocument } from 'pages/accountLists/[accountListId]/conta
import { TaskFiltersDocument } from 'pages/accountLists/[accountListId]/tasks/Tasks.generated';
import { DeleteButton } from 'src/components/common/Modal/ActionButtons/ActionButtons';
import Modal from 'src/components/common/Modal/Modal';
import { useAccountListId } from 'src/hooks/useAccountListId';
import { useDeleteTagMutation } from './Chip/DeleteTag.generated';

interface FilterTagDeleteModalProps {
Expand All @@ -24,7 +23,6 @@ export const FilterTagDeleteModal: React.FC<FilterTagDeleteModalProps> = ({
const { t } = useTranslation();
const { route } = useRouter();
const [deleteTag, { loading: deleting }] = useDeleteTagMutation();
const accountListId = useAccountListId();
const { enqueueSnackbar } = useSnackbar();

const page = route.split('/')[3];
Expand All @@ -36,11 +34,7 @@ export const FilterTagDeleteModal: React.FC<FilterTagDeleteModalProps> = ({
page,
},
refetchQueries: [
{
query:
page === 'contacts' ? ContactFiltersDocument : TaskFiltersDocument,
variables: { accountListId },
},
page === 'contacts' ? ContactFiltersDocument : TaskFiltersDocument,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, how this was wasn't causing the data to refresh. Removing the variables makes it work though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it saw it as a different query

],
onCompleted: () => {
enqueueSnackbar(t('Tag deleted!'), {
Expand Down
Loading