Skip to content

Commit 41e9b63

Browse files
authored
Merge pull request #247 from Agent-BossFighters/feat/daily-calculator
Feat/daily calculator
2 parents 21e9022 + 7295137 commit 41e9b63

7 files changed

Lines changed: 79 additions & 35 deletions

File tree

src/features/dashboard/daily/components/Calculator.jsx

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
1-
import React, { useState } from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import { useRarityManagement } from "../hooks/useRarityManagement";
33
import RaritySelect from "./rarity-select";
44
import { Spark } from "@img/index";
55
import { Switch } from "@shared/ui/switch";
66
import { Input } from '@shared/ui/input';
77
import { Button } from "@shared/ui/button";
8+
import { useUserPreference } from "@context/userPreference.context";
89

910
const defaultSlot = {
1011
rarity: '',
1112
energyStart: '',
1213
energyEnd: ''
1314
}
1415

15-
export default function Calculator() {
16+
export default function Calculator({ onEnergyUsedChange, onPriceChange }) {
17+
const { streamerMode } = useUserPreference();
1618
const { rarities } = useRarityManagement();
17-
1819
const rarityPrices = rarities.reduce((acc, r) => {
1920
if (r.price) acc[r.rarity.toLowerCase()] = r.price;
2021
return acc;
2122
}, {});
2223

23-
const [slots, setSlots] = useState(Array(5).fill({ ...defaultSlot }))
24-
const [autoComplete, setAutoComplete] = useState(false)
24+
// Initialiser les slots et l'état d'autocomplétion à partir du localStorage
25+
const [slots, setSlots] = useState(() => {
26+
const saved = localStorage.getItem("calculator_slots");
27+
return saved ? JSON.parse(saved) : Array(5).fill({ ...defaultSlot });
28+
});
29+
const [autoComplete, setAutoComplete] = useState(() => {
30+
const saved = localStorage.getItem("calculator_autocomplete");
31+
return saved ? JSON.parse(saved) : false;
32+
});
33+
34+
useEffect(() => {
35+
localStorage.setItem("calculator_slots", JSON.stringify(slots));
36+
localStorage.setItem("calculator_autocomplete", JSON.stringify(autoComplete));
37+
}, [slots, autoComplete]);
2538

2639
const handleChange = (index, field, value) => {
2740
const updated = [...slots]
@@ -54,6 +67,14 @@ export default function Calculator() {
5467
}))
5568
);
5669
};
70+
71+
useEffect(() => {
72+
if (onEnergyUsedChange) onEnergyUsedChange(totalEnergyUsed)
73+
}, [totalEnergyUsed])
74+
75+
useEffect(() => {
76+
if (onPriceChange) onPriceChange(totalPriceEnergyUsed)
77+
}, [totalPriceEnergyUsed])
5778

