Moved the parsing logic to backend as suggested.
This commit is contained in:
1 parent
b2fd9b1e89
commit
bd8554707d
10 files changed
+165
-103
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import { useState } from "react";
|
||||
import { networkService } from "../main"; // Your network service
|
||||
|
||||
export const useBulkMonitors = () => {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
const createBulkMonitors = async (file, user) => {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("csvFile", file);
|
||||
formData.append("userId", user._id);
|
||||
formData.append("teamId", user.teamId);
|
||||
|
||||
try {
|
||||
const response = await networkService.createBulkMonitors(formData);
|
||||
return response.data;
|
||||
} catch (err) {
|
||||
setError(err?.response?.data?.msg ?? err.message);
|
||||
return null;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return { createBulkMonitors, isLoading, error };
|
||||
};
|
||||
@@ -1,11 +1,10 @@
|
||||
import { useTheme } from "@emotion/react";
|
||||
import { useState, useRef, useEffect } from "react";
|
||||
import { useState, useRef } from "react";
|
||||
import { Button, Typography } from "@mui/material";
|
||||
import { parse } from "papaparse";
|
||||
import { bulkMonitorsValidation } from "../../../Validation/validation";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import PropTypes from "prop-types";
|
||||
|
||||
export function Upload({ onComplete }) {
|
||||
const UploadFile = ({ onFileSelect }) => { // Changed prop to onFileSelect
|
||||
const theme = useTheme();
|
||||
const [file, setFile] = useState();
|
||||
const [error, setError] = useState("");
|
||||
@@ -18,42 +17,20 @@ export function Upload({ onComplete }) {
|
||||
|
||||
const handleFileChange = (e) => {
|
||||
setError("");
|
||||
setFile(e.target.files[0]);
|
||||
const selectedFile = e.target.files[0];
|
||||
|
||||
// Basic file validation
|
||||
if (!selectedFile) return;
|
||||
|
||||
if (!selectedFile.name.endsWith('.csv')) {
|
||||
setError(t("bulkImport.invalidFileType"));
|
||||
return;
|
||||
}
|
||||
|
||||
setFile(selectedFile);
|
||||
onFileSelect(selectedFile); // Pass the file directly to parent
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!file) return;
|
||||
parse(file, {
|
||||
header: true,
|
||||
skipEmptyLines: true,
|
||||
transform: (value, header) => {
|
||||
if (!value) {
|
||||
return undefined;
|
||||
}
|
||||
if (header === "port" || header === "interval") {
|
||||
return parseInt(value);
|
||||
}
|
||||
return value;
|
||||
},
|
||||
complete: ({ data, errors }) => {
|
||||
if (errors.length > 0) {
|
||||
setError(t("bulkImport.parsingFailed"));
|
||||
return;
|
||||
}
|
||||
const { error } = bulkMonitorsValidation.validate(data);
|
||||
if (error) {
|
||||
setError(
|
||||
error.details?.[0]?.message ||
|
||||
error.message ||
|
||||
t("bulkImport.validationFailed")
|
||||
);
|
||||
return;
|
||||
}
|
||||
onComplete(data);
|
||||
},
|
||||
});
|
||||
}, [file]);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<input
|
||||
@@ -68,7 +45,7 @@ export function Upload({ onComplete }) {
|
||||
mb={theme.spacing(1.5)}
|
||||
sx={{ wordBreak: "break-all" }}
|
||||
>
|
||||
{file?.name}
|
||||
{file?.name || t("bulkImport.noFileSelected")}
|
||||
</Typography>
|
||||
<Typography
|
||||
component="div"
|
||||
@@ -86,4 +63,11 @@ export function Upload({ onComplete }) {
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
UploadFile.prototype = {
|
||||
onFileSelect: PropTypes.func.isRequired,
|
||||
};
|
||||
|
||||
export default UploadFile;
|
||||
@@ -5,45 +5,41 @@ import { useState } from "react";
|
||||
import { Box, Stack, Typography, Button, Link } from "@mui/material";
|
||||
|
||||
//Components
|
||||
import { networkService } from "../../../main";
|
||||
import { createToast } from "../../../Utils/toastUtils";
|
||||
import Breadcrumbs from "../../../Components/Breadcrumbs";
|
||||
import ConfigBox from "../../../Components/ConfigBox";
|
||||
import { Upload } from "./Upload";
|
||||
import { UploadFile } from "./Upload";
|
||||
import { useSelector } from "react-redux";
|
||||
import { useNavigate } from "react-router";
|
||||
import { Trans, useTranslation } from "react-i18next";
|
||||
import { useBulkMonitors } from "../../../Hooks/useBulkMonitors";
|
||||
|
||||
const BulkImport = () => {
|
||||
const theme = useTheme();
|
||||
|
||||
const [monitors, setMonitors] = useState([]);
|
||||
const { user } = useSelector((state) => state.auth);
|
||||
const navigate = useNavigate();
|
||||
const { t } = useTranslation();
|
||||
const [selectedFile, setSelectedFile] = useState(null);
|
||||
|
||||
const crumbs = [
|
||||
{ name: t("uptime"), path: "/uptime" },
|
||||
{ name: t("bulkImport.title"), path: `/uptime/bulk-import` },
|
||||
];
|
||||
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const { createBulkMonitors, isLoading: hookLoading, error } = useBulkMonitors();
|
||||
|
||||
const handleSubmit = async () => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const monitorsWithUser = monitors.map((monitor) => ({
|
||||
...monitor,
|
||||
description: monitor.name || monitor.url,
|
||||
teamId: user.teamId,
|
||||
userId: user._id,
|
||||
}));
|
||||
await networkService.createBulkMonitors({ monitors: monitorsWithUser });
|
||||
if (!selectedFile) {
|
||||
createToast({ body: t("bulkImport.noFileSelected") });
|
||||
return;
|
||||
}
|
||||
const success = await createBulkMonitors(selectedFile, user);
|
||||
|
||||
if (success) {
|
||||
createToast({ body: t("bulkImport.uploadSuccess") });
|
||||
navigate("/uptime");
|
||||
} catch (error) {
|
||||
createToast({ body: error?.response?.data?.msg ?? error.message });
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
} else {
|
||||
createToast({ body: error });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -63,9 +59,7 @@ const BulkImport = () => {
|
||||
</Typography>
|
||||
<ConfigBox>
|
||||
<Box>
|
||||
<Typography component="h2">
|
||||
{t("bulkImport.selectFileTips")}
|
||||
</Typography>
|
||||
<Typography component="h2">{t("bulkImport.selectFileTips")}</Typography>
|
||||
<Typography component="p">
|
||||
<Trans
|
||||
i18nKey="bulkImport.selectFileDescription"
|
||||
@@ -90,7 +84,7 @@ const BulkImport = () => {
|
||||
</Box>
|
||||
<Stack gap={theme.spacing(12)}>
|
||||
<Stack gap={theme.spacing(6)}>
|
||||
<Upload onComplete={setMonitors} />
|
||||
<UploadFile onFileSelect={(file) => setSelectedFile(file)} />
|
||||
</Stack>
|
||||
</Stack>
|
||||
</ConfigBox>
|
||||
@@ -102,8 +96,8 @@ const BulkImport = () => {
|
||||
variant="contained"
|
||||
color="accent"
|
||||
onClick={handleSubmit}
|
||||
disabled={!monitors?.length}
|
||||
loading={isLoading}
|
||||
disabled={hookLoading}
|
||||
loading={hookLoading}
|
||||
>
|
||||
{t("submit")}
|
||||
</Button>
|
||||
@@ -113,4 +107,4 @@ const BulkImport = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default BulkImport;
|
||||
export default BulkImport;
|
||||
@@ -1090,8 +1090,13 @@ class NetworkService {
|
||||
// Create bulk monitors
|
||||
// ************************************
|
||||
|
||||
async createBulkMonitors({ monitors }) {
|
||||
return this.axiosInstance.post(`/monitors/bulk`, monitors);
|
||||
async createBulkMonitors(formData) {
|
||||
const response = await this.axiosInstance.post(`/monitors/bulk`, formData, {
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
}
|
||||
|
||||
// ************************************
|
||||
|
||||
@@ -403,6 +403,7 @@
|
||||
"selectFile": "Select File",
|
||||
"parsingFailed": "Parsing failed",
|
||||
"uploadSuccess": "Monitors created successfully!",
|
||||
"validationFailed": "Validation failed"
|
||||
"validationFailed": "Validation failed",
|
||||
"noFileSelected": "No file selected"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user