From 1a636ba6e00f508ffc2889935faff7efaa900d6c Mon Sep 17 00:00:00 2001 From: TzuHanLiang Date: Wed, 12 Jul 2023 16:08:56 +0800 Subject: [PATCH 1/2] feat: update --- src/contexts/user_context.tsx | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/contexts/user_context.tsx b/src/contexts/user_context.tsx index 7fbc13ef5..eacb08eb0 100644 --- a/src/contexts/user_context.tsx +++ b/src/contexts/user_context.tsx @@ -840,18 +840,9 @@ export const UserProvider = ({children}: IUserProvider) => { }; const getCFD = (id: string) => { - const CFD = CFDsRef.current[id]; - // Deprecate: [debug] (20230707 - tzuhan) - // eslint-disable-next-line no-console - // console.log(`getCFD CFD`, CFD); - /** Deprecate: [debug] (20230707 - tzuhan) - const histories = historiesRef.current.filter( - history => history.receipt.orderSnapshot.id === id - ); - // Deprecate: [debug] (20230707 - tzuhan) - // eslint-disable-next-line no-console - console.log(`getCFD histories`, histories); - */ + let CFD: ICFDOrder | null = CFDsRef.current[id] ?? null; + if (!CFD) CFD = openCFDsRef.current.find(openCFD => openCFD.id === id) ?? null; + if (!CFD) CFD = closedCFDsRef.current.find(closeCFD => closeCFD.id === id) ?? null; return CFD; }; @@ -1049,7 +1040,7 @@ export const UserProvider = ({children}: IUserProvider) => { try { if (!enableServiceTermRef.current) throw new CustomError(Code.SERVICE_TERM_DISABLE); if (!applyCloseCFDOrder) throw new CustomError(Code.INVAILD_ORDER_INPUTS); - const closeAppliedCFD = CFDs[applyCloseCFDOrder.referenceId]; + const closeAppliedCFD = getCFD(applyCloseCFDOrder.referenceId); if (!closeAppliedCFD) throw new CustomError(Code.CFD_ORDER_NOT_FOUND); if (closeAppliedCFD.state !== OrderState.OPENING) throw new CustomError(Code.CFD_ORDER_IS_ALREADY_CLOSED); @@ -1166,7 +1157,7 @@ export const UserProvider = ({children}: IUserProvider) => { try { if (!enableServiceTermRef.current) throw new CustomError(Code.SERVICE_TERM_DISABLE); if (!applyUpdateCFDOrder) throw new CustomError(Code.INVAILD_ORDER_INPUTS); - const updateAppliedCFD = CFDs[applyUpdateCFDOrder.referenceId]; + const updateAppliedCFD = getCFD(applyUpdateCFDOrder.referenceId); if (!updateAppliedCFD) throw new CustomError(Code.CFD_ORDER_NOT_FOUND); if (updateAppliedCFD.state !== OrderState.OPENING) throw new CustomError(Code.CFD_ORDER_IS_ALREADY_CLOSED); From 4366d06486f6972d554608a4eb339df15d8836ec Mon Sep 17 00:00:00 2001 From: TzuHanLiang Date: Thu, 13 Jul 2023 15:13:25 +0800 Subject: [PATCH 2/2] feat: fix bug --- .../position_updated_modal.tsx | 55 +++++++++---------- .../update_form_modal/update_form_modal.tsx | 8 +-- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/components/position_updated_modal/position_updated_modal.tsx b/src/components/position_updated_modal/position_updated_modal.tsx index 11ebdd434..4361ea9de 100644 --- a/src/components/position_updated_modal/position_updated_modal.tsx +++ b/src/components/position_updated_modal/position_updated_modal.tsx @@ -225,39 +225,34 @@ const PositionUpdatedModal = ({ ? t('POSITION_MODAL.GUARANTEED_STOP_YES') : t('POSITION_MODAL.GUARANTEED_STOP_NO'); - const displayedTakeProfit = - updatedProps?.takeProfit !== undefined - ? updatedProps.takeProfit === 0 - ? '-' - : updatedProps.takeProfit !== 0 - ? `$ ${updatedProps.takeProfit.toLocaleString( - UNIVERSAL_NUMBER_FORMAT_LOCALE, - FRACTION_DIGITS - )}` - : undefined - : openCfdDetails?.takeProfit - ? `$ ${openCfdDetails?.takeProfit.toLocaleString( + const displayedTakeProfit = !!updatedProps?.takeProfit + ? updatedProps.takeProfit === 0 + ? '-' + : updatedProps.takeProfit !== 0 + ? `$ ${updatedProps.takeProfit.toLocaleString( UNIVERSAL_NUMBER_FORMAT_LOCALE, FRACTION_DIGITS )}` - : '-'; - - const displayedStopLoss = - updatedProps?.stopLoss !== undefined - ? updatedProps.stopLoss === 0 - ? '-' - : updatedProps.stopLoss !== 0 - ? `$ ${updatedProps.stopLoss.toLocaleString( - UNIVERSAL_NUMBER_FORMAT_LOCALE, - FRACTION_DIGITS - )}` - : undefined - : openCfdDetails?.stopLoss - ? `$ ${openCfdDetails?.stopLoss.toLocaleString( - UNIVERSAL_NUMBER_FORMAT_LOCALE, - FRACTION_DIGITS - )}` - : '-'; + : undefined + : openCfdDetails?.takeProfit + ? `$ ${openCfdDetails?.takeProfit.toLocaleString( + UNIVERSAL_NUMBER_FORMAT_LOCALE, + FRACTION_DIGITS + )}` + : '-'; + + const displayedStopLoss = !!updatedProps?.stopLoss + ? updatedProps.stopLoss === 0 + ? '-' + : updatedProps.stopLoss !== 0 + ? `$ ${updatedProps.stopLoss.toLocaleString(UNIVERSAL_NUMBER_FORMAT_LOCALE, FRACTION_DIGITS)}` + : undefined + : openCfdDetails?.stopLoss + ? `$ ${openCfdDetails?.stopLoss.toLocaleString( + UNIVERSAL_NUMBER_FORMAT_LOCALE, + FRACTION_DIGITS + )}` + : '-'; const displayedTypeOfPosition = openCfdDetails?.typeOfPosition === TypeOfPosition.BUY diff --git a/src/components/update_form_modal/update_form_modal.tsx b/src/components/update_form_modal/update_form_modal.tsx index e6f0c009d..9aa225a0f 100644 --- a/src/components/update_form_modal/update_form_modal.tsx +++ b/src/components/update_form_modal/update_form_modal.tsx @@ -645,13 +645,13 @@ const UpdateFormModal = ({ setTpUpperLimit(caledTpUpperLimit); setTpValue( - openCfdDetails.takeProfit === 0 || openCfdDetails.takeProfit === undefined + openCfdDetails.takeProfit === 0 || !openCfdDetails.takeProfit ? openCfdDetails.suggestion.takeProfit : openCfdDetails.takeProfit ); setSlValue( - openCfdDetails.stopLoss === 0 || openCfdDetails.stopLoss === undefined + openCfdDetails.stopLoss === 0 || !openCfdDetails.stopLoss ? suggestedSl : openCfdDetails.stopLoss ); @@ -796,13 +796,13 @@ const UpdateFormModal = ({
{t('POSITION_MODAL.TP_AND_SL')}
- {cfdTp === undefined || cfdTp === 0 + {!cfdTp || cfdTp === 0 ? '-' : cfdTp.toLocaleString(UNIVERSAL_NUMBER_FORMAT_LOCALE, FRACTION_DIGITS)} {' '} /{' '} - {cfdSl === undefined || cfdSl === 0 + {!cfdSl || cfdSl === 0 ? '-' : cfdSl.toLocaleString(UNIVERSAL_NUMBER_FORMAT_LOCALE, FRACTION_DIGITS)}