fix file submissions

This commit is contained in:
rngsurrounded
2025-03-03 05:56:40 +09:00
parent 7bbb34f574
commit 87872fbdec
3 changed files with 67 additions and 13 deletions
@@ -7,17 +7,23 @@ interface FileSolutionProps {
setSelectedFile: (file: File | null) => void; setSelectedFile: (file: File | null) => void;
fileInputRef: React.RefObject<HTMLInputElement>; fileInputRef: React.RefObject<HTMLInputElement>;
existingFileUrl?: string | null; existingFileUrl?: string | null;
onClearExistingFile?: () => void; // New prop to clear existing file URL
} }
const FileSolution: React.FC<FileSolutionProps> = ({ const FileSolution: React.FC<FileSolutionProps> = ({
selectedFile, selectedFile,
setSelectedFile, setSelectedFile,
fileInputRef, fileInputRef,
existingFileUrl = null existingFileUrl = null,
onClearExistingFile
}) => { }) => {
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => { const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (event.target.files && event.target.files[0]) { if (event.target.files && event.target.files[0]) {
setSelectedFile(event.target.files[0]); setSelectedFile(event.target.files[0]);
// Clear existing file URL when a new file is selected
if (existingFileUrl && onClearExistingFile) {
onClearExistingFile();
}
} }
}; };
@@ -41,7 +47,26 @@ const FileSolution: React.FC<FileSolutionProps> = ({
if (e.dataTransfer.files && e.dataTransfer.files[0]) { if (e.dataTransfer.files && e.dataTransfer.files[0]) {
setSelectedFile(e.dataTransfer.files[0]); setSelectedFile(e.dataTransfer.files[0]);
// Clear existing file URL when a new file is dropped
if (existingFileUrl && onClearExistingFile) {
onClearExistingFile();
} }
}
};
// Handle clearing the file
const handleClearFile = () => {
setSelectedFile(null);
// Also clear the existing file URL if it exists
if (existingFileUrl && onClearExistingFile) {
onClearExistingFile();
}
};
// Handle selecting a new file when an existing file is shown
const handleSelectNewFile = () => {
// Just trigger the file input click - the actual clearing will happen in handleFileChange
fileInputRef.current?.click();
}; };
const fileName = selectedFile const fileName = selectedFile
@@ -79,13 +104,33 @@ const FileSolution: React.FC<FileSolutionProps> = ({
Скачать Скачать
</a> </a>
)} )}
{selectedFile ? (
<Button <Button
variant="ghost" variant="ghost"
className="text-blue-500 text-sm p-0 h-auto hover:bg-transparent hover:text-blue-600 font-hse-sans" className="text-blue-500 text-sm p-0 h-auto hover:bg-transparent hover:text-blue-600 font-hse-sans"
onClick={() => setSelectedFile(null)} onClick={handleClearFile}
> >
{!selectedFile && existingFileUrl ? "Выбрать другой файл" : "Очистить"} Очистить
</Button> </Button>
) : existingFileUrl ? (
<div className="flex gap-3">
<Button
variant="ghost"
className="text-blue-500 text-sm p-0 h-auto hover:bg-transparent hover:text-blue-600 font-hse-sans"
onClick={handleSelectNewFile}
>
Выбрать другой файл
</Button>
<Button
variant="ghost"
className="text-red-500 text-sm p-0 h-auto hover:bg-transparent hover:text-red-600 font-hse-sans"
onClick={handleClearFile}
>
Очистить
</Button>
</div>
) : null}
</div> </div>
</div> </div>
</div> </div>
@@ -44,15 +44,18 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
setIsHistoryOpen(true); setIsHistoryOpen(true);
}; };
const latestSolution = solutionHistory && solutionHistory.length > 0 ? solutionHistory[solutionHistory.length - 1] : null; const latestSolution = solutionHistory && solutionHistory.length > 0 ? solutionHistory[0] : null;
const handleSolutionSelect = async (solution: Solution) => { const handleSolutionSelect = async (solution: Solution) => {
if (!solution.content) return; if (!solution.content) return;
setSelectedSolutionUrl(solution.content);
try { try {
if (task.type !== TaskType.FILE) { if (task.type === TaskType.FILE) {
// For file tasks, just store the URL
setSelectedFile(null); // Clear any selected file first
setSelectedSolutionUrl(solution.content);
} else {
// For INPUT and CODE tasks, fetch the content and set as answer
const response = await fetch(solution.content); const response = await fetch(solution.content);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to fetch solution content: ${response.status}`); throw new Error(`Failed to fetch solution content: ${response.status}`);
@@ -65,6 +68,11 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
} }
}; };
// Function to clear the existing file URL
const handleClearExistingFile = () => {
setSelectedSolutionUrl(null);
};
return ( return (
<div className="md:w-[500px] flex flex-col gap-4"> <div className="md:w-[500px] flex flex-col gap-4">
{latestSolution ? ( {latestSolution ? (
@@ -85,6 +93,7 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
setSelectedFile={setSelectedFile} setSelectedFile={setSelectedFile}
fileInputRef={fileInputRef} fileInputRef={fileInputRef}
existingFileUrl={selectedSolutionUrl} existingFileUrl={selectedSolutionUrl}
onClearExistingFile={handleClearExistingFile}
/> />
)} )}
@@ -55,7 +55,7 @@ export function CompetitionCard({
<span></span> <span></span>
<span className="text-primary-foreground"> <span className="text-primary-foreground">
{competition.state === CompetitionState.STARTED {competition.state === CompetitionState.STARTED
? "В прогрессе" ? "Проводится сейчас"
: "Завершено"} : "Завершено"}
</span> </span>
</> </>