add command api on submit device
This commit is contained in:
@@ -1,19 +1,16 @@
|
||||
import { useState } from "react";
|
||||
import {
|
||||
getDevices,
|
||||
saveDevices,
|
||||
getMerchant,
|
||||
} from "../../../utils/storage";
|
||||
import { getDevices, saveDevices, getMerchant } from "../../../utils/storage";
|
||||
import { useNavigate } from "@tanstack/react-router";
|
||||
|
||||
|
||||
import { useDeviceCommand } from "../../home/hooks/mutations";
|
||||
|
||||
export default function AddDeviceFeature() {
|
||||
const [form, setForm] = useState<DeviceData | null >(null);
|
||||
const [form, setForm] = useState<DeviceData | null>(null);
|
||||
const navigate = useNavigate();
|
||||
|
||||
const merchant = getMerchant();
|
||||
|
||||
const { isPending, mutate } = useDeviceCommand();
|
||||
|
||||
const isMerchantValid =
|
||||
merchant?.merchantName &&
|
||||
merchant?.tower &&
|
||||
@@ -27,7 +24,7 @@ export default function AddDeviceFeature() {
|
||||
form?.deviceLabel?.trim();
|
||||
|
||||
function handleChange(
|
||||
e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>
|
||||
e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>,
|
||||
) {
|
||||
const { name, value } = e.target;
|
||||
setForm((prev) => ({ ...prev, [name]: value }));
|
||||
@@ -49,8 +46,30 @@ export default function AddDeviceFeature() {
|
||||
};
|
||||
|
||||
saveDevices([...devices, deviceToSave]);
|
||||
setForm(null);
|
||||
navigate({ to: "/" });
|
||||
|
||||
mutate(
|
||||
{
|
||||
merchantName: merchant.merchantName,
|
||||
towerNumber: merchant.tower,
|
||||
floorName: merchant.floor,
|
||||
unitNumber: merchant.unit,
|
||||
commandType: "C",
|
||||
deviceName: form?.deviceName,
|
||||
deviceType: form?.deviceType,
|
||||
payload: { action: "On" },
|
||||
roomName: form?.roomName,
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
setForm(null);
|
||||
navigate({ to: "/" });
|
||||
},
|
||||
onError: () => {
|
||||
setForm(null);
|
||||
navigate({ to: "/" });
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -125,7 +144,7 @@ export default function AddDeviceFeature() {
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={!isMerchantValid || !isFormValid}
|
||||
disabled={!isMerchantValid || !isFormValid || isPending}
|
||||
className="
|
||||
w-full py-2 rounded-lg mt-8 text-white cursor-pointer
|
||||
bg-orange-500
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
ChefHat,
|
||||
DoorClosed,
|
||||
Fan,
|
||||
ImageOff,
|
||||
Lightbulb,
|
||||
Minus,
|
||||
Plus,
|
||||
@@ -158,7 +159,7 @@ export default function Devices() {
|
||||
<div
|
||||
className={`h-12 w-12 rounded-full flex justify-center items-center ${item.status ? "bg-orange-500 text-white" : "bg-gray-200 text-gray-700"}`}
|
||||
>
|
||||
{icons[item.deviceName as keyof typeof icons]?.icon}
|
||||
{icons[item.deviceName as keyof typeof icons]?.icon ?? <ImageOff size={24} />}
|
||||
</div>
|
||||
{!["FT1"].includes(item.deviceName ?? "") && (
|
||||
<Switch
|
||||
@@ -189,6 +190,9 @@ export default function Devices() {
|
||||
{icons[item.deviceName as keyof typeof icons]?.label ??
|
||||
item.deviceName}
|
||||
</p>
|
||||
<p className="text-xs font-medium text-gray-700 mb-2">
|
||||
Kode: {item.deviceType}/{item.deviceName}
|
||||
</p>
|
||||
<p className="text-xs font-medium text-gray-700 mb-2">
|
||||
Status: {item?.payload?.toString()}
|
||||
</p>
|
||||
|
||||
Reference in New Issue
Block a user