5879
return (
5980
<div className="border-4 border-yellow-400 rounded-lg mb-10 w-[auto] p-4">
@@ -79,6 +100,9 @@ export default function Calculator() {
79100
onChange={val => handleChange(i, 'rarity', val)}
80101
disabled={false}
81102
showIssueId={true}
103+
selectedBadges={slots
104+
.filter((_, idx) => idx !== i && _.rarity !== "none")
105+
.map(r => r.rarity)}
82106
/>
83107
</div>
84108

@@ -98,7 +122,7 @@ export default function Calculator() {
98122
/>
99123
</div>
100124
<div className="flex flex-col w-full md:w-1/2">
101-
<span className={`text-s mb-1 max-w-[88%] font-bold ${slot.rarity && slot.rarity !== "none" ? "text-white" : "text-gray-400"}`}>
125+
<span className={`text-s mb-1 max-w-[87%] font-bold ${slot.rarity && slot.rarity !== "none" ? "text-white" : "text-gray-400"}`}>
102126
ENERGY END
103127
</span>
104128
<Input
@@ -122,9 +146,11 @@ export default function Calculator() {
122146
<p>
123147
<span className="text-3xl font-bold text-red-500">{totalEnergyUsed}</span>
124148
</p>
125-
<p className="text-md font-semibold">
126-
<span className="text-red-500">{totalPriceEnergyUsed.toFixed(2)} $</span>
127-
</p>
149+
{!streamerMode && (
150+
<p className="text-md font-semibold">
151+
<span className="text-red-500">{totalPriceEnergyUsed.toFixed(2)} $</span>
152+
</p>
153+
)}
128154
</div>
129155
<Button
130156
onClick={() => {

src/features/dashboard/daily/components/MatchDisplayRow.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default function MatchDisplayRow({
2727
});
2828

2929
return (
30-
<tr>
30+
<tr className="select-none">
3131
<td className="min-w-[6%] pl-4 select-none" title={match.build}>
3232
<span className="font-medium">{match.build.length < 10 ? match.build : match.build.slice(0, 10) + "..."}</span>
3333
</td>

src/features/dashboard/daily/components/MatchFormRow.jsx

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export default function MatchFormRow({
2727
onChange,
2828
onRarityChange,
2929
selectedDate,
30-
initialMatchData
30+
initialMatchData,
31+
totalEnergyUsed,
32+
totalPriceEnergyUsed
3133
}) {
3234
const { streamerMode } = useUserPreference();
3335

@@ -49,7 +51,7 @@ export default function MatchFormRow({
4951

5052
// Si c'est aujourd'hui, on rend le formulaire
5153
return (
52-
<tr>
54+
<tr className="select-none">
5355
<td className="min-w-[6%] pl-4">
5456
<Select
5557
value={currentData.buildId}
@@ -101,26 +103,14 @@ export default function MatchFormRow({
101103
))}
102104
<td className="px-4 text-left min-w-[4%]">-</td>
103105

104-
<td className="min-w-[4%]">
105-
<Input
106-
type="number"
107-
className="w-20 text-left px-4 [appearance:textfield] [&::-webkit-outer-spin-button]:m-0 [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:m-0 [&::-webkit-inner-spin-button]:appearance-none"
108-
value={currentData.energyUsed}
109-
placeholder="0"
110-
onChange={(e) => onChange("energyUsed", e.target.value)}
111-
/>
106+
<td className="min-w-[4%] px-4 text-left text-white">
107+
{totalEnergyUsed}
112108
</td>
113109

114110
{/* Masquer les colonnes financières en mode streamer */}
115111
{!streamerMode && (
116-
<td className="min-w-[4%]">
117-
<Input
118-
type="number"
119-
className="w-20 text-left px-4 [appearance:textfield] [&::-webkit-outer-spin-button]:m-0 [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:m-0 [&::-webkit-inner-spin-button]:appearance-none"
120-
value={currentData.energyCost}
121-
placeholder="0"
122-
onChange={(e) => onChange("energyCost", e.target.value)}
123-
/>
112+
<td className="min-w-[4%] px-4 text-left text-red-500">
113+
${typeof totalPriceEnergyUsed === "number" ? totalPriceEnergyUsed.toFixed(2) : "0.00"}
124114
</td>
125115
)}
126116

src/features/dashboard/daily/components/match-entry.jsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export default function MatchEntry({
3030
unlockedSlots,
3131
selectedDate,
3232
initialMatchData,
33+
totalEnergyUsed,
34+
totalPriceEnergyUsed
3335
}) {
3436
const [formData, setFormData] = useState(() => {
3537
const initialData = {
@@ -98,7 +100,9 @@ export default function MatchEntry({
98100
const selectedBuild = builds.find((b) => b.id === data.buildId);
99101
if (!selectedBuild) return null;
100102

101-
if (!data.map || !data.result || !data.bft || !data.energyUsed || !data.energyCost) {
103+
if (!data.map || !data.result || !data.bft || data.energyUsed === null || data.energyUsed === undefined ||
104+
data.energyCost === null || data.energyCost === undefined
105+
) {
102106
throw new Error("Tous les champs obligatoires doivent être remplis");
103107
}
104108

@@ -149,8 +153,8 @@ export default function MatchEntry({
149153
totalPremiumCurrency: totalPremiumCurrency,
150154
bonusMultiplier: parseFloat(selectedBuild.bonusMultiplier),
151155
perksMultiplier: parseFloat(selectedBuild.perksMultiplier),
152-
energyUsed: parseFloat(data.energyUsed),
153-
energyCost: parseFloat(data.energyCost),
156+
energyUsed: parseFloat(totalEnergyUsed),
157+
energyCost: parseFloat(totalPriceEnergyUsed),
154158
badge_used_attributes: data.rarities
155159
.map((rarity, index) => {
156160
const slot = index + 1;
@@ -242,7 +246,11 @@ export default function MatchEntry({
242246
};
243247

244248
const handleSubmit = () => {
245-
const data = isCreating ? formData : editedData;
249+
const data = {
250+
...(isCreating ? formData : editedData),
251+
energyUsed: totalEnergyUsed,
252+
energyCost: totalPriceEnergyUsed
253+
};
246254
if (!validateForm(data)) return;
247255

248256
if (isCreating) {
@@ -293,6 +301,8 @@ export default function MatchEntry({
293301
onChange={handleChange}
294302
onRarityChange={handleRarityChange}
295303
selectedDate={selectedDate}
304+
totalEnergyUsed={totalEnergyUsed}
305+
totalPriceEnergyUsed={totalPriceEnergyUsed}
296306
/>
297307
<MatchDialogs
298308
showMissingFieldsDialog={showMissingFieldsDialog}

src/features/dashboard/daily/components/matches-list.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export default function MatchesList({
2626
onCancel,
2727
selectedDate,
2828
initialMatchData,
29+
totalEnergyUsed,
30+
totalPriceEnergyUsed
2931
}) {
3032
const { streamerMode } = useUserPreference();
3133

@@ -55,7 +57,7 @@ export default function MatchesList({
5557
};
5658

5759
return (
58-
<div className="flex-grow overflow-x-auto">
60+
<div className="flex-grow overflow-x-auto select-none">
5961
<div
6062
className="w-full min-w-0"
6163
ref={tableRef}
@@ -141,6 +143,8 @@ export default function MatchesList({
141143
onSubmit={onAdd}
142144
selectedDate={selectedDate}
143145
initialMatchData={initialMatchData}
146+
totalEnergyUsed={totalEnergyUsed}
147+
totalPriceEnergyUsed={totalPriceEnergyUsed}
144148
/>
145149
{visibleMatches.map((match) => (
146150
<MatchEntry
@@ -156,6 +160,8 @@ export default function MatchesList({
156160
onCancel={onCancel}
157161
unlockedSlots={unlockedSlots}
158162
selectedDate={selectedDate}
163+
totalEnergyUsed={totalEnergyUsed}
164+
totalPriceEnergyUsed={totalPriceEnergyUsed}
159165
/>
160166
))}
161167
</TableBody>

src/features/dashboard/daily/daily-container.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import DailySummary from "./daily-summary";
33
import DailyMatches from "./daily-matches";
44
import { useDailyData } from "./hooks/useDailyData.jsx";
55
import DateSelector from "./components/DateSelector";
6-
import Calculator from "./components/Calculator";
76

87
export default function DailyContainer() {
98
const {
@@ -53,7 +52,6 @@ export default function DailyContainer() {
5352
</div>
5453
) : (
5554
<>
56-
<Calculator />
5755
<DailyMatches
5856
matches={matches}
5957
builds={builds}

src/features/dashboard/daily/daily-matches.jsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import MatchesList from "./components/matches-list";
33
import { useUserPreference } from "@context/userPreference.context";
44
import { MatchDialogs } from "./components/MatchDialogs";
55
import { useLastMatchData } from "@shared/hook/useLastMatchData";
6+
import Calculator from "./components/Calculator";
7+
68

79
export default function DailyMatches({
810
matches,
@@ -19,6 +21,10 @@ export default function DailyMatches({
1921
const [showEditTimeoutDialog, setShowEditTimeoutDialog] = useState(false);
2022
const [currentMatch, setCurrentMatch] = useState(null);
2123
const { lastMatchData, saveLastMatchData } = useLastMatchData();
24+
const [totalEnergyUsed, setTotalEnergyUsed] = useState(0)
25+
const [totalPriceEnergyUsed, setTotalPriceEnergyUsed] = useState(0)
26+
27+
2228

2329
const availableSlots = unlockedSlots;
2430

@@ -110,6 +116,12 @@ export default function DailyMatches({
110116

111117
return (
112118
<div className="flex flex-col gap-8">
119+
120+
<Calculator
121+
onEnergyUsedChange={setTotalEnergyUsed}
122+
onPriceChange={setTotalPriceEnergyUsed}
123+
/>
124+
113125
<MatchesList
114126
matches={matches}
115127
builds={builds}
@@ -124,6 +136,8 @@ export default function DailyMatches({
124136
onCancel={handleCancel}
125137
selectedDate={selectedDate}
126138
initialMatchData={getInitialMatchData()}
139+
totalEnergyUsed={totalEnergyUsed}
140+
totalPriceEnergyUsed={totalPriceEnergyUsed}
127141
/>
128142

129143
{/* Dialog for edit timeout */}

0 commit comments

Comments
 (0)