fe->be
This commit is contained in:
1 parent
f94afb63c6
commit
83d56f1ff6
2 files changed
+14
-12
No files matched your search
@@ -8,6 +8,7 @@ import { useTranslation } from "react-i18next";
|
|||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
import DummyChart from "@/Pages/Settings/DummyChart";
|
import DummyChart from "@/Pages/Settings/DummyChart";
|
||||||
import { useGet, usePatch, usePost, useLazyGet } from "@/Hooks/UseApi";
|
import { useGet, usePatch, usePost, useLazyGet } from "@/Hooks/UseApi";
|
||||||
|
import { useToast } from "@/Hooks/UseToast";
|
||||||
import { useSettingsForm } from "@/Hooks/useSettingsForm";
|
import { useSettingsForm } from "@/Hooks/useSettingsForm";
|
||||||
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
||||||
import type { SettingsFormData } from "@/Validation/settings";
|
import type { SettingsFormData } from "@/Validation/settings";
|
||||||
@@ -44,10 +45,13 @@ export const SettingsPage = () => {
|
|||||||
const { t, i18n } = useTranslation();
|
const { t, i18n } = useTranslation();
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const isAdmin = useIsAdmin();
|
const isAdmin = useIsAdmin();
|
||||||
|
const { toastError } = useToast();
|
||||||
// Local state for demo monitors dialog
|
// Local state for demo monitors dialog
|
||||||
const [isDemoMonitorsDialogOpen, setIsDemoMonitorsDialogOpen] = useState(false);
|
const [isDemoMonitorsDialogOpen, setIsDemoMonitorsDialogOpen] = useState(false);
|
||||||
const { post: postDemoMonitors, loading: isPostingDemoMonitors } = usePost();
|
const { post: postDemoMonitors, loading: isPostingDemoMonitors } = usePost();
|
||||||
const { deleteFn: deleteAllMonitors, loading: isDeletingAllMonitors } = useDelete();
|
const { deleteFn: deleteAllMonitors, loading: isDeletingAllMonitors } = useDelete();
|
||||||
|
// Import monitors functionality
|
||||||
|
const { post: importMonitors, loading: isImportingMonitors } = usePost();
|
||||||
|
|
||||||
// Fetch settings data from API
|
// Fetch settings data from API
|
||||||
const { data: fetchedSettings } = useGet<SettingsResponse>("/settings");
|
const { data: fetchedSettings } = useGet<SettingsResponse>("/settings");
|
||||||
@@ -209,26 +213,25 @@ export const SettingsPage = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (file.type !== "application/json") {
|
if (file.type !== "application/json") {
|
||||||
alert("Please select a valid JSON file");
|
toastError("Please select a valid JSON file");
|
||||||
event.target.value = ""; // Reset input
|
event.target.value = "";
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the file contents
|
|
||||||
try {
|
try {
|
||||||
const text = await file.text();
|
const text = await file.text();
|
||||||
const monitors = JSON.parse(text);
|
const monitors = JSON.parse(text);
|
||||||
|
|
||||||
if (!Array.isArray(monitors)) {
|
if (!Array.isArray(monitors)) {
|
||||||
alert("Invalid file format: expected an array of monitors");
|
toastError("Invalid file format: expected an array of monitors");
|
||||||
event.target.value = "";
|
event.target.value = "";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log(monitors);
|
|
||||||
event.target.value = ""; // Reset input
|
await importMonitors("/monitors/import/json", { monitors });
|
||||||
|
|
||||||
|
event.target.value = "";
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Import error:", error);
|
toastError("Error parsing JSON file. Please check the file format.");
|
||||||
alert("Error importing monitors. Please check the file format.");
|
|
||||||
event.target.value = "";
|
event.target.value = "";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -914,6 +917,7 @@ export const SettingsPage = () => {
|
|||||||
<Button
|
<Button
|
||||||
variant="contained"
|
variant="contained"
|
||||||
onClick={() => document.getElementById("monitor-import-input")?.click()}
|
onClick={() => document.getElementById("monitor-import-input")?.click()}
|
||||||
|
disabled={isImportingMonitors}
|
||||||
>
|
>
|
||||||
{t("common.buttons.importFromJSON")}
|
{t("common.buttons.importFromJSON")}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -499,7 +499,6 @@ export class MonitorService implements IMonitorService {
|
|||||||
}): Promise<{ imported: number; errors: string[] }> => {
|
}): Promise<{ imported: number; errors: string[] }> => {
|
||||||
const errors: string[] = [];
|
const errors: string[] = [];
|
||||||
|
|
||||||
// Clean the data: remove id, _id, createdAt, updatedAt, recentChecks fields
|
|
||||||
const cleanedMonitors = monitors.map((monitor) => {
|
const cleanedMonitors = monitors.map((monitor) => {
|
||||||
const cleanData = { ...monitor };
|
const cleanData = { ...monitor };
|
||||||
delete cleanData.id;
|
delete cleanData.id;
|
||||||
@@ -507,12 +506,11 @@ export class MonitorService implements IMonitorService {
|
|||||||
delete cleanData.createdAt;
|
delete cleanData.createdAt;
|
||||||
delete cleanData.updatedAt;
|
delete cleanData.updatedAt;
|
||||||
delete cleanData.recentChecks;
|
delete cleanData.recentChecks;
|
||||||
// Force the monitor to belong to the current team
|
// Monitors must belong to current team
|
||||||
cleanData.teamId = teamId;
|
cleanData.teamId = teamId;
|
||||||
return cleanData;
|
return cleanData;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Use createBulkMonitors for efficient bulk insert
|
|
||||||
const createdMonitors = await this.createBulkMonitors(cleanedMonitors, userId, teamId);
|
const createdMonitors = await this.createBulkMonitors(cleanedMonitors, userId, teamId);
|
||||||
|
|
||||||
if (!createdMonitors || createdMonitors.length === 0) {
|
if (!createdMonitors || createdMonitors.length === 0) {
|
||||||
|
|||||||
Reference in new issue
Block a user