Skip to content

Commit 54d25ee

Browse files
committed
fix: Parameter order and null-safety fixes for open trades
- Fixed parameter order in calculateMonthMetrics (deposits/withdrawals were swapped) - Added null-coalescing operators for pnl and returnPercentage - Fixed exitDate display to show 'Open' for open trades - Prevents crashes when displaying open trades in month details
1 parent 722c59d commit 54d25ee

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

app/add-month.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default function AddMonthScreen() {
5555
const with_ = parseCurrency(withdrawals);
5656

5757
if (start > 0 && end >= 0) {
58-
return calculateMonthMetrics(start, end, dep, with_);
58+
return calculateMonthMetrics(start, end, with_, dep);
5959
}
6060
return null;
6161
}, [startingCapital, endingCapital, deposits, withdrawals]);
@@ -102,7 +102,7 @@ export default function AddMonthScreen() {
102102
return;
103103
}
104104

105-
const validation = validateMonthForm(start, end, dep, with_, selectedMonth);
105+
const validation = validateMonthForm(start, end, with_, dep, selectedMonth);
106106
if (!validation.isValid) {
107107
Alert.alert('Validation Error', validation.error);
108108
return;
@@ -122,8 +122,8 @@ export default function AddMonthScreen() {
122122
selectedMonth,
123123
start,
124124
end,
125-
dep,
126125
with_,
126+
dep,
127127
notes,
128128
'closed'
129129
);

app/month-details/[id].tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function MonthDetailsScreen() {
3434

3535
// Use trade P&L when trades exist
3636
const hasTrades = monthTrades.length > 0;
37-
const tradePnL = hasTrades ? monthTrades.reduce((sum, t) => sum + t.pnl, 0) : 0;
37+
const tradePnL = hasTrades ? monthTrades.reduce((sum, t) => sum + (t.pnl ?? 0), 0) : 0;
3838
const effectivePnL = hasTrades ? tradePnL : (month?.netProfitLoss ?? 0);
3939
const effectiveReturn = hasTrades && month && month.startingCapital > 0
4040
? (tradePnL / month.startingCapital) * 100
@@ -78,7 +78,7 @@ export default function MonthDetailsScreen() {
7878
const with_ = parseCurrency(withdrawals);
7979

8080
if (start > 0 && end >= 0) {
81-
return calculateMonthMetrics(start, end, dep, with_);
81+
return calculateMonthMetrics(start, end, with_, dep);
8282
}
8383
return null;
8484
}, [startingCapital, endingCapital, deposits, withdrawals]);
@@ -138,7 +138,7 @@ export default function MonthDetailsScreen() {
138138
return;
139139
}
140140

141-
const validation = validateMonthForm(start, end, dep, with_, month.month);
141+
const validation = validateMonthForm(start, end, with_, dep, month.month);
142142
if (!validation.isValid) {
143143
Alert.alert('Validation Error', validation.error);
144144
return;
@@ -147,12 +147,12 @@ export default function MonthDetailsScreen() {
147147
setIsSubmitting(true);
148148

149149
try {
150-
const metrics = calculateMonthMetrics(start, end, dep, with_);
150+
const metrics = calculateMonthMetrics(start, end, with_, dep);
151151
await updateMonth(month.id, {
152152
startingCapital: start,
153153
endingCapital: end,
154-
deposits: dep,
155-
withdrawals: with_,
154+
deposits: with_,
155+
withdrawals: dep,
156156
notes,
157157
...metrics,
158158
});
@@ -453,29 +453,29 @@ export default function MonthDetailsScreen() {
453453
width: 32,
454454
height: 32,
455455
borderRadius: 8,
456-
backgroundColor: trade.pnl >= 0 ? 'rgba(16, 185, 95, 0.1)' : 'rgba(239, 68, 68, 0.1)',
456+
backgroundColor: (trade.pnl ?? 0) >= 0 ? 'rgba(16, 185, 95, 0.1)' : 'rgba(239, 68, 68, 0.1)',
457457
justifyContent: 'center',
458458
alignItems: 'center',
459459
}}>
460460
<Ionicons
461-
name={trade.pnl >= 0 ? 'trending-up' : 'trending-down'}
461+
name={(trade.pnl ?? 0) >= 0 ? 'trending-up' : 'trending-down'}
462462
size={16}
463-
color={trade.pnl >= 0 ? colors.profit : colors.loss}
463+
color={(trade.pnl ?? 0) >= 0 ? colors.profit : colors.loss}
464464
/>
465465
</View>
466466
<View>
467467
<Text style={{ fontFamily: fonts.semiBold, fontSize: 14, color: colors.text }}>{trade.symbol}</Text>
468468
<Text style={{ fontFamily: fonts.regular, fontSize: 12, color: colors.textMuted }}>
469-
{new Date(trade.exitDate).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}
469+
{trade.exitDate ? new Date(trade.exitDate).toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) : 'Open'}
470470
</Text>
471471
</View>
472472
</View>
473473
<View style={{ alignItems: 'flex-end' }}>
474-
<Text style={{ fontFamily: fonts.bold, fontSize: 14, color: trade.pnl >= 0 ? colors.profit : colors.loss }}>
475-
{trade.pnl >= 0 ? '+' : ''}${Math.abs(trade.pnl).toFixed(2)}
474+
<Text style={{ fontFamily: fonts.bold, fontSize: 14, color: (trade.pnl ?? 0) >= 0 ? colors.profit : colors.loss }}>
475+
{(trade.pnl ?? 0) >= 0 ? '+' : ''}${Math.abs(trade.pnl ?? 0).toFixed(2)}
476476
</Text>
477477
<Text style={{ fontFamily: fonts.regular, fontSize: 12, color: colors.textMuted }}>
478-
{trade.returnPercentage >= 0 ? '+' : ''}{trade.returnPercentage.toFixed(1)}%
478+
{(trade.returnPercentage ?? 0) >= 0 ? '+' : ''}{(trade.returnPercentage ?? 0).toFixed(1)}%
479479
</Text>
480480
</View>
481481
</TouchableOpacity>

0 commit comments

Comments
 (0)