From d80284ff158e9d5a8ab58afff9f71f0a2710014e Mon Sep 17 00:00:00 2001 From: sam rolfe Date: Thu, 11 Sep 2025 17:13:25 +1000 Subject: [PATCH] prisma migration and fix dropdowns - working --- src/app/_components/.CommandManager.tsx.swp | Bin 0 -> 16384 bytes src/app/_components/CommandManager.tsx | 156 +++++++++++--------- 2 files changed, 83 insertions(+), 73 deletions(-) create mode 100644 src/app/_components/.CommandManager.tsx.swp diff --git a/src/app/_components/.CommandManager.tsx.swp b/src/app/_components/.CommandManager.tsx.swp new file mode 100644 index 0000000000000000000000000000000000000000..6fe9b298f0369fff147c5892017bcf72d27c6ed0 GIT binary patch literal 16384 zcmeHOTZ|iL6&}JRPzaYIQSo+mh#GIPJ$q|N9DA#<$%fEemc5i#A#BDozwOD6XNH;I z*)`UJc;Jl|+9FXwDau26=>yLQ0ulltmCHkD5i0Of3aIc9@x~+He;Ir1^%AE7RFSdt ztvxgU<(%)F|NQ6tYu9HUIeVUtmuC&U-ftL{<)5?~s~<96p@T+Zx5eGtzR<3;kT@}8 zLFp7rws;tg?PIl>>)95XTgT>(FD#v!J1+mu&dypJ35yf!6!U%SROoctEZ~W?%4~;Q zOD9)1&#bI%uCJb4dieC}=IZH_OY2q=JC+?q)}|A-qcD)|m%?`24&3v0U^iJ@=E=@j zIr5u119u$*9o{G%JG3!BH#;MA|A;w7AG~+tuA?DWIA1#h3w#CG0RDN% zF#Zg@0=x)Z2hIcc0f&Ga?=g%Qfaid31KYq1@Q-&J#`gdZxEDAGynGL203HQC4E*w4 zhVgyiYryA$Mc~zU8pciF2Jk%aRiFyI2Dx7XZUD~%PXS*7IzS7!3_J=*{oaK9=OX0{ zkMmm$q9TXH;*NV&;~P&dg98b^;fl+b*@j zYb>tlXCIrHF-PVh3WFuzbGB-|!Zg(u$(7Qqvv%a$oSpA*n|onUZi^4B&?C>SU{>|C z?76f%#D);PsuUfWXn+0JSm2H{enZ+f4yw3t``R6H4rB%q`PW%;4Hs2Li-fjC|JMQ$u`?l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LG+ZPDX|AVA$ue;x`r3@-8@lpix>%=PB$7Tl?tghl2B${h~9sOqrh;BIuVXjSR$v> zE-!UrI3|=gwYSPO-`5axD3LDdK1D>ov?RO?=`hQhJRsDrq+1J~WYcv)G1?7UR`;X^ ziv?4sKyp3kA=G-;p1OA`wiAUhr$#4mWMQZyq5d&3&MUJak#;NT)~Fk-#9@?(pl^s> zCfhcRekTlAhs6yPu;cp5uEGW=MMdE3#6I?!7UxmLve=G|gHk3IsMXLVXu0kMCfp8P zRGBH_jlAkthGHBR$ny*MU)%wOQ3xpz7cTV($FdcgDUu&F@lLy!)nKAV5FJ%@E@TMX z%mn#~II%S!3p?@J+SyaM6X6_r8>a#K?ux$DLZ!8U7)xr7@Li$VIm{%a9LnT}+Z@$R zWCosnB$ECL&EvE|4xv(L(I527A4~LaWSy7JL1uNMZe|I5&W|!LS~F zXbunQWj9K-f$YsrX_p#t*e3G}mhaW^OLfnL{nG((nQ6e|$gyx<;qYvmbRiTo3ojis dZETomg<}g32?Y9LrJfC_fvg5TH`(vw{{>fwr62$R literal 0 HcmV?d00001 diff --git a/src/app/_components/CommandManager.tsx b/src/app/_components/CommandManager.tsx index ada7a3b..619a5cf 100644 --- a/src/app/_components/CommandManager.tsx +++ b/src/app/_components/CommandManager.tsx @@ -1,9 +1,21 @@ // In file: src/app/_components/CommandManager.tsx "use client"; -import { useState } from "react"; +import { useState, useEffect } from "react"; import { api } from "~/trpc/react"; -import { postCommand, type DeviceCommandType } from "~/lib/api"; // Your Laravel API helper +import { postCommand, type DeviceCommandType } from "~/lib/api"; + +const predefinedPayloads: Record = { + lights: { on: true }, + camera: { on: true }, + sleep: { interval_sec: 300 }, + telemetry_sec: { interval: 60 }, + poll_sec: { interval: 120 }, + wifi: { target: "home", ssid: "YourSSID", password: "YourPassword" }, + ring_fence: { enabled: true, points: [{ lat: 0, lng: 0 }] }, + reboot: {}, + ota: { url: "http://example.com/firmware.bin" }, +}; interface CommandManagerProps { selectedImei: string; @@ -11,23 +23,28 @@ interface CommandManagerProps { export function CommandManager({ selectedImei }: CommandManagerProps) { const [name, setName] = useState(""); - const [type, setType] = useState("reboot"); - const [payload, setPayload] = useState(""); // Stored as a string for the textarea + const [type, setType] = useState("lights"); + const [payload, setPayload] = useState(""); + + // This useEffect hook updates the payload whenever the command type changes + useEffect(() => { + const newPayload = predefinedPayloads[type]; + setPayload(JSON.stringify(newPayload, null, 2)); // Pretty-print the JSON + }, [type]); // Dependency array: this runs only when 'type' changes const utils = api.useUtils(); const templatesQuery = api.commandTemplate.getAll.useQuery(); const createTemplateMutation = api.commandTemplate.create.useMutation({ onSuccess: () => { - utils.commandTemplate.getAll.invalidate(); // Invalidate cache to refetch list + utils.commandTemplate.getAll.invalidate(); setName(""); - setPayload(""); }, }); const deleteTemplateMutation = api.commandTemplate.delete.useMutation({ onSuccess: () => { - utils.commandTemplate.getAll.invalidate(); // Refetch list after deleting + utils.commandTemplate.getAll.invalidate(); }, }); @@ -40,23 +57,23 @@ export function CommandManager({ selectedImei }: CommandManagerProps) { } }; - const handleSendCommand = async (template: { type: string; payload: any }) => { - try { - await postCommand(selectedImei, { - type: template.type as DeviceCommandType, - payload: template.payload, - }); - alert(`Command "${template.type}" sent to ${selectedImei}!`); - } catch (e) { - alert(`Failed to send command: ${e instanceof Error ? e.message : "Unknown error"}`); - } + const handleSendCommand = async (template: { name: string, type: string; payload: any }) => { + try { + await postCommand(selectedImei, { + type: template.type as DeviceCommandType, + payload: template.payload, + }); + alert(`Command "${template.name}" sent to ${selectedImei}!`); + } catch (e) { + alert(`Failed to send command: ${e instanceof Error ? e.message : "Unknown error"}`); + } }; return (
{/* Form to add new templates */}
-

Create New Command Template

+

Create/Edit Command

setName(e.target.value)} className="p-2 text-white rounded-md bg-gray-800 border border-gray-600" /> - - -