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

GRWT-3133 / Kate / [DTrader-V2] Tech Debt: Refactor Stake component [WIP] #17620

Open
wants to merge 25 commits into
base: master
Choose a base branch
from

Conversation

kate-deriv
Copy link
Contributor

@kate-deriv kate-deriv commented Nov 21, 2024

Changes:

Stake validation refactoring:
Instead of executing onChange function from trade store (that will change store values and send new proposal with them) when user is typing, we are going to use useDtraderQuery hook without subscription. It will allow us to validate the new values + we can extract more information from response. Because we are not subscribing, the store values won't be changed and we won't form a new proposal request with subscription. That will also allow us to restore original values in case of page reload & clicking on back arrow & closing without saving, so there is no need in stake in v2_params_initial_values. Only when user clicks on 'Save' button we'll call onChange.

Stake error snackbar refactoring:
Refactoring the logic of snackbar executing should include removing it outside of the Stake component.
Current problems:
Each trade param has it's own logic for triggering a snackbar (code duplications)
Because at the same moment we have 2 instances of the same component at the page (e.g. stake big and stake minimized), BOTH of them triggers snackbar. As a result there are a lot of bugs, where same error is shown twice.
Suggested:
To create a component for trade param error snackbar (TradeParamErrorSnackbar) and keep all the logic of triggering/showing the snackbar there. Not in multiple places across all the trade params.
To render this component in top level, in trade.tsx for e.g. In this case, even if we have 2 instances of the same component with the same error, we will have only one snackbar.
To create a hook (useTradeParamError), which will incapsulate the logic of errors checking. We will just pass fields, which it should track (e.g. ['take_profit', 'stop_loss', 'stake']) and get from hook info if this field has error and what is the error text.

What was done, short:

  • Added useDtraderQuery hook usage in Stake component (mainly for validation)
  • Reduced the usage of onChange function from trade store. It will be called only on 'Save' button click.
  • Not allow user to Save the value before the validation from API comes
  • Removed stake from v2_params_initial_values
  • Removed error snackbar logic outside of the Stake component. Now TradeParamErrorSnackbar is handling it + a helper-hook for data transformation
  • Covered changes with tests

Copy link

vercel bot commented Nov 21, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
deriv-app ✅ Ready (Inspect) Visit Preview Nov 29, 2024 9:27am

Copy link
Contributor

github-actions bot commented Nov 21, 2024

A production App ID was automatically generated for this PR. (log)

Click here to copy & paste above information.
- **PR**: [https://github.com/deriv-com/deriv-app/pull/17620](https://github.com/deriv-com/deriv-app/pull/17620)
- **URLs**:
    - **w/ App ID + Server**: https://deriv-app-git-fork-kate-deriv-kate-dtra-2396stakerefactor.binary.sx?qa_server=red.derivws.com&app_id=32920
    - **Original**: https://deriv-app-git-fork-kate-deriv-kate-dtra-2396stakerefactor.binary.sx
- **App ID**: `32920`

Copy link
Contributor

github-actions bot commented Nov 21, 2024

🚨 Lighthouse report for the changes in this PR:

Category Score
🔺 Performance 40
🟧 Accessibility 70
🟢 Best practices 92
🟧 SEO 77
🟧 PWA 78

Lighthouse ran with https://deriv-app-git-fork-kate-deriv-kate-dtra-2396stakerefactor.binary.sx/

@coveralls
Copy link

coveralls commented Nov 21, 2024

Pull Request Test Coverage Report for Build 12081873869

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 89 of 111 (80.18%) changed or added relevant lines in 6 files are covered.
  • 1103 unchanged lines in 23 files lost coverage.
  • Overall coverage decreased (-0.02%) to 53.911%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/trader/src/AppV2/Components/TradeParameters/Stake/stake-input.tsx 64 86 74.42%
Files with Coverage Reduction New Missed Lines %
packages/bot-web-ui/src/pages/dashboard/announcements/config.tsx 1 80.0%
packages/wallets/src/features/cfd/screens/CompareAccounts/compareAccountsConfig.ts 1 95.0%
packages/stores/src/mockStore.ts 2 0.0%
packages/trader/src/AppV2/Utils/trade-params-utils.tsx 2 88.11%
packages/account/src/Configs/user-profile-validation-config.ts 4 83.93%
packages/appstore/src/components/banners/wallets-banner/wallets-banner-upgrading.tsx 4 90.16%
packages/bot-web-ui/src/pages/dashboard/announcements/announcement-dialog.tsx 4 59.09%
packages/wallets/src/features/cfd/screens/CompareAccounts/CompareAccountsTitleIcon.tsx 4 88.89%
packages/account/src/App.tsx 5 0.0%
packages/appstore/src/components/banners/wallets-banner/wallets-banner.tsx 6 65.52%
Totals Coverage Status
Change from base Build 12045920841: -0.02%
Covered Lines: 32827
Relevant Lines: 56793

💛 - Coveralls

@@ -1,4 +1,5 @@
import Stake from './stake';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All this empty lines between imports is a result of running autofix to sort these imports because of eslint


const [proposal_request_values, setNewValues] = React.useState<TNewValues>({ amount }); // contains information for creating proposal request: stake (amount), payout_per_point for Turbos and barrier_1 for Vanillas
const [stake_error, setStakeError] = React.useState('');
const [fe_stake_error, setFEStakeError] = React.useState('');
Copy link
Contributor Author

@kate-deriv kate-deriv Nov 29, 2024

Choose a reason for hiding this comment

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

stake_error contains API error, fe_stake_error stands for cases when input in empty, user clicks on Save btn and we need to show him hardcoded on FE text

trade_store,
trade_type: contract_types[0],
});
const { data: response_1, is_fetching: is_fetching_1 } = useDtraderQuery<Parameters<TOnProposalResponse>[0]>(
Copy link
Contributor Author

@kate-deriv kate-deriv Nov 29, 2024

Choose a reason for hiding this comment

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

Instead of executing onChange function from trade store (that will change store values and send new proposal with them) when user is typing, we are using useDtraderQuery hook without subscription. It will allow us to validate the new values + we can extract more information from response. Because we are not subscribing, the store values won't be changed and we won't form a new proposal request with subscription. That will also allow us to restore original values in case of page reload & clicking on back arrow & closing without saving, so there is no need in stake in v2_params_initial_values. Only when user clicks on 'Save' button we'll call onChange.

}
);

