2025-05-18 12:04:31 +02:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useState } from "react";
|
|
|
|
|
import ErrorToast from "@/components/Error";
|
|
|
|
|
import SuccessToast from "@/components/Success";
|
2025-05-18 20:15:23 +02:00
|
|
|
import { Building2, MapPin, FileText } from "lucide-react";
|
2025-05-20 20:23:08 +02:00
|
|
|
import useSite from "@/hooks/useSites";
|
2025-05-18 12:04:31 +02:00
|
|
|
|
2025-05-18 12:50:56 +02:00
|
|
|
interface AddSiteProps {
|
|
|
|
|
onSiteAdded?: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function AddSite({ onSiteAdded }: AddSiteProps) {
|
2025-05-18 12:04:31 +02:00
|
|
|
const [name, setName] = useState("");
|
|
|
|
|
const [description, setDescription] = useState("");
|
|
|
|
|
const [error, setError] = useState("");
|
|
|
|
|
const [success, setSuccess] = useState("");
|
2025-05-20 19:53:23 +02:00
|
|
|
const { addSite } = useSite();
|
2025-05-18 12:04:31 +02:00
|
|
|
|
2025-05-20 19:53:23 +02:00
|
|
|
const addSiteHandler = async () => {
|
2025-05-24 21:15:52 +02:00
|
|
|
const response = addSite({ id: 0, name, description, networks: [] });
|
2025-05-20 19:53:23 +02:00
|
|
|
if (typeof response === "string") {
|
|
|
|
|
setError(response)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-05-18 12:04:31 +02:00
|
|
|
try {
|
2025-05-20 19:53:23 +02:00
|
|
|
const successMessage = await response
|
|
|
|
|
if (onSiteAdded && successMessage) {
|
|
|
|
|
onSiteAdded()
|
|
|
|
|
setSuccess("Site added successfully")
|
2025-05-18 12:04:31 +02:00
|
|
|
}
|
2025-05-20 19:53:23 +02:00
|
|
|
} catch (apiError: any) {
|
|
|
|
|
setError(apiError)
|
2025-05-20 20:06:32 +02:00
|
|
|
} finally {
|
|
|
|
|
setName("")
|
|
|
|
|
setDescription("")
|
2025-05-18 12:04:31 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-18 11:53:40 +02:00
|
|
|
return (
|
2025-05-18 12:04:31 +02:00
|
|
|
<div>
|
|
|
|
|
<dialog id="add_site" className="modal">
|
2025-05-18 20:15:23 +02:00
|
|
|
<div className="modal-box w-11/12 max-w-3xl border-l-4 border-success">
|
|
|
|
|
<div className="flex items-center gap-3 mb-3">
|
|
|
|
|
<div className="bg-success text-success-content rounded-full p-2 flex items-center justify-center">
|
|
|
|
|
<Building2 className="h-6 w-6" />
|
|
|
|
|
</div>
|
|
|
|
|
<h3 className="font-bold text-xl">Add New Site</h3>
|
|
|
|
|
</div>
|
2025-05-18 12:04:31 +02:00
|
|
|
|
2025-05-18 20:15:23 +02:00
|
|
|
<div className="bg-base-200 p-4 rounded-lg mb-4">
|
|
|
|
|
<div className="space-y-4">
|
2025-05-18 12:04:31 +02:00
|
|
|
<div className="form-control">
|
|
|
|
|
<label className="label">
|
2025-05-18 20:15:23 +02:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<MapPin className="h-4 w-4 opacity-70" />
|
|
|
|
|
<span className="label-text font-medium">Site Name</span>
|
|
|
|
|
</div>
|
2025-05-18 12:04:31 +02:00
|
|
|
</label>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
2025-05-18 20:15:23 +02:00
|
|
|
className="input input-bordered w-full"
|
2025-05-18 12:04:31 +02:00
|
|
|
placeholder="e.g. Downtown Office"
|
|
|
|
|
value={name}
|
|
|
|
|
onChange={(e) => setName(e.target.value)}
|
2025-05-18 20:15:23 +02:00
|
|
|
required
|
2025-05-18 12:04:31 +02:00
|
|
|
/>
|
|
|
|
|
</div>
|
2025-05-18 11:53:40 +02:00
|
|
|
|
2025-05-18 12:04:31 +02:00
|
|
|
<div className="form-control">
|
|
|
|
|
<label className="label">
|
2025-05-18 20:15:23 +02:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<FileText className="h-4 w-4 opacity-70" />
|
|
|
|
|
<span className="label-text font-medium">Description</span>
|
|
|
|
|
<span className="ml-1 text-xs opacity-60">(optional)</span>
|
|
|
|
|
</div>
|
2025-05-18 12:04:31 +02:00
|
|
|
</label>
|
|
|
|
|
<textarea
|
2025-05-18 20:15:23 +02:00
|
|
|
className="textarea textarea-bordered w-full"
|
|
|
|
|
placeholder="Enter a brief description of this site..."
|
2025-05-18 12:04:31 +02:00
|
|
|
rows={4}
|
|
|
|
|
value={description}
|
|
|
|
|
onChange={(e) => setDescription(e.target.value)}
|
|
|
|
|
></textarea>
|
|
|
|
|
</div>
|
2025-05-18 11:53:40 +02:00
|
|
|
</div>
|
2025-05-18 20:15:23 +02:00
|
|
|
</div>
|
2025-05-18 11:53:40 +02:00
|
|
|
|
2025-05-18 20:15:23 +02:00
|
|
|
<div className="modal-action">
|
|
|
|
|
<form method="dialog" className="flex gap-3 w-full justify-end">
|
|
|
|
|
<button className="btn btn-outline">Cancel</button>
|
|
|
|
|
<button
|
|
|
|
|
className="btn btn-success text-success-content"
|
2025-05-20 19:53:23 +02:00
|
|
|
onClick={addSiteHandler}
|
2025-05-18 20:15:23 +02:00
|
|
|
>
|
|
|
|
|
Add Site
|
|
|
|
|
</button>
|
|
|
|
|
</form>
|
2025-05-18 11:53:40 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-05-18 12:04:31 +02:00
|
|
|
</dialog>
|
|
|
|
|
<ErrorToast message={error} show={error !== ''} onClose={() => setError('')} />
|
|
|
|
|
<SuccessToast message={success} show={success !== ''} onClose={() => setSuccess('')} />
|
|
|
|
|
</div>
|
2025-05-18 20:15:23 +02:00
|
|
|
);
|
2025-05-18 11:53:40 +02:00
|
|
|
}
|