Skip to content

Commit

Permalink
resolve issues with types
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytroshch committed Sep 19, 2023
1 parent 22ce385 commit 3ed5212
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
55 changes: 27 additions & 28 deletions src/modals/Strategy/StrategySettingsModal/StrategySettingsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { STRATEGY_SHAPE } from '../../../constants/prop-types-shapes'

import './style.scss'

const getProcessedLocalState = (value) => String(AmountInput.processValue(value))
const convertNumberToString = (value) => (value ? String(AmountInput.processValue(value)) : '')

const StrategySettingsModal = (props) => {
const {
Expand Down Expand Up @@ -86,21 +86,21 @@ const StrategySettingsModal = (props) => {

const saveStrategyOptionsHelper = () => {
const optionsToSave = {
[STRATEGY_OPTIONS_KEYS.CAPITAL_ALLOCATION]: getProcessedLocalState(
[STRATEGY_OPTIONS_KEYS.CAPITAL_ALLOCATION]: convertNumberToString(
capitalAllocationValue,
),
[STRATEGY_OPTIONS_KEYS.STOP_LOSS_PERC]:
getProcessedLocalState(stopLossPercValue),
convertNumberToString(stopLossPercValue),
[STRATEGY_OPTIONS_KEYS.MAX_DRAWDOWN_PERC]:
getProcessedLocalState(maxDrawdownPercValue),
convertNumberToString(maxDrawdownPercValue),

[STRATEGY_OPTIONS_KEYS.MARGIN]: tradeOnMargin,
[STRATEGY_OPTIONS_KEYS.ADD_STOP_ORDER]: additionStopOrder,
}

if (tradeOnMargin) {
optionsToSave[STRATEGY_OPTIONS_KEYS.USE_MAX_LEVERAGE] = marginTradeMode === MARGIN_TRADE_MODES.MAX
optionsToSave[STRATEGY_OPTIONS_KEYS.LEVERAGE] = getProcessedLocalState(leverageValue)
optionsToSave[STRATEGY_OPTIONS_KEYS.LEVERAGE] = convertNumberToString(leverageValue)
optionsToSave[STRATEGY_OPTIONS_KEYS.INCREASE_LEVERAGE] = increaseLeverage
}
if (additionStopOrder) {
Expand Down Expand Up @@ -200,36 +200,35 @@ const StrategySettingsModal = (props) => {
}, [t, getTabContentComponent])

useEffect(() => {
setCapitalAllocationValue(capitalAllocation)
setMaxDrawdownPercValue(maxDrawdownPerc)
setStopLossPercValue(stopLossPerc)
setCapitalAllocationValue(convertNumberToString(capitalAllocation))
setMaxDrawdownPercValue(convertNumberToString(maxDrawdownPerc))
setStopLossPercValue(convertNumberToString(stopLossPerc))

setTradeOnMargin(margin)
if (margin) {
if (!useMaxLeverage) {
setMarginTradeMode(MARGIN_TRADE_MODES.FIXED)
}
if (leverage) {
setLeverageValue(leverage)
}
if (savedIncreaseLeverage) {
setIncreaseLeverage(savedIncreaseLeverage)
}

if (!useMaxLeverage) {
setMarginTradeMode(MARGIN_TRADE_MODES.FIXED)
}
if (leverage) {
setLeverageValue(convertNumberToString(leverage))
}
if (savedIncreaseLeverage) {
setIncreaseLeverage(savedIncreaseLeverage)
}

setAdditionStopOrder(addStopOrder)
if (addStopOrder && stopOrderPercent) {
setStopOrderValue(stopOrderPercent)
if (stopOrderPercent) {
setStopOrderValue(convertNumberToString(stopOrderPercent))
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [strategyId, isOpen])

useEffect(() => {
if (
pendingForSaveOptions
&& capitalAllocation === getProcessedLocalState(capitalAllocationValue)
&& maxDrawdownPerc === getProcessedLocalState(maxDrawdownPercValue)
&& stopLossPerc === getProcessedLocalState(stopLossPercValue)
&& capitalAllocation === convertNumberToString(capitalAllocationValue)
&& maxDrawdownPerc === convertNumberToString(maxDrawdownPercValue)
&& stopLossPerc === convertNumberToString(stopLossPercValue)
) {
// Continue process (execute or backtest) after options was saved
const isExecution = strategySettingsModalType === EXECUTION_TYPES.LIVE
Expand Down Expand Up @@ -257,14 +256,14 @@ const StrategySettingsModal = (props) => {

useEffect(() => {
if (
capitalAllocation !== capitalAllocationValue
|| maxDrawdownPerc !== maxDrawdownPercValue
|| stopLossPerc !== stopLossPercValue
convertNumberToString(capitalAllocation) !== capitalAllocationValue
|| convertNumberToString(maxDrawdownPerc) !== maxDrawdownPercValue
|| convertNumberToString(stopLossPerc) !== stopLossPercValue
|| margin !== tradeOnMargin
|| leverage !== leverageValue
|| convertNumberToString(leverage) !== leverageValue
|| savedIncreaseLeverage !== increaseLeverage
|| addStopOrder !== additionStopOrder
|| stopOrderPercent !== stopOrderValue
|| convertNumberToString(stopOrderPercent) !== stopOrderValue
) {
setIsDirty(true)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,9 @@ const ExecutionTab = ({
}

ExecutionTab.propTypes = {
capitalAllocation: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
stopLossPerc: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
maxDrawdownPerc: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
capitalAllocation: PropTypes.string.isRequired,
stopLossPerc: PropTypes.string.isRequired,
maxDrawdownPerc: PropTypes.string.isRequired,
setCapitalAllocationValue: PropTypes.func.isRequired,
setStopLossPercValue: PropTypes.func.isRequired,
setMaxDrawdownPercValue: PropTypes.func.isRequired,
Expand Down

0 comments on commit 3ed5212

Please sign in to comment.