Skip to content

Commit

Permalink
Make dragging work
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaloney111 committed Jun 13, 2024
1 parent 614efcf commit 90b0009
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 47 deletions.
85 changes: 40 additions & 45 deletions src/components/selectFileCard/selectFileCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Papa from "papaparse";
import CsvTable from "../csvTable/csvTable";
import { Link } from "@mui/material";

interface props {
interface Props {
selectedFile: File | null;
setSelectedFile: (file: File | null) => void;
setData: (data: any[][]) => void;
Expand All @@ -20,46 +20,46 @@ export default function SelectFileCard({
data,
header,
setHeader,
}: props) {
}: Props) {
const [isDragging, setIsDragging] = useState<boolean>(false);

// Selecionar arquivo
// Handle file selection from file input
const handleFileChange = async (event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file && file.name.endsWith(".csv")) {
setSelectedFile(file);

Papa.parse(file, {
header: true,
dynamicTyping: true,
skipEmptyLines: true,
complete(results, file) {
let chaves = Object.keys(results.data[0] || []);

let data: any[][] = results.data.map((row: any) => {
let newRow: any[] = [];
chaves.forEach((chave) => {
newRow.push(row[chave]);
});
return newRow;
});

setData(data);
setHeader(chaves);
},
});
parseCSV(file);
} else {
setSelectedFile(null);
}
};

// Parse CSV file
const parseCSV = (file: File) => {
Papa.parse(file, {
header: true,
dynamicTyping: true,
skipEmptyLines: true,
complete(results) {
const keys = Object.keys(results.data[0] || []);
const data: any[][] = results.data.map((row: any) => {
return keys.map((key) => row[key]);
});
setData(data);
setHeader(keys);
},
});
};

// Handle file drop
const handleDrop = (event: React.DragEvent<HTMLDivElement>) => {
setIsDragging(false);
event.preventDefault();

const file = event.dataTransfer.files[0];
if (file && file.name.endsWith(".csv")) {
setSelectedFile(file);
parseCSV(file);
} else {
setSelectedFile(null);
}
Expand All @@ -83,36 +83,35 @@ export default function SelectFileCard({
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
>

<Icon
icon="ic:outline-upload-file"
className="text-gray"
width="50"
/>


<h2 className="mb-0">Arraste e solte ou <a
className="underline cursor-pointer"
onClick={() => {
const fileInput = document.getElementById("fileInput");
if (fileInput) {
fileInput.click();
}
}}
>
selecione do Computador
</a>
<h2 className="mb-0">
Arraste e solte ou{" "}
<a
className="underline cursor-pointer"
onClick={() => {
const fileInput = document.getElementById("fileInput");
if (fileInput) {
fileInput.click();
}
}}
>
selecione do Computador
</a>
<input
type="file"
id="fileInput"
data-testid="fileInput"
style={{ display: "none" }}
accept=".csv"
onChange={handleFileChange}
/></h2>
/>
</h2>
</div>
) : (

<div
className={`${data.length > 0 ? `w-4/5` : `w-2/5`
} min-h-[170px] relative mx-auto flex flex-col items-center justify-center ${isDragging ? "blur-sm" : ""
Expand All @@ -121,11 +120,7 @@ export default function SelectFileCard({
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
>


{data.length > 0 && (
<CsvTable data={data.slice(0, 4)} head={header} />
)}
{data.length > 0 && <CsvTable data={data.slice(0, 4)} head={header} />}
</div>
)
);
}
3 changes: 1 addition & 2 deletions src/pages/views/trainView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default function TrainView() {
const newProgress: number =
training_in_progress || training_progress === 100
? training_progress
: 0; // Explicitly type newProgress
: 0;
updateLoadingProgress(newProgress);

setTrainLosses(train_losses);
Expand All @@ -179,7 +179,6 @@ export default function TrainView() {
};

const updateLoadingProgress = (newProgress: number) => {
// Explicitly type newProgress parameter
const duration = 1000;
const startTime = Date.now();
const startProgress = prevLoadingProgressRef.current;
Expand Down

0 comments on commit 90b0009

Please sign in to comment.