-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathCodeRun.jsx
More file actions
291 lines (268 loc) · 8.91 KB
/
CodeRun.jsx
File metadata and controls
291 lines (268 loc) · 8.91 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import React, { useState, useEffect, useContext } from 'react';
import axios from 'axios';
import { AuthContext } from "../context/AuthContext";
import { ToastContainer, toast } from 'react-toastify';
import { motion, AnimatePresence } from 'framer-motion';
import { FiClock } from 'react-icons/fi';
import { ExtraContext } from '../context/ExtraContext';
import { useNavigate } from 'react-router-dom';
import { RUST_MAIN_URL } from '../utils/constant.js';
const problems = [
{
id: 1,
question: "Given an array of integers, return the sum of all positive numbers.",
sampleInput: "[1, -2, 3, 4, -5]",
sampleOutput: "8",
submitted: false,
},
{
id: 2,
question: "Write a function to check if a string is a palindrome.",
sampleInput: "'racecar'",
sampleOutput: "true",
submitted: false,
},
{
id: 3,
question: "Find the largest element in an array.",
sampleInput: "[3, 1, 4, 1, 5, 9]",
sampleOutput: "9",
submitted: false,
}
];
const CodeRun = () => {
const [selectedProblem, setSelectedProblem] = useState(problems[0]);
const [timeLeft, setTimeLeft] = useState(300); // 5 minutes timer
const [file, setFile] = useState();
const [score, setScore] = useState(0);
const { user } = useContext(AuthContext);
const { contestId } = useContext(ExtraContext);
const navigate = useNavigate();
const handleSubmit = async () => {
try {
if (selectedProblem.submitted) {
toast.error(`You have already submitted this problem`);
return;
}
const formData = new FormData();
formData.append("file", file);
console.log(formData);
const response = await axios.post(`${RUST_MAIN_URL}runcode`, formData);
console.log(response);
if (response.data === "AC") {
setScore((prevScore) => {
const newScore = Number(prevScore) + Number(1000 - (300 - timeLeft));
console.log("New Score: ", newScore, "Time Left: ", timeLeft);
return newScore;
});
toast.success(`ACCEPTED`);
setSelectedProblem((prev) => ({ ...prev, submitted: true }));
} else {
toast.error(`WRONG ANSWER`);
}
} catch (err) {
console.log(err);
}
};
useEffect(() => {
const timer = setInterval(() => {
setTimeLeft((prevTime) => {
if (prevTime <= 0) {
clearInterval(timer);
return 0;
}
return prevTime - 1;
});
}, 1000);
return () => clearInterval(timer);
}, []);
useEffect(() => {
const handleBeforeUnload = (event) => {
event.preventDefault();
event.returnValue = '';
};
window.addEventListener('beforeunload', handleBeforeUnload);
return () => {
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, []);
const formatTime = (seconds) => {
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
return `${String(minutes).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
};
const handleUpload = (e) => {
const originalFile = e.target.files[0];
const newFileName = `ques${selectedProblem.id}.py`;
const renamedFile = new File([originalFile], newFileName, {
type: originalFile.type,
});
setFile(renamedFile);
console.log(file);
};
const handleLeave = async () => {
toast.success("You have completed the contest");
try {
const response = await axios.put(`${RUST_MAIN_URL}updatecontestscore`, {
id: `${contestId}`,
contestscore: `${score}`,
});
console.log(response);
navigate('/request')
} catch (err) {
console.log("Error leaving contest : " + err.message);
}
};
const containerVariants = {
hidden: { opacity: 0, y: 20 },
visible: {
opacity: 1,
y: 0,
transition: {
duration: 0.5,
when: "beforeChildren",
staggerChildren: 0.1
}
},
exit: { opacity: 0, y: 20, transition: { duration: 0.3 } }
};
const itemVariants = {
hidden: { opacity: 0, y: 20 },
visible: { opacity: 1, y: 0 }
};
return (
<motion.div
variants={containerVariants}
initial="hidden"
animate="visible"
exit="exit"
className="p-8 shadow-lg h-screen flex flex-col justify-between bg-neutral-900 text-white"
>
<motion.header variants={itemVariants} className="mb-6 flex justify-between items-center">
<div>
<motion.h1
className="text-4xl font-bold text-[#ff0059]"
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
>
CodeRun
</motion.h1>
<motion.div
className="flex items-center text-lg mt-2 text-neutral-400"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ delay: 0.5 }}
>
<FiClock className="mr-2 text-[#ff0059]" />
<span>Time Left: <span className="font-bold text-white">{formatTime(timeLeft)}</span></span>
</motion.div>
</div>
<motion.div variants={itemVariants}>
<label className="text-gray-400 mr-2">Select Problem:</label>
<motion.select
value={selectedProblem.id}
onChange={(e) =>
setSelectedProblem(problems.find((p) => p.id === Number(e.target.value)))
}
className="bg-neutral-800 text-white p-2 rounded-md outline-none border border-gray-600"
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
>
{problems.map((problem) => (
<option key={problem.id} value={problem.id}>
Problem {problem.id}
</option>
))}
</motion.select>
</motion.div>
</motion.header>
<AnimatePresence mode="wait">
<motion.section
key={selectedProblem.id}
variants={itemVariants}
initial="hidden"
animate="visible"
exit="hidden"
className="flex-1"
>
<motion.h2
className="text-2xl font-semibold mb-4"
whileHover={{ x: 10 }}
>
Problem {selectedProblem.id}
</motion.h2>
<motion.p
className="text-neutral-400 mb-4"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ delay: 0.2 }}
>
{selectedProblem.question}
</motion.p>
<motion.div className="mb-4" variants={itemVariants}>
<h3 className="text-lg font-semibold text-neutral-300">Sample Input:</h3>
<motion.pre
className="bg-neutral-800 p-4 rounded-md mt-2 text-sm text-neutral-200"
whileHover={{ scale: 1.02 }}
>
{selectedProblem.sampleInput}
</motion.pre>
</motion.div>
<motion.div className="mb-6" variants={itemVariants}>
<h3 className="text-lg font-semibold text-neutral-300">Sample Output:</h3>
<motion.pre
className="bg-neutral-800 p-4 rounded-md mt-2 text-sm text-neutral-200"
whileHover={{ scale: 1.02 }}
>
{selectedProblem.sampleOutput}
</motion.pre>
</motion.div>
<motion.label className="block mb-6" variants={itemVariants}>
<span className="text-gray-400">Upload your code:</span>
<motion.input
type="file"
accept=".js,.py,.cpp"
onChange={handleUpload}
className="mt-2 block w-full p-2 rounded-md bg-neutral-800 outline-none text-white border border-gray-600"
whileHover={{ scale: 1.02 }}
whileTap={{ scale: 0.98 }}
/>
<motion.p
className="mt-2 text-sm text-neutral-500"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ delay: 0.3 }}
>
Accepted formats: .js, .py, .cpp
</motion.p>
</motion.label>
</motion.section>
</AnimatePresence>
<motion.footer
variants={itemVariants}
className="flex justify-between items-center mt-8"
>
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
type="button"
onClick={handleLeave}
className="bg-neutral-800 hover:bg-gray-600 text-white py-2 px-6 rounded-md transition-all duration-300"
>
Leave Contest
</motion.button>
<motion.button
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
type="button"
onClick={handleSubmit}
className="bg-[#ff0059] hover:bg-red-500 text-white py-2 px-6 rounded-md transition-all duration-300"
>
Submit Code
</motion.button>
</motion.footer>
<ToastContainer />
</motion.div>
);
};
export default CodeRun;