Skip to content

Commit

Permalink
Nada/88951/change chat url (binary-com#7798)
Browse files Browse the repository at this point in the history
* fix: get chat url from p2p_order_create instead of p2p_chat_create

* fix: fixed issue with order details page

* fix: chat channel url empty issue fixed for p2p_order_create response

* fix: function added to store

* fix: added timeout for setting chat_channel in order details

* fix: pr comments fixed

* fix: increased time out

* fix: removed delay for going to details page from list
  • Loading branch information
nada-deriv committed Mar 21, 2023
1 parent ce96993 commit 4e7bd37
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
22 changes: 18 additions & 4 deletions packages/p2p/src/components/order-details/order-details.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useModalManagerContext } from 'Components/modal-manager/modal-manager-c
import 'Components/order-details/order-details.scss';

const OrderDetails = observer(() => {
const { general_store, my_profile_store, order_store, sendbird_store } = useStores();
const { general_store, my_profile_store, order_store, sendbird_store, buy_sell_store } = useStores();
const { hideModal, showModal, useRegisterModalProps } = useModalManagerContext();

const {
Expand Down Expand Up @@ -85,10 +85,22 @@ const OrderDetails = observer(() => {
order_store.setIsRecommended(undefined);
my_profile_store.getPaymentMethodsList();

if (order_channel_url) {
sendbird_store.setChatChannelUrl(order_channel_url);
const handleChatChannelCreation = () => {
if (order_channel_url) {
sendbird_store.setChatChannelUrl(order_channel_url);
} else {
sendbird_store.createChatForNewOrder(order_store.order_id);
}
};

// TODO: remove condition check and settimeout once access chat_channel_url from p2p_order_create is activated in BO, since chat channel url response is always delayed
// Added delay only for first time order creation, since response is delayed. To be removed after feature release.
if (buy_sell_store.is_create_order_subscribed) {
setTimeout(() => {
handleChatChannelCreation();
}, 1250);
} else {
sendbird_store.createChatForNewOrder(order_store.order_id);
handleChatChannelCreation();
}

return () => {
Expand All @@ -102,6 +114,8 @@ const OrderDetails = observer(() => {
redirectToOrderDetails: general_store.redirectToOrderDetails,
setIsRatingModalOpen: is_open => (is_open ? showRatingModal : hideModal),
});
buy_sell_store.setIsCreateOrderSubscribed(false);
buy_sell_store.unsubscribeCreateOrder();
};
}, []); // eslint-disable-line react-hooks/exhaustive-deps

Expand Down
60 changes: 45 additions & 15 deletions packages/p2p/src/stores/buy-sell-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { formatMoney, getDecimalPlaces, isMobile } from '@deriv/shared';
import { Text } from '@deriv/components';
import { localize } from 'Components/i18next';
import { buy_sell } from 'Constants/buy-sell';
import { requestWS } from 'Utils/websocket';
import { requestWS, subscribeWS } from 'Utils/websocket';
import { textValidator, lengthValidator } from 'Utils/validations';
import { countDecimalPlaces } from 'Utils/string';
import { removeTrailingZeros } from 'Utils/format-value';
Expand Down Expand Up @@ -41,6 +41,7 @@ export default class BuySellStore extends BaseStore {
submitForm = () => {};
table_type = buy_sell.BUY;
form_props = {};
is_create_order_subscribed = false;

initial_values = {
amount: this.advert?.min_order_amount_limit,
Expand Down Expand Up @@ -84,6 +85,7 @@ export default class BuySellStore extends BaseStore {
submitForm: observable,
table_type: observable,
form_props: observable,
is_create_order_subscribed: observable,
account_currency: computed,
advert: computed,
has_payment_info: computed,
Expand Down Expand Up @@ -141,9 +143,13 @@ export default class BuySellStore extends BaseStore {
validatePopup: action.bound,
sort_list: computed,
fetchAdvertiserAdverts: action.bound,
handleResponse: action.bound,
setIsCreateOrderSubscribed: action.bound,
});
}

create_order_subscription = {};

get account_currency() {
return this.advert?.account_currency;
}
Expand Down Expand Up @@ -260,6 +266,33 @@ export default class BuySellStore extends BaseStore {
this.setIsSortDropdownOpen(false);
}

handleResponse = async order => {
const { sendbird_store, order_store, general_store, floating_rate_store } = this.root_store;
const { setErrorMessage, handleConfirm, handleClose } = this.form_props;
if (order.error) {
setErrorMessage(order.error.message);
this.setFormErrorCode(order.error.code);
} else {
if (order?.subscription?.id && !this.is_create_order_subscribed) {
this.setIsCreateOrderSubscribed(true);
}
setErrorMessage(null);
general_store.hideModal();
floating_rate_store.setIsMarketRateChanged(false);
sendbird_store.setChatChannelUrl(order?.p2p_order_create?.chat_channel_url ?? '');
if (order?.p2p_order_create?.id) {
const response = await requestWS({ p2p_order_info: 1, id: order?.p2p_order_create?.id });
handleConfirm(response?.p2p_order_info);
}
handleClose();
this.payment_method_ids = [];
}
if (order?.p2p_order_info?.id && order?.p2p_order_info?.chat_channel_url) {
sendbird_store.setChatChannelUrl(order?.p2p_order_info?.chat_channel_url ?? '');
order_store.setOrderDetails(order);
}
};

handleSubmit = async (isMountedFn, values, { setSubmitting }) => {
if (isMountedFn()) {
setSubmitting(true);
Expand All @@ -284,20 +317,7 @@ export default class BuySellStore extends BaseStore {
payload.rate = values.rate;
}

const order = await requestWS({ ...payload });

if (order.error) {
this.form_props.setErrorMessage(order.error.message);
this.setFormErrorCode(order.error.code);
} else {
this.form_props.setErrorMessage(null);
this.root_store.general_store.hideModal();
this.root_store.floating_rate_store.setIsMarketRateChanged(false);
const response = await requestWS({ p2p_order_info: 1, id: order.p2p_order_create.id });
this.form_props.handleConfirm(response.p2p_order_info);
this.form_props.handleClose();
this.payment_method_ids = [];
}
this.create_order_subscription = subscribeWS({ ...payload }, [this.handleResponse]);

if (isMountedFn()) {
setSubmitting(false);
Expand Down Expand Up @@ -602,6 +622,10 @@ export default class BuySellStore extends BaseStore {
this.submitForm = submitFormFn;
}

setIsCreateOrderSubscribed(is_create_order_subscribed) {
this.is_create_order_subscribed = is_create_order_subscribed;
}

showAdvertiserPage(selected_advert) {
this.setSelectedAdState(selected_advert);
this.setShowAdvertiserPage(true);
Expand Down Expand Up @@ -720,4 +744,10 @@ export default class BuySellStore extends BaseStore {

return () => disposeAdvertIntervalReaction();
}

unsubscribeCreateOrder = () => {
if (this.create_order_subscription.unsubscribe) {
this.create_order_subscription.unsubscribe();
}
};
}
5 changes: 4 additions & 1 deletion packages/p2p/src/stores/order-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,13 @@ export default class OrderStore {
}

onOrderIdUpdate() {
const { buy_sell_store } = this.root_store;
this.unsubscribeFromCurrentOrder();

if (this.order_id) {
this.subscribeToCurrentOrder();
if (!buy_sell_store.is_create_order_subscribed) {
this.subscribeToCurrentOrder();
}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/p2p/src/stores/sendbird-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default class SendbirdStore extends BaseStore {
this.chat_messages.push(chat_message);
}

// TODO: remove when access chat_channel_url from p2p_order_create is activated in BO
createChatForNewOrder(id) {
if (!this.chat_channel_url) {
// If order_information doesn't have chat_channel_url this is a new order
Expand Down

0 comments on commit 4e7bd37

Please sign in to comment.