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 handleAddNetwork = async () => {
|
||||
const response = addNetwork({
|
||||
id: "0",
|
||||
siteId,
|
||||
name,
|
||||
ipv4Subnet,
|
||||
ipv6Subnet,
|
||||
gateway
|
||||
});
|
||||
if (typeof response === "string") {
|
||||
setError(response)
|
||||
return
|
||||
}
|
||||
try {
|
||||
const response = addNetwork({
|
||||
id: "0",
|
||||
siteId,
|
||||
name,
|
||||
ipv4Subnet,
|
||||
ipv6Subnet,
|
||||
gateway
|
||||
});
|
||||
if (typeof response === "string") {
|
||||
setError(response)
|
||||
return
|
||||
const successMessage = await response
|
||||
if (onNetworkAdded && successMessage) {
|
||||
onNetworkAdded()
|
||||
setSuccess("Network added successfully")
|
||||
}
|
||||
try {
|
||||
const successMessage = await response
|
||||
if (onNetworkAdded && successMessage) {
|
||||
onNetworkAdded()
|
||||
setSuccess("Network added successfully")
|
||||
}
|
||||
} catch (apiError: any) {
|
||||
setError(apiError)
|
||||
}
|
||||
} catch (err) {
|
||||
setError("Failed to add network")
|
||||
} catch (apiError: any) {
|
||||
setError(apiError)
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ import axios from "axios";
|
||||
import ErrorToast from "@/components/Error";
|
||||
import SuccessToast from "@/components/Success";
|
||||
import { Building2, MapPin, FileText } from "lucide-react";
|
||||
import useSite from "@/hooks/useSite";
|
||||
|
||||
interface AddSiteProps {
|
||||
onSiteAdded?: () => void;
|
||||
@ -15,30 +16,22 @@ export default function AddSite({ onSiteAdded }: AddSiteProps) {
|
||||
const [description, setDescription] = useState("");
|
||||
const [error, setError] = 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 {
|
||||
const response = await axios.post("/api/sites/add", { name, description });
|
||||
if (response.status === 201) {
|
||||
const site = response.data.site;
|
||||
if (site) {
|
||||
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");
|
||||
}
|
||||
const successMessage = await response
|
||||
if (onSiteAdded && successMessage) {
|
||||
onSiteAdded()
|
||||
setSuccess("Site added successfully")
|
||||
}
|
||||
} catch (error: any) {
|
||||
setError(error.response.data.error);
|
||||
} catch (apiError: any) {
|
||||
setError(apiError)
|
||||
}
|
||||
};
|
||||
|
||||
@ -96,7 +89,7 @@ export default function AddSite({ onSiteAdded }: AddSiteProps) {
|
||||
<button className="btn btn-outline">Cancel</button>
|
||||
<button
|
||||
className="btn btn-success text-success-content"
|
||||
onClick={addSite}
|
||||
onClick={addSiteHandler}
|
||||
>
|
||||
Add Site
|
||||
</button>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user