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

Search by label added in template Screen #2449

Merged
merged 4 commits into from
Jul 24, 2023
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
@@ -1,8 +1,8 @@
import { MockedProvider } from '@apollo/client/testing';
import { render, waitFor } from '@testing-library/react';
import { AddAutoComplete } from './AddAutoComplete';
import { CreateAutoComplete } from './CreateAutoComplete';
import { CREATE_LABEL } from 'graphql/mutations/Tags';
describe('<AddAutoComplete />', () => {
describe('<CreateAutoComplete />', () => {
const mocks = [
{
request: {
Expand Down Expand Up @@ -39,10 +39,10 @@ describe('<AddAutoComplete />', () => {
form: { dirty: false, touched: false, errors: false, setFieldValue: mockHandleChange },
};

it('renders <AddAutoComplete /> component', () => {
it('renders <CreateAutoComplete /> component', () => {
const container = render(
<MockedProvider mocks={mocks}>
<AddAutoComplete {...defaultProps} />
<CreateAutoComplete {...defaultProps} />
</MockedProvider>
);
waitFor(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { useMutation } from '@apollo/client';
import { AutoComplete, AutocompleteProps } from '../AutoComplete/AutoComplete';
import { CREATE_LABEL } from 'graphql/mutations/Tags';

export interface AddAutoCompleteProp extends AutocompleteProps {}
export const AddAutoComplete = ({ ...rest }: AddAutoCompleteProp) => {
export interface CreateAutoCompleteProp extends AutocompleteProps {}

export const CreateAutoComplete = ({ ...rest }: CreateAutoCompleteProp) => {
const [createTag] = useMutation(CREATE_LABEL);

const handleCreateItem = async (value: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/containers/Flow/Flow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const Flow = () => {
multiple: false,
textFieldProps: {
variant: 'outlined',
label: t('Label'),
label: t('Tag'),
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/containers/Flow/FlowList/FlowList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export const FlowList = () => {
const tagFilter = (
<AutoComplete
isFilterType
placeholder="Select label"
placeholder="Select tag"
options={tag ? tag.tags : []}
optionLabel="label"
disabled={false}
Expand Down
39 changes: 21 additions & 18 deletions src/containers/InteractiveMessage/InteractiveMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
validator,
} from './InteractiveMessage.helper';
import { GET_TAGS } from 'graphql/queries/Tags';
import { AddAutoComplete } from 'components/UI/Form/AddAutoComplete/AddAutoComplete';
import { CreateAutoComplete } from 'components/UI/Form/CreateAutoComplete/CreateAutoComplete';

const interactiveMessageIcon = (
<InteractiveMessageIcon className={styles.Icon} data-testid="interactive-icon" />
Expand Down Expand Up @@ -535,22 +535,6 @@ export const InteractiveMessage = () => {
},
onGlobalButtonInputChange: (value: string) => setGlobalButton(value),
},
{
component: AddAutoComplete,
name: 'tagId',
options: tag ? tag.tags : [],
optionLabel: 'label',
disabled: false,
hasCreateOption: true,
multiple: false,
onChange: (value: any) => {
setTagId(value);
},
textFieldProps: {
variant: 'outlined',
label: t('Label'),
},
},
];

const getTemplateButtonPayload = (typeVal: string, buttons: Array<any>) => {
Expand Down Expand Up @@ -718,7 +702,26 @@ export const InteractiveMessage = () => {
},
];

const formFields = templateType === LIST ? [...fields] : [...fields, ...attachmentInputs];
let formFields: any = templateType === LIST ? [...fields] : [...fields, ...attachmentInputs];
formFields = [
...formFields,
{
component: CreateAutoComplete,
name: 'tagId',
options: tag ? tag.tags : [],
optionLabel: 'label',
disabled: false,
hasCreateOption: true,
multiple: false,
onChange: (value: any) => {
setTagId(value);
},
textFieldProps: {
variant: 'outlined',
label: t('Tag'),
},
},
];

const validation = validator(templateType, t);
const validationScheme = Yup.object().shape(validation, [['type', 'attachmentURL']]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const InteractiveMessageList = () => {
const tagFilter = (
<AutoComplete
isFilterType
placeholder="Select label"
placeholder="Select tag"
options={tag ? tag.tags : []}
optionLabel="label"
disabled={false}
Expand Down
1 change: 1 addition & 0 deletions src/containers/Template/Form/HSM/HSM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const HSM = () => {
media: {},
body: '',
});

const [shortcode, setShortcode] = useState('');
const [example, setExample] = useState(EditorState.createEmpty());
const [category, setCategory] = useState<any>({ label: '', id: '' });
Expand Down
34 changes: 32 additions & 2 deletions src/containers/Template/Form/Template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import { LanguageBar } from 'components/UI/LanguageBar/LanguageBar';
import { GET_TEMPLATE, FILTER_TEMPLATES } from 'graphql/queries/Template';
import { CREATE_MEDIA_MESSAGE } from 'graphql/mutations/Chat';
import { USER_LANGUAGES } from 'graphql/queries/Organization';
import { GET_TAGS } from 'graphql/queries/Tags';
import { CREATE_TEMPLATE, UPDATE_TEMPLATE, DELETE_TEMPLATE } from 'graphql/mutations/Template';
import { MEDIA_MESSAGE_TYPES, CALL_TO_ACTION, QUICK_REPLY } from 'common/constants';
import { getPlainTextFromEditor, getEditorFromContent } from 'common/RichEditor';
import Loading from 'components/UI/Layout/Loading/Loading';
import { CreateAutoComplete } from 'components/UI/Form/CreateAutoComplete/CreateAutoComplete';
import { validateMedia } from 'common/utils';
import styles from './Template.module.css';

Expand Down Expand Up @@ -171,6 +173,7 @@ const Template = ({
? options.filter(({ label }) => label !== 'AUDIO' && label !== 'STICKER')
: options;

const [tagId, setTagId] = useState<any>(null);
const [label, setLabel] = useState('');
const [body, setBody] = useState(EditorState.createEmpty());
const [example, setExample] = useState(EditorState.createEmpty());
Expand All @@ -196,6 +199,11 @@ const Template = ({
const location: any = useLocation();
const params = useParams();

const { data: tag } = useQuery(GET_TAGS, {
variables: {},
fetchPolicy: 'network-only',
});

const states = {
language,
label,
Expand All @@ -205,6 +213,7 @@ const Template = ({
shortcode,
example,
category,
tagId,
isActive,
templateButtons,
isAddButtonChecked,
Expand All @@ -221,6 +230,7 @@ const Template = ({
MessageMedia: MessageMediaValue,
shortcode: shortcodeValue,
category: categoryValue,
tag: tagIdValue,
buttonType: templateButtonType,
buttons,
hasButtons,
Expand Down Expand Up @@ -302,6 +312,9 @@ const Template = ({
if (categoryValue) {
setCategory({ label: categoryValue, id: categoryValue });
}
if (tagIdValue) {
setTagId(tagIdValue);
}
};

const updateStates = ({
Expand Down Expand Up @@ -731,14 +744,31 @@ const Template = ({
},
];

const tags = {
component: CreateAutoComplete,
name: 'tagId',
options: tag ? tag.tags : [],
optionLabel: 'label',
disabled: false,
hasCreateOption: true,
multiple: false,
onChange: (value: any) => {
setTagId(value);
},
textFieldProps: {
variant: 'outlined',
label: t('Tag'),
},
};

const hsmFields = formField && [
...formField.slice(0, 1),
...templateRadioOptions,
...formField.slice(1),
];

const fields = defaultAttribute.isHsm
? [formIsActive, ...formFields, ...hsmFields, ...attachmentField]
? [formIsActive, ...formFields, ...hsmFields, ...attachmentField, tags]
: [...formFields, ...attachmentField];

// Creating payload for button template
Expand Down Expand Up @@ -849,7 +879,7 @@ const Template = ({
}
if (payloadCopy.isHsm) {
payloadCopy.category = payloadCopy.category.label;

payloadCopy.tagId = payload.tagId.id;
if (isAddButtonChecked && templateType) {
const templateButtonData = getButtonTemplatePayload();
Object.assign(payloadCopy, { ...templateButtonData });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { setUserSession } from 'services/AuthService';
import { SpeedSendList } from './SpeedSendList';

afterEach(cleanup);
const mocks = TEMPLATE_MOCKS;
const mocks = [...TEMPLATE_MOCKS, ...TEMPLATE_MOCKS];

const speedSend = (
<MockedProvider mocks={mocks} addTypename={false}>
Expand Down
3 changes: 2 additions & 1 deletion src/containers/Template/List/Template.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const hsmProps: any = {
};

describe('HSM templates', () => {
const hsmMocks = [...HSM_LIST, bulkApplyMutation];
const hsmMocks = [...HSM_LIST, ...HSM_LIST, bulkApplyMutation];
const hsmComponent = (
<Router>
<MockedProvider mocks={hsmMocks} addTypename={false}>
Expand Down Expand Up @@ -142,6 +142,7 @@ describe('HSM templates', () => {

describe('Provider: Gupshup enterprise', () => {
const hsmMocks = [
...HSM_LIST,
...HSM_LIST,
getOrganizationBSP,
importTemplateMutation,
Expand Down
42 changes: 40 additions & 2 deletions src/containers/Template/List/Template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
import { Checkbox, FormControlLabel } from '@mui/material';

import { List } from 'containers/List/List';
import { useMutation } from '@apollo/client';
import { useMutation, useQuery } from '@apollo/client';
import { WhatsAppToJsx } from 'common/RichEditor';
import { DATE_TIME_FORMAT, GUPSHUP_ENTERPRISE_SHORTCODE } from 'common/constants';
import {
Expand All @@ -29,6 +29,8 @@ import { setNotification } from 'common/notification';
import { BULK_APPLY_SAMPLE_LINK } from 'config';
import styles from './Template.module.css';
import { CopyAllOutlined } from '@mui/icons-material';
import { GET_TAGS } from 'graphql/queries/Tags';
import { AutoComplete } from 'components/UI/Form/AutoComplete/AutoComplete';

const getLabel = (label: string) => <div className={styles.LabelText}>{label}</div>;

Expand Down Expand Up @@ -80,11 +82,16 @@ export const Template = ({
const { t } = useTranslation();

const { provider } = useContext(ProviderContext);

const [selectedTag, setSelectedTag] = useState<any>(null);
const [importing, setImporting] = useState(false);

const [filters, setFilters] = useState<any>({ ...statusFilter, APPROVED: true });

const { data: tag } = useQuery(GET_TAGS, {
variables: {},
fetchPolicy: 'network-only',
});

const [importTemplatesMutation] = useMutation(IMPORT_TEMPLATES, {
onCompleted: (data: any) => {
setImporting(false);
Expand Down Expand Up @@ -344,6 +351,36 @@ export const Template = ({
button.show = false;
}

// OnChange handler for the dropdown
const handleDropdownChange = (event: any) => {
setSelectedTag(event.target.value);
};

const tagFilter = (
<AutoComplete
isFilterType
placeholder="Select tag"
options={tag ? tag.tags : []}
optionLabel="label"
disabled={false}
hasCreateOption={false}
multiple={false}
onChange={(value: any) => {
setSelectedTag(value);
}}
form={{ setFieldValue: handleDropdownChange }}
field={{
value: selectedTag,
onChange: handleDropdownChange,
}}
/>
);

appliedFilters = {
...appliedFilters,
...(selectedTag?.id && { tagIds: [parseInt(selectedTag?.id)] }),
};

return (
<List
secondaryButton={secondaryButton}
Expand All @@ -362,6 +399,7 @@ export const Template = ({
filterList={isHSM && filterTemplateStatus}
collapseOpen={open}
collapseRow={Id}
filterDropdowm={isHSM && tagFilter}
/>
);
};
Expand Down
23 changes: 23 additions & 0 deletions src/containers/Template/Template.test.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'mocks/Organization';
import { templateCountQuery } from 'mocks/Template';
import { CREATE_MEDIA_MESSAGE } from 'graphql/mutations/Chat';
import { GET_TAGS } from 'graphql/queries/Tags';

const count = templateCountQuery(false, 2);

Expand Down Expand Up @@ -268,6 +269,28 @@ const getTemplateData = {
};

export const TEMPLATE_MOCKS = [
{
request: {
query: GET_TAGS,
variables: {},
},
result: {
data: {
tags: [
{
__typename: 'Tag',
id: '1',
label: 'Messages',
},
{
__typename: 'Tag',
id: '2',
label: 'Contacts',
},
],
},
},
},
{
request: {
query: CREATE_TEMPLATE,
Expand Down
4 changes: 4 additions & 0 deletions src/graphql/queries/Template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export const GET_TEMPLATE = gql`
caption
sourceUrl
}
tag {
id
label
}
category
shortcode
example
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/en/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,11 @@
"Opened by": "Opened by",
"Change assignee": "Change assignee",
"Select Tag": "Select Tag",
"Select Label": "Select Label",
"Select tag": "Select tag",
"View profile": "View profile",
"Copy UUID": "Copy UUID",
"Org Id": "Org Id",
"Tag": "Tag",
"Export collection": "Export collection",
"Sheet type": "Sheet type"
}