From 53a3e03c86cd232ff05c4b58d930b3f02604081f Mon Sep 17 00:00:00 2001 From: dikapratana Date: Mon, 26 Jan 2026 14:41:33 +0700 Subject: [PATCH] add command api on submit device --- src/features/device/add/index.tsx | 49 ++++++++++++++++++-------- src/features/home/sections/devices.tsx | 6 +++- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/features/device/add/index.tsx b/src/features/device/add/index.tsx index a98f801..f130d12 100644 --- a/src/features/device/add/index.tsx +++ b/src/features/device/add/index.tsx @@ -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(null); + const [form, setForm] = useState(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 + e: React.ChangeEvent, ) { const { name, value } = e.target; setForm((prev) => ({ ...prev, [name]: value })); @@ -36,9 +33,9 @@ export default function AddDeviceFeature() { function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!isMerchantValid || !isFormValid) return; - + const devices = getDevices(); - + const deviceToSave: DeviceData = { ...form, id: new Date().getTime().toString(), @@ -47,10 +44,32 @@ export default function AddDeviceFeature() { unitNumber: merchant.unit, deviceLabel: form?.deviceLabel, }; - + 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() {