const proposal_req_2 = getProposalRequestObject({
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 Rise/Fall and Digits we need two proposal calls, e.g. one for Call and second for Put. They should be validate independently, because of scenarios, when payout is exceed for Call, but not for Put

contract_payout: first_contract_payout,
max_payout,
error: first_payout_error,
} = getPayoutInfo(proposal_info[contract_types[0]]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created getPayoutInfo function for information extraction, you can check it in trade-params-utils.tsx file below. It contains some scary hardcoded stuff, that Maryia did and we will need to get rid of, when API will be updated

}

// Edge cases for Vanilla and Turbos
if (is_vanilla && error?.details?.barrier_choices) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lines from 141 to 157 was taken from trade-store, where this cases were handled inside of onProposalResponse

@@ -151,7 +152,7 @@ const useContractsForCompany = () => {
? 'euro_atm'
: (contract.barrier_category as TConfig['barrier_category']);
config.default_stake = contract.default_stake;

if (type === contract_type) setDefaultStake(contract.default_stake);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

default stake for every trade type is coming in contract_for_ API response. Setting the default stake to the trade store for active (selected) trade type after receiving it

@@ -463,3 +463,27 @@ export const getProposalRequestObject = ({

return request;
};

export const getPayoutInfo = (proposal_info: ReturnType<typeof getProposalInfo>) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here we are extracting payout and max payout from proposal. In case if proposal contains error, we do not receive receive this fields. Instead we are extracting payout and max payout from error text with reg exp :(
Need future improvement: ask BE team to add fields in error details.

@@ -1348,11 +1348,6 @@ export default class TradeStore extends BaseStore {

if (has_currency_changed && should_reset_stake) {
obj_new_values.amount = obj_new_values.amount || getMinPayout(obj_new_values.currency ?? '');
if (this.is_dtrader_v2_enabled)
this.setV2ParamsInitialValues({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed stake from setV2ParamsInitialValues as now we are not setting new stake values to trade store before user save the results.

@@ -131,7 +148,10 @@ const Trade = observer(() => {
)}
<ServiceErrorSheet />
<ClosedMarketMessage />
<TradeErrorSnackbar error_fields={['stop_loss', 'take_profit', 'date_start']} should_show_snackbar />
<TradeErrorSnackbar
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed hardcoded snackbar for Stake, but added stake errors to the array with observables

@kate-deriv kate-deriv marked this pull request as ready for review November 29, 2024 06:20
second_contract_payout !== new_payout_2 ||
stop_out_value !== new_stop_out ||
max_payout !== new_max_payout
) {
setDisplayedValues({
commission: new_commission,
first_contract_payout: new_payout_1,
is_first_payout_exceed,
Copy link
Contributor

Choose a reason for hiding this comment

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

exceeded*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants