Skip to content

Commit

Permalink
Adrienne / Editable PM list in Create/Edit buy ad page (deriv-com#5348)
Browse files Browse the repository at this point in the history
* Added autocomplete in input to make PM editable

* Added autocomplete in input to make PM editable

* Updated autocomplete list, placeholder for adding PM

* Updated autocomplete list, placeholder for adding PM

* Updated autocomplete list, placeholder for adding PM

* Refactored editable ad buy PM list to a component

* Fixed build issue

* Refactored variable name as per code review

* Incorporated code review changes

* Incorporated code review changes

* Incorporated code review changes

* Autocomplete dropdown is now top aligned

Co-authored-by: Yashim Wong <75345074+yashim-deriv@users.noreply.github.com>
Co-authored-by: Carol Sachdeva <58209918+carol-binary@users.noreply.github.com>
  • Loading branch information
3 people authored and vinu-deriv committed Jun 17, 2022
1 parent 12ac372 commit 2127371
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 285 deletions.
205 changes: 205 additions & 0 deletions packages/p2p/src/components/my-ads/buy-ad-payment-methods-list.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import * as React from 'react';
import { Formik, Field } from 'formik';
import { observer } from 'mobx-react-lite';
import { Autocomplete, Icon } from '@deriv/components';
import { useStores } from 'Stores';
import { localize } from 'Components/i18next';
import PropTypes from 'prop-types';
import './buy-ad-payment-methods-list.scss';

const BuyAdPaymentMethodsList = ({ selected_methods, setSelectedMethods }) => {
const { my_ads_store, my_profile_store } = useStores();
const [selected_edit_method, setSelectedEditMethod] = React.useState();
const [payment_methods_list, setPaymentMethodsList] = React.useState([]);

const MAX_PAYMENT_METHOD_SELECTION = 3;

React.useEffect(() => {
setPaymentMethodsList(
my_profile_store.payment_methods_list.filter(({ value }) => !selected_methods.includes(value))
);
}, [selected_methods]);

const onClickDeletePaymentMethodItem = value => {
if (value) {
my_ads_store.payment_method_names = my_ads_store.payment_method_names.filter(
payment_method_id => payment_method_id !== value
);
setSelectedMethods(selected_methods.filter(i => i !== value));
setPaymentMethodsList([
...payment_methods_list,
{
value,
text: my_profile_store.getPaymentMethodDisplayName(value),
},
]);
}
};

const onEditPaymentMethodItem = (value, index) => {
if (value && !my_ads_store.payment_method_names.includes(value)) {
const edited_methods = [...selected_methods];
edited_methods[index] = value;
my_ads_store.payment_method_names[index] = value;
setSelectedMethods(edited_methods);
setPaymentMethodsList([
...payment_methods_list.filter(payment_method => payment_method.value !== value),
selected_edit_method,
]);
}
};

const onClickPaymentMethodItem = value => {
if (value && !my_ads_store.payment_method_names.includes(value)) {
if (my_ads_store.payment_method_names.length < MAX_PAYMENT_METHOD_SELECTION) {
my_ads_store.payment_method_names.push(value);
setSelectedMethods([...selected_methods, value]);
setPaymentMethodsList(payment_methods_list.filter(payment_method => payment_method.value !== value));
}
}
};

const checkValidPaymentMethod = payment_method_text => {
return (
my_profile_store.payment_methods_list.find(payment_method => payment_method.text === payment_method_text)
?.value ?? false
);
};

if (selected_methods?.length > 0) {
return (
<div className='buy-ad-payment-methods-list__container'>
{selected_methods.map((payment_method, key) => {
const method = my_profile_store.getPaymentMethodDisplayName(payment_method);
const payment_method_icon = method.replace(' ', '');

return (
<Formik key={key} enableReinitialize initialValues={{ payment_method: method }}>
{({ setFieldValue }) => (
<Field name='payment_method'>
{({ field }) => (
<Autocomplete
{...field}
autoComplete='off' // prevent chrome autocomplete
className='quick-add-modal--input'
data-lpignore='true'
is_alignment_top
leading_icon={
<Icon
icon={
payment_method_icon === 'BankTransfer' ||
payment_method_icon === 'Other'
? `IcCashier${payment_method_icon}`
: 'IcCashierEwallet'
}
/>
}
list_items={payment_methods_list}
list_portal_id='deriv_app'
onBlur={e => {
e.preventDefault();
const value = checkValidPaymentMethod(e.target.value);
if (e.target.value === '') {
setFieldValue('payment_method', method);
} else if (!value) {
onClickDeletePaymentMethodItem(payment_method);
} else {
onEditPaymentMethodItem(value, key);
}
}}
onItemSelection={({ value }) => onEditPaymentMethodItem(value, key)}
onFocus={() => {
setSelectedEditMethod({ value: payment_method, text: method });
setFieldValue('payment_method', '');
}}
required
trailing_icon={
<Icon
icon='IcDelete'
onClick={() => onClickDeletePaymentMethodItem(payment_method)}
/>
}
type='text'
/>
)}
</Field>
)}
</Formik>
);
})}
{my_ads_store.payment_method_names.length < MAX_PAYMENT_METHOD_SELECTION &&
payment_methods_list.length > 0 && (
<Formik enableReinitialize initialValues={{ payment_method: '' }}>
{({ setFieldValue }) => (
<Field name='payment_method'>
{({ field }) => (
<div className='p2p-my-ads--border'>
<Autocomplete
{...field}
autoComplete='off' // prevent chrome autocomplete
className='quick-add-modal--input'
data-lpignore='true'
is_alignment_top
leading_icon={<Icon icon='IcAddOutline' size={14} />}
list_items={payment_methods_list}
list_portal_id='deriv_app'
onItemSelection={({ value }) => onClickPaymentMethodItem(value)}
onBlur={e => {
e.preventDefault();
setFieldValue('payment_method', '');
}}
placeholder={localize('Add')}
required
trailing_icon={<></>}
type='text'
/>
</div>
)}
</Field>
)}
</Formik>
)}
</div>
);
}

return (
<div className='buy-ad-payment-methods-list__container'>
<Formik enableReinitialize initialValues={{ payment_method: '' }}>
{({ setFieldValue }) => (
<Field name='payment_method'>
{({ field }) => (
<div className='p2p-my-ads--border'>
<Autocomplete
{...field}
autoComplete='off' // prevent chrome autocomplete
className='quick-add-modal--input'
data-lpignore='true'
is_alignment_top
leading_icon={<Icon icon='IcAddOutline' size={14} />}
list_items={payment_methods_list}
list_portal_id='deriv_app'
onItemSelection={({ text, value }) => {
setFieldValue('payment_method', value ? text : '');
onClickPaymentMethodItem(value);
}}
placeholder={localize('Add')}
required
trailing_icon={<></>}
type='text'
/>
</div>
)}
</Field>
)}
</Formik>
</div>
);
};

BuyAdPaymentMethodsList.propTypes = {
selected_methods: PropTypes.array,
setSelectedMethods: PropTypes.func,
};

export default observer(BuyAdPaymentMethodsList);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.buy-ad-payment-methods-list {
&__container {
max-width: 67.2rem;
}
}
143 changes: 3 additions & 140 deletions packages/p2p/src/components/my-ads/create-ad-form-payment-methods.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as React from 'react';
import { Formik, Field } from 'formik';
import { observer } from 'mobx-react-lite';
import { Autocomplete, Icon, Input, Text } from '@deriv/components';
import { useStores } from 'Stores';
import PaymentMethodCard from '../my-profile/payment-methods/payment-method-card';
import { localize, Localize } from 'Components/i18next';
import { localize } from 'Components/i18next';
import BuyAdPaymentMethodsList from './buy-ad-payment-methods-list';

const CreateAdFormPaymentMethods = ({ is_sell_advert, onSelectPaymentMethods }) => {
const { my_ads_store, my_profile_store } = useStores();
Expand All @@ -16,31 +15,6 @@ const CreateAdFormPaymentMethods = ({ is_sell_advert, onSelectPaymentMethods })
borderWidth: '2px',
};

const onClickDeletePaymentMethodItem = value => {
if (value) {
my_ads_store.payment_method_names = my_ads_store.payment_method_names.filter(
payment_method_id => payment_method_id !== value
);
setSelectedBuyMethods(selected_buy_methods.filter(i => i !== value));
}
};

const onClickPaymentMethodItem = value => {
if (value) {
if (!my_ads_store.payment_method_names.includes(value)) {
if (my_ads_store.payment_method_names.length < 3) {
my_ads_store.payment_method_names.push(value);
setSelectedBuyMethods([...selected_buy_methods, value]);
}
} else {
my_ads_store.payment_method_names = my_ads_store.payment_method_names.filter(
payment_method_id => payment_method_id !== value
);
setSelectedBuyMethods(selected_buy_methods.filter(i => i !== value));
}
}
};

const onClickPaymentMethodCard = payment_method => {
if (!my_ads_store.payment_method_ids.includes(payment_method.ID)) {
if (my_ads_store.payment_method_ids.length < 3) {
Expand Down Expand Up @@ -107,120 +81,9 @@ const CreateAdFormPaymentMethods = ({ is_sell_advert, onSelectPaymentMethods })
/>
);
}
if (selected_buy_methods?.length > 0) {
return (
<React.Fragment>
{selected_buy_methods.map((payment_method, key) => {
const method = my_profile_store.getPaymentMethodDisplayName(payment_method);
const payment_method_icon = method.replace(' ', '');

return (
<Formik key={key} enableReinitialize initialValues={{}}>
<Field name='payment_method'>
{({ field }) => (
<Input
{...field}
className='quick-add-modal--input'
leading_icon={
<Icon
icon={
payment_method_icon === 'BankTransfer' ||
payment_method_icon === 'Other'
? `IcCashier${payment_method_icon}`
: 'IcCashierEwallet'
}
/>
}
trailing_icon={
<Icon
icon='IcDelete'
onClick={() => {
onClickDeletePaymentMethodItem(payment_method);
}}
/>
}
type='text'
value={method}
/>
)}
</Field>
</Formik>
);
})}
{my_ads_store.payment_method_names.length < 3 && (
<Formik enableReinitialize initialValues={{ payment_method: '' }}>
{({ resetForm }) => (
<Field name='payment_method'>
{({ field }) => (
<div className='p2p-my-ads--border'>
<Autocomplete
{...field}
autoComplete='off' // prevent chrome autocomplete
data-lpignore='true'
has_updating_list={false}
is_alignment_top
label={
<React.Fragment>
<Icon icon='IcAddCircle' size={14} />
<Text color='less-prominent' size='xs'>
<Localize i18n_default_text='Add' />
</Text>
</React.Fragment>
}
list_items={my_profile_store.payment_methods_list}
list_portal_id='deriv_app'
onItemSelection={({ value }) => {
onClickPaymentMethodItem(value);
resetForm();
}}
required
trailing_icon={<></>}
type='text'
/>
</div>
)}
</Field>
)}
</Formik>
)}
</React.Fragment>
);
}

return (
<Formik enableReinitialize initialValues={{ payment_method: '' }}>
{({ resetForm }) => (
<Field name='payment_method'>
{({ field }) => (
<div className='p2p-my-ads--border'>
<Autocomplete
{...field}
autoComplete='off' // prevent chrome autocomplete
data-lpignore='true'
has_updating_list={false}
is_alignment_top
label={
<React.Fragment>
<Icon icon='IcAddCircle' size={14} />
<Text color='less-prominent' size='xs'>
<Localize i18n_default_text='Add' />
</Text>
</React.Fragment>
}
list_items={my_profile_store.payment_methods_list}
onItemSelection={({ value }) => {
onClickPaymentMethodItem(value);
resetForm();
}}
required
trailing_icon={<></>}
type='text'
/>
</div>
)}
</Field>
)}
</Formik>
<BuyAdPaymentMethodsList selected_methods={selected_buy_methods} setSelectedMethods={setSelectedBuyMethods} />
);
};

Expand Down
Loading

0 comments on commit 2127371

Please sign in to comment.