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

added dynamic links to buttons #3180

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 25 additions & 4 deletions src/containers/HSM/HSM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export const HSM = () => {
const [validatingURL, setValidatingURL] = useState<boolean>(false);
const [isUrlValid, setIsUrlValid] = useState<any>();
const [templateType, setTemplateType] = useState<string | null>(CALL_TO_ACTION);
const [dynamicUrlParams, setDynamicUrlParams] = useState<any>({
urlType: 'Static',
sampleSuffix: '',
});
const [sampleMessages, setSampleMessages] = useState({
type: 'TEXT',
location: null,
Expand Down Expand Up @@ -160,15 +164,25 @@ export const HSM = () => {
};

// Creating payload for button template
const getButtonTemplatePayload = () => {
const buttons = templateButtons.reduce((result: any, button) => {
const getButtonTemplatePayload = (urlType: string, sampleSuffix: string) => {
const buttons = templateButtons.reduce((result: any, button: any) => {
const { type: buttonType, value, title }: any = button;

if (templateType === CALL_TO_ACTION) {
const typeObj: any = {
phone_number: 'PHONE_NUMBER',
url: 'URL',
};
const obj: any = { type: typeObj[buttonType], text: title, [buttonType]: value };
let obj: any = { type: typeObj[buttonType], text: title, [buttonType]: value };

if (buttonType === 'url' && urlType === 'Dynamic') {
obj = {
type: typeObj[buttonType],
text: title,
[buttonType]: `${value}{{1}}`,
example: [`${value}${sampleSuffix}`],
};
}
result.push(obj);
}

Expand All @@ -192,6 +206,10 @@ export const HSM = () => {
};
};

const handleDynamicParamsChange = (value: any) => {
setDynamicUrlParams(value);
};

const setStates = ({
isActive: isActiveValue,
language: languageIdValue,
Expand Down Expand Up @@ -269,6 +287,7 @@ export const HSM = () => {

const setPayload = (payload: any) => {
let payloadCopy = { ...payload, isHsm: true };
const { urlType, sampleSuffix } = dynamicUrlParams;
if (isEditing) {
payloadCopy.shortcode = payloadCopy.newShortcode;
} else {
Expand All @@ -278,7 +297,7 @@ export const HSM = () => {
payloadCopy.languageId = payload.language.id;
payloadCopy.example = getExampleFromBody(payloadCopy.body, variables);
if (isAddButtonChecked && templateType) {
const templateButtonData = getButtonTemplatePayload();
const templateButtonData = getButtonTemplatePayload(urlType, sampleSuffix);
Object.assign(payloadCopy, { ...templateButtonData });
}
if (payloadCopy.type) {
Expand Down Expand Up @@ -515,6 +534,8 @@ export const HSM = () => {
onRemoveClick: removeTemplateButtons,
onInputChange: handeInputChange,
onTemplateTypeChange: handleTemplateTypeChange,
dynamicUrlParams,
onDynamicParamsChange: handleDynamicParamsChange,
},
{
component: AutoComplete,
Expand Down
25 changes: 20 additions & 5 deletions src/containers/TemplateOptions/TemplateOptions.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

.CallToActionWrapper {
composes: Font;
display: flex;
flex-direction: column;
gap: 0.5rem;
}

.CallToActionWrapper > div {
Expand Down Expand Up @@ -42,7 +45,7 @@

.RadioStyles {
composes: Font;
margin-bottom: 12px;
margin-bottom: 0.5rem;
}

.RadioLabel > span:last-child {
Expand All @@ -57,10 +60,6 @@
display: block !important;
}

.FormControl {
margin-bottom: 17px;
}

.FormControl > p {
margin-left: 12px;
}
Expand Down Expand Up @@ -115,3 +114,19 @@
.Button {
margin-top: 7px;
}

.FieldLabel {
font-weight: 500;
font-size: 16px;
line-height: 18px;
color: #555555;
padding-bottom: 14px;
}

.DefaultInputRoot {
background-color: #ffffff;
}

.StartAdornment {
padding-right: 0.5rem;
}
69 changes: 65 additions & 4 deletions src/containers/TemplateOptions/TemplateOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { RadioGroup, FormControlLabel, Radio, TextField, FormHelperText, FormControl } from '@mui/material';
import {
RadioGroup,
FormControlLabel,
Radio,
TextField,
FormHelperText,
FormControl,
Autocomplete,
Typography,
} from '@mui/material';
import { FieldArray } from 'formik';

import { Button } from 'components/UI/Form/Button/Button';
Expand All @@ -14,31 +23,36 @@
isAddButtonChecked: boolean;
templateType: string | null;
inputFields: Array<any>;
form: { touched: any; errors: any; values: any };
form: { touched: any; errors: any; values: any; setFieldValue: any };
onAddClick: any;
onRemoveClick: any;
onInputChange: any;
onTemplateTypeChange: any;
disabled: any;
dynamicUrlParams?: any;
onDynamicParamsChange?: any;
}
export const TemplateOptions = ({
isAddButtonChecked,
templateType,
inputFields,
form: { touched, errors, values },
form: { touched, errors, values, setFieldValue },
onAddClick,
onRemoveClick,
onTemplateTypeChange,
onInputChange,
disabled = false,
dynamicUrlParams,
onDynamicParamsChange,
}: TemplateOptionsProps) => {
const buttonTitle = 'Button Title';
const buttonValue = 'Button Value';
const buttonTitles: any = {
CALL_TO_ACTION: 'Call to action',
QUICK_REPLY: 'Quick Reply',
};

const options = ['Static', 'Dynamic'];
const { urlType, sampleSuffix } = dynamicUrlParams;
const handleAddClick = (helper: any, type: boolean) => {
const obj = type ? { type: '', value: '', title: '' } : { value: '' };
helper.push(obj);
Expand All @@ -54,7 +68,7 @@
const title = templateType ? buttonTitles[templateType] : '';
const buttonClass = templateType === QUICK_REPLY ? styles.QuickReplyAddButton : styles.CallToActionAddButton;
return (
<Button

Check failure on line 71 in src/containers/TemplateOptions/TemplateOptions.tsx

View workflow job for this annotation

GitHub Actions / CI

src/containers/TemplateOptions/TemplateOptions.test.tsx > it renders component and selects call to action type

TypeError: Cannot destructure property 'urlType' of '((cov_6azyxwbl6(...).s[5]++) , dynamicUrlParams)' as it is undefined. ❯ TemplateOptions src/containers/TemplateOptions/TemplateOptions.tsx:71:8 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22

Check failure on line 71 in src/containers/TemplateOptions/TemplateOptions.tsx

View workflow job for this annotation

GitHub Actions / CI

src/containers/TemplateOptions/TemplateOptions.test.tsx > it renders call to action button template successfully

TypeError: Cannot destructure property 'urlType' of '((cov_6azyxwbl6(...).s[5]++) , dynamicUrlParams)' as it is undefined. ❯ TemplateOptions src/containers/TemplateOptions/TemplateOptions.tsx:71:8 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22

Check failure on line 71 in src/containers/TemplateOptions/TemplateOptions.tsx

View workflow job for this annotation

GitHub Actions / CI

src/containers/TemplateOptions/TemplateOptions.test.tsx > it renders quick reply button template successfully

TypeError: Cannot destructure property 'urlType' of '((cov_6azyxwbl6(...).s[5]++) , dynamicUrlParams)' as it is undefined. ❯ TemplateOptions src/containers/TemplateOptions/TemplateOptions.tsx:71:8 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22
className={buttonClass}
variant="outlined"
color="primary"
Expand Down Expand Up @@ -133,6 +147,23 @@
) : null}
</div>
</div>
{type === 'url' && (
<div className={styles.TextFieldWrapper}>
<Autocomplete
options={options}
classes={{ inputRoot: styles.DefaultInputRoot }}
renderInput={(params) => <TextField {...params} label="Select URL Type" />}
clearIcon={false}
value={urlType}
onChange={(event: any, newValue: string | null) => {
onDynamicParamsChange({
...dynamicUrlParams,
urlType: newValue,
});
}}
/>
</div>
)}
<div className={styles.TextFieldWrapper} data-testid="buttonTitle">
<FormControl fullWidth error={isError('title')} className={styles.FormControl}>
<TextField
Expand Down Expand Up @@ -167,7 +198,37 @@
) : null}
</FormControl>
</div>
{urlType === 'Dynamic' && type === 'url' && (
<div>
<FormControl fullWidth error={isError('title')} className={styles.FormControl}>
<TextField
disabled={disabled}
label={'Sample Suffix'}
className={styles.TextField}
slotProps={{
input: {
startAdornment: (
<Typography
variant="body2"
color="textSecondary"
className={styles.StartAdornment}
>{`{{1}}`}</Typography>
),
},
}}
onChange={(event) =>
onDynamicParamsChange({
...dynamicUrlParams,
sampleSuffix: event.target.value,
})
}
value={sampleSuffix}
/>
</FormControl>
</div>
)}
</div>

<div className={styles.Button}>
{inputFields.length === index + 1 && inputFields.length !== 2 ? addButton(arrayHelpers, true) : null}
</div>
Expand Down
Loading