diff --git a/src/App.js b/src/App.js index 8ebd2b2..8f5e0e5 100644 --- a/src/App.js +++ b/src/App.js @@ -14,6 +14,12 @@ const theme = deepMerge(grommet, { brand: "#ff4e4e", text: "#336", }, + input: { + padding: { + horizontal: "small", + vertical: "xsmall", + }, + }, focus: { border: { color: "#ff4e4e80", @@ -28,6 +34,16 @@ const theme = deepMerge(grommet, { radius: "4px", }, }, + tabs: { + header: { + background: "#ccc", + }, + }, + tab: { + margin: { + vertical: "xsmall", + }, + }, }); function App() { diff --git a/src/apolloclient.js b/src/apolloclient.js index 37b9589..c6326ea 100644 --- a/src/apolloclient.js +++ b/src/apolloclient.js @@ -69,6 +69,13 @@ const client = new ApolloClient({ }, }, }), + resolvers: { + Device: { + messages() { + return []; + }, + }, + }, }); const CONNECTED = gql` @@ -119,6 +126,7 @@ const GETDEVICES = gql` devices { id ...DeviceInfo + messages @client properties { id name @@ -136,6 +144,7 @@ const GETDEVICE = gql` device(id: $id) { id ...DeviceInfo + messages @client properties { id name @@ -155,6 +164,7 @@ client .subscribe(({ data }) => { const res = client.readQuery({ query: GETDEVICES }); data.newDevice.properties = []; + data.newDevice.messages = []; client.writeQuery({ query: GETDEVICES, data: { devices: [...res.devices, data.newDevice] }, @@ -239,4 +249,41 @@ client } }); +const NEW_MESSAGE = gql` + subscription newMessage { + newMessage { + id + device { + id + } + message + } + } +`; + +client + .subscribe({ + query: NEW_MESSAGE, + }) + .subscribe(({ data }) => { + const res = client.readQuery({ + query: GETDEVICE, + variables: { id: data.newMessage.device.id }, + }); + const messages = res.device.messages + ? [data.newMessage.message, ...res.device.messages] + : [data.newMessage.message]; + + client.writeQuery({ + query: GETDEVICE, + variables: { id: res.device.id }, + data: { + device: { + ...res.device, + messages, + }, + }, + }); + }); + export default client; diff --git a/src/components/indi/device.js b/src/components/indi/device.js index b253430..a7a256c 100644 --- a/src/components/indi/device.js +++ b/src/components/indi/device.js @@ -1,9 +1,10 @@ -import React, { useState, useEffect } from "react"; -import { useMutation } from "@apollo/react-hooks"; -import { driversToInterfaces } from "../../utils/indi"; +import React, { useState, useEffect, Fragment } from "react"; +import { useMutation, useApolloClient } from "@apollo/react-hooks"; +import { msgToColor } from "../../utils/indi"; import Property from "./property"; import gql from "graphql-tag"; -import { Box, Button, Tabs, Tab } from "grommet"; +import { Box, Button, Tabs, Tab, Text, Layer } from "grommet"; +import { Close, Trash } from "grommet-icons"; const CONNECT_DEVICE = gql` mutation connectDevice($id: String!) { @@ -13,7 +14,14 @@ const CONNECT_DEVICE = gql` } `; -const Device = ({ device }) => { +const MSGS_FRAGMENT = gql` + fragment DeviceMsgs on Device { + messages + } +`; + +const Device = ({ device, messages, onMessagesClosed }) => { + const client = useApolloClient(); const [connect] = useMutation(CONNECT_DEVICE, { variables: { id: device.id }, }); @@ -40,12 +48,16 @@ const Device = ({ device }) => { }} /> )} - { - setCurrentTab(nextTab); - const newValue = { ...selectedTabs }; - newValue[device.id] = nextTab; - setSelectedTabs(newValue) - }}> + { + setCurrentTab(nextTab); + const newValue = { ...selectedTabs }; + newValue[device.id] = nextTab; + setSelectedTabs(newValue); + }} + > {groups.map((g) => ( @@ -57,6 +69,32 @@ const Device = ({ device }) => { ))} + + {messages ? ( + + + diff --git a/src/components/indi/property.js b/src/components/indi/property.js index 6d8f70a..66afaa0 100644 --- a/src/components/indi/property.js +++ b/src/components/indi/property.js @@ -1,5 +1,5 @@ import React from "react"; -import { Box } from "grommet"; +import { Box, Text } from "grommet"; import { StatusGoodSmall } from "grommet-icons"; import { stateToColor } from "../../utils/indi"; @@ -16,7 +16,7 @@ const Property = ({ property }) => { size="medium" color={stateToColor(property.vector.state)} /> - {property.vector.label || property.name} + {property.vector.label || property.name} {property.vector.__typename === "NumberVector" && ( diff --git a/src/components/indi/switch.js b/src/components/indi/switch.js index 3b1f5a2..eafeb13 100644 --- a/src/components/indi/switch.js +++ b/src/components/indi/switch.js @@ -35,14 +35,8 @@ const SwitchVector = ({ vector }) => { property: vector.name, values: newValues, }; - console.log(vector.rule); - console.log(input); - sendSwitch({ - variables: { input }, - }).then((data) => { - console.log(data); - }); + sendSwitch({ variables: { input } }); }} > ))} diff --git a/src/components/indi/text.js b/src/components/indi/text.js index 0b4235e..3c7666a 100644 --- a/src/components/indi/text.js +++ b/src/components/indi/text.js @@ -1,7 +1,59 @@ -import React from "react"; -import { Box, Button, TextInput } from "grommet"; +import React, { useState } from "react"; +import { Box, Button, TextInput, Text } from "grommet"; +import gql from "graphql-tag"; +import { useMutation } from "@apollo/react-hooks"; + +const SEND = gql` + mutation sendOneText($input: OneTextInput!) { + sendOneText(input: $input) + } +`; + +const TextValue = ({ vector, value }) => { + const [newval, setNewval] = useState(value.text); + const [sendOneText] = useMutation(SEND); + + return ( + + {value.label || value.name} + {value.text} + {vector.permission !== 0 && ( + + setNewval(e.target.value)} + > + + + )} + + ); +}; const TextVector = ({ vector }) => { + return ( + + {vector.values.map((v) => ( + + ))} + + ); +}; + +/*const TextVector = ({ vector }) => { return ( {vector.values.map((v) => ( @@ -18,6 +70,6 @@ const TextVector = ({ vector }) => { ))} ); -}; +};*/ export default TextVector; \ No newline at end of file diff --git a/src/devices.js b/src/devices.js index c01eb20..4a7087c 100644 --- a/src/devices.js +++ b/src/devices.js @@ -4,7 +4,8 @@ import { useQuery, useSubscription, useMutation } from "@apollo/react-hooks"; import gql from "graphql-tag"; import { deviceInfoFragment, propertyVectorFragment } from "./graphql/fragment"; import Device from "./components/indi/device"; -import { Box, Tabs, Tab, Button, Header, Heading } from "grommet"; +import { Box, Tabs, Tab, Button, Header, Heading, Layer } from "grommet"; +import { Contact } from "grommet-icons"; import { isConnectedQuery } from "./graphql/query"; const GETDEVICES = gql` @@ -12,6 +13,7 @@ const GETDEVICES = gql` devices { id ...DeviceInfo + messages @client properties { id name @@ -52,6 +54,7 @@ const Devices = () => { const { data } = useQuery(GETDEVICES); const [connect] = useMutation(CONNECT); const [disconnect] = useMutation(DISCONNECT); + const [openMsg, setOpenMsg] = useState(false); if (!data || !data.devices) { return
No data
; @@ -66,16 +69,8 @@ const Devices = () => { -