add command api on submit device
This commit is contained in:
@@ -1,19 +1,16 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import {
|
import { getDevices, saveDevices, getMerchant } from "../../../utils/storage";
|
||||||
getDevices,
|
|
||||||
saveDevices,
|
|
||||||
getMerchant,
|
|
||||||
} from "../../../utils/storage";
|
|
||||||
import { useNavigate } from "@tanstack/react-router";
|
import { useNavigate } from "@tanstack/react-router";
|
||||||
|
import { useDeviceCommand } from "../../home/hooks/mutations";
|
||||||
|
|
||||||
|
|
||||||
export default function AddDeviceFeature() {
|
export default function AddDeviceFeature() {
|
||||||
const [form, setForm] = useState<DeviceData | null >(null);
|
const [form, setForm] = useState<DeviceData | null>(null);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const merchant = getMerchant();
|
const merchant = getMerchant();
|
||||||
|
|
||||||
|
const { isPending, mutate } = useDeviceCommand();
|
||||||
|
|
||||||
const isMerchantValid =
|
const isMerchantValid =
|
||||||
merchant?.merchantName &&
|
merchant?.merchantName &&
|
||||||
merchant?.tower &&
|
merchant?.tower &&
|
||||||
@@ -27,7 +24,7 @@ export default function AddDeviceFeature() {
|
|||||||
form?.deviceLabel?.trim();
|
form?.deviceLabel?.trim();
|
||||||
|
|
||||||
function handleChange(
|
function handleChange(
|
||||||
e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>
|
e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>,
|
||||||
) {
|
) {
|
||||||
const { name, value } = e.target;
|
const { name, value } = e.target;
|
||||||
setForm((prev) => ({ ...prev, [name]: value }));
|
setForm((prev) => ({ ...prev, [name]: value }));
|
||||||
@@ -36,9 +33,9 @@ export default function AddDeviceFeature() {
|
|||||||
function handleSubmit(e: React.FormEvent) {
|
function handleSubmit(e: React.FormEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (!isMerchantValid || !isFormValid) return;
|
if (!isMerchantValid || !isFormValid) return;
|
||||||
|
|
||||||
const devices = getDevices();
|
const devices = getDevices();
|
||||||
|
|
||||||
const deviceToSave: DeviceData = {
|
const deviceToSave: DeviceData = {
|
||||||
...form,
|
...form,
|
||||||
id: new Date().getTime().toString(),
|
id: new Date().getTime().toString(),
|
||||||
@@ -47,10 +44,32 @@ export default function AddDeviceFeature() {
|
|||||||
unitNumber: merchant.unit,
|
unitNumber: merchant.unit,
|
||||||
deviceLabel: form?.deviceLabel,
|
deviceLabel: form?.deviceLabel,
|
||||||
};
|
};
|
||||||
|
|
||||||
saveDevices([...devices, deviceToSave]);
|
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 (
|
return (
|
||||||
@@ -125,7 +144,7 @@ export default function AddDeviceFeature() {
|
|||||||
|
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
disabled={!isMerchantValid || !isFormValid}
|
disabled={!isMerchantValid || !isFormValid || isPending}
|
||||||
className="
|
className="
|
||||||
w-full py-2 rounded-lg mt-8 text-white cursor-pointer
|
w-full py-2 rounded-lg mt-8 text-white cursor-pointer
|
||||||
bg-orange-500
|
bg-orange-500
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import {
|
|||||||
ChefHat,
|
ChefHat,
|
||||||
DoorClosed,
|
DoorClosed,
|
||||||
Fan,
|
Fan,
|
||||||
|
ImageOff,
|
||||||
Lightbulb,
|
Lightbulb,
|
||||||
Minus,
|
Minus,
|
||||||
Plus,
|
Plus,
|
||||||
@@ -158,7 +159,7 @@ export default function Devices() {
|
|||||||
<div
|
<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"}`}
|
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>
|
</div>
|
||||||
{!["FT1"].includes(item.deviceName ?? "") && (
|
{!["FT1"].includes(item.deviceName ?? "") && (
|
||||||
<Switch
|
<Switch
|
||||||
@@ -189,6 +190,9 @@ export default function Devices() {
|
|||||||
{icons[item.deviceName as keyof typeof icons]?.label ??
|
{icons[item.deviceName as keyof typeof icons]?.label ??
|
||||||
item.deviceName}
|
item.deviceName}
|
||||||
</p>
|
</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">
|
<p className="text-xs font-medium text-gray-700 mb-2">
|
||||||
Status: {item?.payload?.toString()}
|
Status: {item?.payload?.toString()}
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
Reference in New Issue
Block a user