Create concentrator logic
This commit is contained in:
@@ -1,16 +1,19 @@
|
|||||||
import { useState, useEffect, useMemo } from "react";
|
import { useState, useEffect, useMemo } from "react";
|
||||||
import { Plus, Trash2, Pencil, RefreshCcw } from "lucide-react";
|
import { Plus, Trash2, Pencil, RefreshCcw } from "lucide-react";
|
||||||
import MaterialTable from "@material-table/core";
|
import MaterialTable from "@material-table/core";
|
||||||
import { fetchProjects } from "./concentrators.api";
|
import { fetchProjects, createConcentrator } from "./concentrators.api";
|
||||||
|
|
||||||
/* ================= TYPES ================= */
|
/* ================= TYPES ================= */
|
||||||
interface Concentrator {
|
interface Concentrator {
|
||||||
id: number;
|
"Area Name": string;
|
||||||
name: string;
|
"Device S/N": string;
|
||||||
location: string;
|
"Device Name": string;
|
||||||
status: "ACTIVE" | "INACTIVE";
|
"Device Time": string;
|
||||||
project: string;
|
"Device Status": string;
|
||||||
createdAt: string;
|
"Operator": string;
|
||||||
|
"Installed Time": string;
|
||||||
|
"Communication Time": string;
|
||||||
|
"Instruction Manual": string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface User {
|
interface User {
|
||||||
@@ -67,28 +70,37 @@ export default function ConcentratorsPage() {
|
|||||||
|
|
||||||
const [concentrators, setConcentrators] = useState<Concentrator[]>([
|
const [concentrators, setConcentrators] = useState<Concentrator[]>([
|
||||||
{
|
{
|
||||||
id: 1,
|
"Area Name": "GRH (PADRE)",
|
||||||
name: "Concentrador A",
|
"Device S/N": "SN001",
|
||||||
location: "Planta 1",
|
"Device Name": "Concentrador A",
|
||||||
status: "ACTIVE",
|
"Device Time": "2025-12-17T10:00:00Z",
|
||||||
project: "GRH (PADRE)",
|
"Device Status": "ACTIVE",
|
||||||
createdAt: "2025-12-17",
|
"Operator": "Operador 1",
|
||||||
|
"Installed Time": "2025-12-17",
|
||||||
|
"Communication Time": "2025-12-17T10:30:00Z",
|
||||||
|
"Instruction Manual": "Manual A",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 2,
|
"Area Name": "CESPT",
|
||||||
name: "Concentrador B",
|
"Device S/N": "SN002",
|
||||||
location: "Planta 2",
|
"Device Name": "Concentrador B",
|
||||||
status: "INACTIVE",
|
"Device Time": "2025-12-16T11:00:00Z",
|
||||||
project: "CESPT",
|
"Device Status": "INACTIVE",
|
||||||
createdAt: "2025-12-16",
|
"Operator": "Operador 2",
|
||||||
|
"Installed Time": "2025-12-16",
|
||||||
|
"Communication Time": "2025-12-16T11:30:00Z",
|
||||||
|
"Instruction Manual": "Manual B",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 3,
|
"Area Name": "Proyecto A",
|
||||||
name: "Concentrador C",
|
"Device S/N": "SN003",
|
||||||
location: "Planta 3",
|
"Device Name": "Concentrador C",
|
||||||
status: "ACTIVE",
|
"Device Time": "2025-12-15T12:00:00Z",
|
||||||
project: "Proyecto A",
|
"Device Status": "ACTIVE",
|
||||||
createdAt: "2025-12-15",
|
"Operator": "Operador 3",
|
||||||
|
"Installed Time": "2025-12-15",
|
||||||
|
"Communication Time": "2025-12-15T12:30:00Z",
|
||||||
|
"Instruction Manual": "Manual C",
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -96,40 +108,53 @@ export default function ConcentratorsPage() {
|
|||||||
const [search, setSearch] = useState("");
|
const [search, setSearch] = useState("");
|
||||||
|
|
||||||
const [showModal, setShowModal] = useState(false);
|
const [showModal, setShowModal] = useState(false);
|
||||||
const [editingId, setEditingId] = useState<number | null>(null);
|
const [editingSerial, setEditingSerial] = useState<string | null>(null);
|
||||||
|
|
||||||
const getEmptyConcentrator = (): Omit<Concentrator, "id"> => ({
|
const getEmptyConcentrator = (): Omit<Concentrator, "id"> => ({
|
||||||
name: "",
|
"Area Name": selectedProject,
|
||||||
location: "",
|
"Device S/N": "",
|
||||||
status: "ACTIVE",
|
"Device Name": "",
|
||||||
project: selectedProject,
|
"Device Time": new Date().toISOString(),
|
||||||
createdAt: new Date().toISOString().slice(0, 10),
|
"Device Status": "ACTIVE",
|
||||||
|
"Operator": "",
|
||||||
|
"Installed Time": new Date().toISOString().slice(0, 10),
|
||||||
|
"Communication Time": new Date().toISOString(),
|
||||||
|
"Instruction Manual": "",
|
||||||
});
|
});
|
||||||
|
|
||||||
const [form, setForm] = useState<Omit<Concentrator, "id">>(getEmptyConcentrator());
|
const [form, setForm] = useState<Omit<Concentrator, "id">>(getEmptyConcentrator());
|
||||||
|
|
||||||
/* ================= CRUD ================= */
|
/* ================= CRUD ================= */
|
||||||
const handleSave = () => {
|
const handleSave = async () => {
|
||||||
if (editingId) {
|
try {
|
||||||
|
if (editingSerial) {
|
||||||
setConcentrators((prev) =>
|
setConcentrators((prev) =>
|
||||||
prev.map((c) =>
|
prev.map((c) =>
|
||||||
c.id === editingId ? { id: editingId, ...form } : c
|
c["Device S/N"] === editingSerial ? { ...form } : c
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
const newId = Date.now();
|
await createConcentrator(form);
|
||||||
setConcentrators((prev) => [...prev, { id: newId, ...form }]);
|
setConcentrators((prev) => [...prev, { ...form }]);
|
||||||
}
|
}
|
||||||
setShowModal(false);
|
setShowModal(false);
|
||||||
setEditingId(null);
|
setEditingSerial(null);
|
||||||
setForm({ ...getEmptyConcentrator(), project: selectedProject });
|
setForm({ ...getEmptyConcentrator(), "Area Name": selectedProject });
|
||||||
setActiveConcentrator(null);
|
setActiveConcentrator(null);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error saving concentrator:', error);
|
||||||
|
setConcentrators((prev) => [...prev, { ...form }]);
|
||||||
|
setShowModal(false);
|
||||||
|
setEditingSerial(null);
|
||||||
|
setForm({ ...getEmptyConcentrator(), "Area Name": selectedProject });
|
||||||
|
setActiveConcentrator(null);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = () => {
|
const handleDelete = () => {
|
||||||
if (!activeConcentrator) return;
|
if (!activeConcentrator) return;
|
||||||
setConcentrators((prev) =>
|
setConcentrators((prev) =>
|
||||||
prev.filter((c) => c.id !== activeConcentrator.id)
|
prev.filter((c) => c["Device S/N"] !== activeConcentrator["Device S/N"])
|
||||||
);
|
);
|
||||||
setActiveConcentrator(null);
|
setActiveConcentrator(null);
|
||||||
};
|
};
|
||||||
@@ -137,9 +162,9 @@ export default function ConcentratorsPage() {
|
|||||||
/* ================= FILTER ================= */
|
/* ================= FILTER ================= */
|
||||||
const filtered = concentrators.filter(
|
const filtered = concentrators.filter(
|
||||||
(c) =>
|
(c) =>
|
||||||
(c.name.toLowerCase().includes(search.toLowerCase()) ||
|
(c["Device Name"].toLowerCase().includes(search.toLowerCase()) ||
|
||||||
c.location.toLowerCase().includes(search.toLowerCase())) &&
|
c["Device S/N"].toLowerCase().includes(search.toLowerCase())) &&
|
||||||
c.project === selectedProject
|
c["Area Name"] === selectedProject
|
||||||
);
|
);
|
||||||
|
|
||||||
/* ================= UI ================= */
|
/* ================= UI ================= */
|
||||||
@@ -184,8 +209,8 @@ export default function ConcentratorsPage() {
|
|||||||
<div className="flex gap-3">
|
<div className="flex gap-3">
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setForm({ ...getEmptyConcentrator(), project: selectedProject });
|
setForm({ ...getEmptyConcentrator(), "Area Name": selectedProject });
|
||||||
setEditingId(null);
|
setEditingSerial(null);
|
||||||
setShowModal(true);
|
setShowModal(true);
|
||||||
}}
|
}}
|
||||||
className="flex items-center gap-2 px-4 py-2 bg-white text-[#4c5f9e] rounded-lg"
|
className="flex items-center gap-2 px-4 py-2 bg-white text-[#4c5f9e] rounded-lg"
|
||||||
@@ -196,8 +221,8 @@ export default function ConcentratorsPage() {
|
|||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (!activeConcentrator) return;
|
if (!activeConcentrator) return;
|
||||||
setEditingId(activeConcentrator.id);
|
setEditingSerial(activeConcentrator["Device S/N"]);
|
||||||
setForm({ ...activeConcentrator });
|
setForm(activeConcentrator);
|
||||||
setShowModal(true);
|
setShowModal(true);
|
||||||
}}
|
}}
|
||||||
disabled={!activeConcentrator}
|
disabled={!activeConcentrator}
|
||||||
@@ -235,25 +260,26 @@ export default function ConcentratorsPage() {
|
|||||||
<MaterialTable
|
<MaterialTable
|
||||||
title="Concentrators"
|
title="Concentrators"
|
||||||
columns={[
|
columns={[
|
||||||
{ title: "Name", field: "name" },
|
{ title: "Device Name", field: "Device Name" },
|
||||||
|
{ title: "Device S/N", field: "Device S/N" },
|
||||||
{
|
{
|
||||||
title: "Status",
|
title: "Device Status",
|
||||||
field: "status",
|
field: "Device Status",
|
||||||
render: (rowData) => (
|
render: (rowData) => (
|
||||||
<span
|
<span
|
||||||
className={`px-3 py-1 rounded-full text-xs font-semibold border ${
|
className={`px-3 py-1 rounded-full text-xs font-semibold border ${
|
||||||
rowData.status === "ACTIVE"
|
rowData["Device Status"] === "ACTIVE"
|
||||||
? "text-blue-600 border-blue-600"
|
? "text-blue-600 border-blue-600"
|
||||||
: "text-red-600 border-red-600"
|
: "text-red-600 border-red-600"
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
{rowData.status}
|
{rowData["Device Status"]}
|
||||||
</span>
|
</span>
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{ title: "Location", field: "location" },
|
{ title: "Operator", field: "Operator" },
|
||||||
{ title: "Project", field: "project" },
|
{ title: "Area Name", field: "Area Name" },
|
||||||
{ title: "Created", field: "createdAt", type: "date" },
|
{ title: "Installed Time", field: "Installed Time", type: "date" },
|
||||||
]}
|
]}
|
||||||
data={filtered}
|
data={filtered}
|
||||||
onRowClick={(_, rowData) => setActiveConcentrator(rowData as Concentrator)}
|
onRowClick={(_, rowData) => setActiveConcentrator(rowData as Concentrator)}
|
||||||
@@ -264,7 +290,7 @@ export default function ConcentratorsPage() {
|
|||||||
sorting: true,
|
sorting: true,
|
||||||
rowStyle: (rowData) => ({
|
rowStyle: (rowData) => ({
|
||||||
backgroundColor:
|
backgroundColor:
|
||||||
activeConcentrator?.id === (rowData as Concentrator).id
|
activeConcentrator?.["Device S/N"] === (rowData as Concentrator)["Device S/N"]
|
||||||
? "#EEF2FF"
|
? "#EEF2FF"
|
||||||
: "#FFFFFF",
|
: "#FFFFFF",
|
||||||
}),
|
}),
|
||||||
@@ -277,41 +303,93 @@ export default function ConcentratorsPage() {
|
|||||||
<div className="fixed inset-0 bg-black/40 flex items-center justify-center">
|
<div className="fixed inset-0 bg-black/40 flex items-center justify-center">
|
||||||
<div className="bg-white rounded-xl p-6 w-96 space-y-3">
|
<div className="bg-white rounded-xl p-6 w-96 space-y-3">
|
||||||
<h2 className="text-lg font-semibold">
|
<h2 className="text-lg font-semibold">
|
||||||
{editingId ? "Edit Concentrator" : "Add Concentrator"}
|
{editingSerial ? "Edit Concentrator" : "Add Concentrator"}
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
|
<div className="space-y-1">
|
||||||
|
<label className="block text-sm font-medium text-gray-700">Device Name</label>
|
||||||
<input
|
<input
|
||||||
className="w-full border px-3 py-2 rounded"
|
className="w-full border px-3 py-2 rounded"
|
||||||
placeholder="Name"
|
placeholder="Enter device name"
|
||||||
value={form.name}
|
value={form["Device Name"]}
|
||||||
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
onChange={(e) => setForm({ ...form, "Device Name": e.target.value })}
|
||||||
/>
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-1">
|
||||||
|
<label className="block text-sm font-medium text-gray-700">Device S/N</label>
|
||||||
<input
|
<input
|
||||||
className="w-full border px-3 py-2 rounded"
|
className="w-full border px-3 py-2 rounded"
|
||||||
placeholder="Location"
|
placeholder="Enter device serial number"
|
||||||
value={form.location}
|
value={form["Device S/N"]}
|
||||||
onChange={(e) => setForm({ ...form, location: e.target.value })}
|
onChange={(e) => setForm({ ...form, "Device S/N": e.target.value })}
|
||||||
/>
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-1">
|
||||||
|
<label className="block text-sm font-medium text-gray-700">Operator</label>
|
||||||
|
<input
|
||||||
|
className="w-full border px-3 py-2 rounded"
|
||||||
|
placeholder="Enter operator name"
|
||||||
|
value={form["Operator"]}
|
||||||
|
onChange={(e) => setForm({ ...form, "Operator": e.target.value })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-1">
|
||||||
|
<label className="block text-sm font-medium text-gray-700">Instruction Manual</label>
|
||||||
|
<input
|
||||||
|
className="w-full border px-3 py-2 rounded"
|
||||||
|
placeholder="Enter instruction manual"
|
||||||
|
value={form["Instruction Manual"]}
|
||||||
|
onChange={(e) => setForm({ ...form, "Instruction Manual": e.target.value })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-1">
|
||||||
|
<label className="block text-sm font-medium text-gray-700">Device Status</label>
|
||||||
<button
|
<button
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
setForm({
|
setForm({
|
||||||
...form,
|
...form,
|
||||||
status: form.status === "ACTIVE" ? "INACTIVE" : "ACTIVE",
|
"Device Status": form["Device Status"] === "ACTIVE" ? "INACTIVE" : "ACTIVE",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
className="w-full border rounded px-3 py-2"
|
className="w-full border rounded px-3 py-2 hover:bg-gray-50"
|
||||||
>
|
>
|
||||||
Status: {form.status}
|
Status: {form["Device Status"]}
|
||||||
</button>
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-1">
|
||||||
|
<label className="block text-sm font-medium text-gray-700">Installed Time</label>
|
||||||
<input
|
<input
|
||||||
type="date"
|
type="date"
|
||||||
className="w-full border px-3 py-2 rounded"
|
className="w-full border px-3 py-2 rounded"
|
||||||
value={form.createdAt}
|
value={form["Installed Time"]}
|
||||||
onChange={(e) => setForm({ ...form, createdAt: e.target.value })}
|
onChange={(e) => setForm({ ...form, "Installed Time": e.target.value })}
|
||||||
/>
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-1">
|
||||||
|
<label className="block text-sm font-medium text-gray-700">Device Time</label>
|
||||||
|
<input
|
||||||
|
type="datetime-local"
|
||||||
|
className="w-full border px-3 py-2 rounded"
|
||||||
|
value={form["Device Time"].slice(0, 16)}
|
||||||
|
onChange={(e) => setForm({ ...form, "Device Time": new Date(e.target.value).toISOString() })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-1">
|
||||||
|
<label className="block text-sm font-medium text-gray-700">Communication Time</label>
|
||||||
|
<input
|
||||||
|
type="datetime-local"
|
||||||
|
className="w-full border px-3 py-2 rounded"
|
||||||
|
value={form["Communication Time"].slice(0, 16)}
|
||||||
|
onChange={(e) => setForm({ ...form, "Communication Time": new Date(e.target.value).toISOString() })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="flex justify-end gap-2 pt-3">
|
<div className="flex justify-end gap-2 pt-3">
|
||||||
<button onClick={() => setShowModal(false)}>Cancel</button>
|
<button onClick={() => setShowModal(false)}>Cancel</button>
|
||||||
|
|||||||
@@ -22,6 +22,36 @@ interface ProjectsResponse {
|
|||||||
nestedPrev?: string;
|
nestedPrev?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const createConcentrator = async (concentratorData: {
|
||||||
|
"Area Name": string;
|
||||||
|
"Device S/N": string;
|
||||||
|
"Device Name": string;
|
||||||
|
"Device Time": string;
|
||||||
|
"Device Status": string;
|
||||||
|
"Operator": string;
|
||||||
|
"Installed Time": string;
|
||||||
|
"Communication Time": string;
|
||||||
|
"Instruction Manual": string;
|
||||||
|
}): Promise<void> => {
|
||||||
|
try {
|
||||||
|
// const response = await fetch('/api/v3/data/ppfu31vhv5gf6i0/mqqvi3woqdw5ziq/records', {
|
||||||
|
// method: 'POST',
|
||||||
|
// headers: {
|
||||||
|
// 'Content-Type': 'application/json',
|
||||||
|
// },
|
||||||
|
// body: JSON.stringify({ fields: concentratorData }),
|
||||||
|
// });
|
||||||
|
// if (!response.ok) {
|
||||||
|
// throw new Error('Failed to create concentrator');
|
||||||
|
// }
|
||||||
|
|
||||||
|
console.log('Creating concentrator with data:', concentratorData);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error creating concentrator:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const fetchProjects = async (): Promise<string[]> => {
|
export const fetchProjects = async (): Promise<string[]> => {
|
||||||
try {
|
try {
|
||||||
// const response = await fetch('/api/v3/data/ppfu31vhv5gf6i0/m05u6wpquvdbv3c/records');
|
// const response = await fetch('/api/v3/data/ppfu31vhv5gf6i0/m05u6wpquvdbv3c/records');
|
||||||
|
|||||||
Reference in New Issue
Block a user