mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-17 15:36:50 +00:00
AddSite useSite
This commit is contained in:
parent
d1a289c4fa
commit
4f182ca21d
@ -21,30 +21,26 @@ export default function AddNetwork({ onNetworkAdded, siteId }: AddNetworkProps)
|
|||||||
const { addNetwork } = useNetworks();
|
const { addNetwork } = useNetworks();
|
||||||
|
|
||||||
const handleAddNetwork = async () => {
|
const handleAddNetwork = async () => {
|
||||||
|
const response = addNetwork({
|
||||||
|
id: "0",
|
||||||
|
siteId,
|
||||||
|
name,
|
||||||
|
ipv4Subnet,
|
||||||
|
ipv6Subnet,
|
||||||
|
gateway
|
||||||
|
});
|
||||||
|
if (typeof response === "string") {
|
||||||
|
setError(response)
|
||||||
|
return
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const response = addNetwork({
|
const successMessage = await response
|
||||||
id: "0",
|
if (onNetworkAdded && successMessage) {
|
||||||
siteId,
|
onNetworkAdded()
|
||||||
name,
|
setSuccess("Network added successfully")
|
||||||
ipv4Subnet,
|
|
||||||
ipv6Subnet,
|
|
||||||
gateway
|
|
||||||
});
|
|
||||||
if (typeof response === "string") {
|
|
||||||
setError(response)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
try {
|
} catch (apiError: any) {
|
||||||
const successMessage = await response
|
setError(apiError)
|
||||||
if (onNetworkAdded && successMessage) {
|
|
||||||
onNetworkAdded()
|
|
||||||
setSuccess("Network added successfully")
|
|
||||||
}
|
|
||||||
} catch (apiError: any) {
|
|
||||||
setError(apiError)
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
setError("Failed to add network")
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import axios from "axios";
|
|||||||
import ErrorToast from "@/components/Error";
|
import ErrorToast from "@/components/Error";
|
||||||
import SuccessToast from "@/components/Success";
|
import SuccessToast from "@/components/Success";
|
||||||
import { Building2, MapPin, FileText } from "lucide-react";
|
import { Building2, MapPin, FileText } from "lucide-react";
|
||||||
|
import useSite from "@/hooks/useSite";
|
||||||
|
|
||||||
interface AddSiteProps {
|
interface AddSiteProps {
|
||||||
onSiteAdded?: () => void;
|
onSiteAdded?: () => void;
|
||||||
@ -15,30 +16,22 @@ export default function AddSite({ onSiteAdded }: AddSiteProps) {
|
|||||||
const [description, setDescription] = useState("");
|
const [description, setDescription] = useState("");
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
const [success, setSuccess] = useState("");
|
const [success, setSuccess] = useState("");
|
||||||
|
const { addSite } = useSite();
|
||||||
|
|
||||||
const addSite = async () => {
|
const addSiteHandler = async () => {
|
||||||
|
const response = addSite({ id: "", name, description, networks: [] });
|
||||||
|
if (typeof response === "string") {
|
||||||
|
setError(response)
|
||||||
|
return
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const response = await axios.post("/api/sites/add", { name, description });
|
const successMessage = await response
|
||||||
if (response.status === 201) {
|
if (onSiteAdded && successMessage) {
|
||||||
const site = response.data.site;
|
onSiteAdded()
|
||||||
if (site) {
|
setSuccess("Site added successfully")
|
||||||
setName("");
|
|
||||||
setDescription("");
|
|
||||||
setSuccess("Site added successfully");
|
|
||||||
|
|
||||||
document.getElementById('add_site')?.querySelector('form')?.dispatchEvent(
|
|
||||||
new Event('submit', { cancelable: true })
|
|
||||||
);
|
|
||||||
|
|
||||||
if (onSiteAdded) {
|
|
||||||
onSiteAdded();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
setError("No site received");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (apiError: any) {
|
||||||
setError(error.response.data.error);
|
setError(apiError)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -96,7 +89,7 @@ export default function AddSite({ onSiteAdded }: AddSiteProps) {
|
|||||||
<button className="btn btn-outline">Cancel</button>
|
<button className="btn btn-outline">Cancel</button>
|
||||||
<button
|
<button
|
||||||
className="btn btn-success text-success-content"
|
className="btn btn-success text-success-content"
|
||||||
onClick={addSite}
|
onClick={addSiteHandler}
|
||||||
>
|
>
|
||||||
Add Site
|
Add Site
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user