-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaloriePuzzle.tsx
94 lines (85 loc) · 2.88 KB
/
CaloriePuzzle.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React, { useState } from "react";
import axios from "axios";
import { CaloriesInput, CaloriesOutput, getTopCalories } from "../utils/CaloriesCalculator";
const CaloriePuzzle: React.FC = () => {
const [input, setInput] = useState<string>("");
const [result, setResult] = useState<string>("");
const [error, setError] = useState<string>("");
const [loading, setLoading] = useState<boolean>(false);
const fixedK = 3; // fixed k (top) calories
const apiBaseUrl = process.env.REACT_APP_CALORIES_API_BASE_URL; // Java API URL
// Handle file input
const handleFileUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const text = e.target?.result as string;
setInput(text);
};
reader.readAsText(file);
}
};
// Solve using JavaScript (client-side)
const solveWithJS = () => {
const caloriesInput: CaloriesInput = { calories: input, k: fixedK };
const caloriesOutput: CaloriesOutput = getTopCalories(caloriesInput);
if (caloriesOutput.message !== "Success") {
setError(caloriesOutput.message || "An unknown error occurred.");
setResult("");
} else {
setResult(`Max Calories: ${caloriesOutput.maxCalories}\nTop ${fixedK} Calories Sum: ${caloriesOutput.topKCaloriesSum}`);
setError("");
}
};
// Solve using Java (call API)
const solveWithJava = async () => {
setLoading(true);
setError("");
try {
const response = await axios.post(`${apiBaseUrl}`, {
calories: input,
k: fixedK,
});
if (response.data.message !== "Success") {
setError(response.data.message || "An unknown error occurred.");
setResult("");
} else {
setResult(`Max Calories (Java API): ${response.data.maxCalories}\nTop 3 Calories Sum (Java API): ${response.data.topKCaloriesSum}`);
setError("");
}
} catch (error) {
setError("Error calling Java API");
setResult("");
} finally {
setLoading(false);
}
};
return (
<div>
<h2>Calorie Counting</h2>
<textarea
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Enter or upload puzzle input here"
rows={20}
cols={70}
style={{ resize: "none", overflowY: "scroll", whiteSpace: "pre-wrap" }}
/>
<br />
<input type="file" onChange={handleFileUpload} />
<br />
<button onClick={solveWithJS} disabled={loading}>
Solve with JavaScript
</button>
<button onClick={solveWithJava} disabled={loading}>
{loading ? "Solving..." : "Solve with Java API"}
</button>
<div>
<h2>Result:</h2>
{error ? <pre style={{ color: "red" }}>{error}</pre> : <pre>{result}</pre>}
</div>
</div>
);
};
export default CaloriePuzzle;