@@ -7,18 +7,24 @@ import {
77} from "react-icons/ai" ;
88import { BsArrowRepeat , BsGlobe } from "react-icons/bs" ;
99import { FaNetworkWired } from "react-icons/fa" ;
10+ import { useRecoilCallback , useRecoilState , useRecoilValue } from "recoil" ;
11+ import { SimpleForm } from "../../components/forms/Form" ;
1012import { Pop } from "../../components/pop/Pop" ;
11- import Table from "../../components/table/Table" ;
13+ import { Table , Row } from "../../components/table/Table" ;
1214import {
1315 DeleteDropdownItem ,
16+ EditDropdownItem ,
1417 InteractionBadge ,
1518 OptionsDropdown ,
1619} from "../../components/utils/Common" ;
20+ import { getPage , InteractionOption } from "../../constants/globals" ;
1721import {
18- getPage ,
19- InteractionOption ,
20- InteractionOptions ,
21- } from "../../constants/globals" ;
22+ Instance ,
23+ instanceIds ,
24+ instances ,
25+ intanceFormFieldErrors ,
26+ } from "../../recoil/atoms/instances" ;
27+ import { InstanceFormFields } from "./InstanceForm" ;
2228
2329interface InstanceService {
2430 name : string ;
@@ -27,11 +33,34 @@ interface InstanceService {
2733 running : boolean ;
2834}
2935
30- interface Instance {
31- name : string ;
32- services : InstanceService [ ] ;
33- address ?: string ;
34- }
36+ const EditInstance = ( { instance } : { instance : Instance } ) => {
37+ const pageName = "Instances" ;
38+ // Get the page to set the icon
39+ const page = getPage ( pageName ) ;
40+
41+ // Create a setter for the submit
42+ const id = instance . id !== undefined ? instance . id : - 1 ;
43+ const onSubmit = useRecoilCallback ( ( { set } ) => ( instance : Instance ) => {
44+ set ( instances ( id ) , ( prev ) => ( { ...prev , ...instance } ) ) ;
45+ } ) ;
46+
47+ // Create the form with the default values as they currently are
48+ const content = (
49+ < SimpleForm
50+ create = { false }
51+ defaultValues = { instance }
52+ errors = { intanceFormFieldErrors }
53+ onSubmit = { onSubmit }
54+ page = { pageName }
55+ fields = { InstanceFormFields }
56+ />
57+ ) ;
58+
59+ // Get the form with the update tag
60+ return (
61+ < EditDropdownItem form = { content } icon = { page ?. icon } title = { "profile" } />
62+ ) ;
63+ } ;
3564
3665const ProxyInfoPop = ( { proxy } : { proxy : Number } ) => {
3766 return (
@@ -74,7 +103,13 @@ const InstanceRowInfoPop = ({ services }: { services: InstanceService[] }) => {
74103 ) ;
75104} ;
76105
77- const InstanceRowInfo = ( { name, services } : Instance ) => {
106+ const InstanceRowInfo = ( {
107+ name,
108+ services,
109+ } : {
110+ name : string ;
111+ services : InstanceService [ ] ;
112+ } ) => {
78113 return (
79114 < >
80115 < div > { name } </ div >
@@ -83,7 +118,13 @@ const InstanceRowInfo = ({ name, services }: Instance) => {
83118 ) ;
84119} ;
85120
86- const InstanceRowAddress = ( { value } : { value ?: string } ) => {
121+ const InstanceRowAddress = ( { id } : { id : number } ) => {
122+ const [ instance , setter ] = useRecoilState ( instances ( id ) ) ;
123+ const [ value , setValue ] = useState ( instance . host ) ;
124+ const onClick = ( ) => {
125+ setter ( { ...instance , host : value } ) ;
126+ } ;
127+
87128 return (
88129 < Col xs = "8" >
89130 < InputGroup size = "sm" className = "address" >
@@ -95,63 +136,74 @@ const InstanceRowAddress = ({ value }: { value?: string }) => {
95136 aria-label = "Instance's address"
96137 aria-describedby = "basic-addon2"
97138 value = { value }
139+ onChange = { ( e ) => setValue ( e . target . value ) }
98140 />
99- < Button variant = "outline-secondary" id = "button-addon2" >
141+ < Button
142+ variant = "outline-secondary"
143+ id = "button-addon2"
144+ onClick = { onClick }
145+ >
100146 < BsArrowRepeat />
101147 </ Button >
102148 </ InputGroup >
103149 </ Col >
104150 ) ;
105151} ;
106152
107- const InstanceRowOptions = ( { name } : { name : string } ) => {
153+ const InstanceRowOptions = ( { instance } : { instance : Instance } ) => {
154+ const ids = useRecoilValue ( instanceIds ) ;
155+
156+ const deleteCallback = useRecoilCallback (
157+ ( { set } ) =>
158+ ( id : number | undefined ) => {
159+ set (
160+ instanceIds ,
161+ ids . filter ( ( curr ) => curr !== id )
162+ ) ;
163+ }
164+ ) ;
165+
108166 const page = getPage ( "Instances" ) ;
109167 const note =
110168 "Instance services will be stopped and removed from the instance register." ;
111169 return (
112170 < OptionsDropdown >
171+ < EditInstance instance = { instance } />
113172 { page && (
114173 < DeleteDropdownItem
115- onClick = { ( ) => {
116- return ;
117- } }
118174 page = { page }
119175 note = { note }
120- name = { name }
176+ name = { instance . name }
177+ onClick = { ( ) => deleteCallback ( instance . id ) }
121178 />
122179 ) }
123180 </ OptionsDropdown >
124181 ) ;
125182} ;
126183
127- const InstanceRow = ( { name, address, services } : Instance ) => {
128- return [
129- < InstanceRowInfo name = { name } services = { services } /> ,
130- < InstanceRowAddress value = { address } /> ,
131- < InstanceRowOptions name = { name } /> ,
184+ const InstanceRow = ( { id } : { id : number } ) => {
185+ const instance = useRecoilValue ( instances ( id ) ) ;
186+ const services : InstanceService [ ] = [ ] ;
187+
188+ const cells = [
189+ < InstanceRowInfo name = { instance . name } services = { services } /> ,
190+ < InstanceRowAddress id = { id } /> ,
191+ < InstanceRowOptions instance = { instance } /> ,
132192 ] ;
193+
194+ return < Row cells = { cells } /> ;
133195} ;
134196
135197export const InstancesTable = ( ) => {
136- const props : Instance = {
137- name : "Lab1" ,
138- address : "127.0.0.11" ,
139- services : [
140- {
141- name : "CoAP" ,
142- proxy : 5683 ,
143- interaction : InteractionOptions [ 0 ] ,
144- running : true ,
145- } ,
146- ] ,
147- } ;
148-
149- const rows = [ InstanceRow ( props ) , InstanceRow ( props ) ] ;
198+ const insts = useRecoilValue ( instanceIds ) ;
199+ const rows = insts . map ( ( instance , index ) => (
200+ < InstanceRow key = { index } id = { instance } />
201+ ) ) ;
150202
151203 const data = {
152204 headers : [ `${ rows . length } Instances` , "" , "" ] ,
153- rows : rows ,
205+ rows : [ ] ,
154206 } ;
155207
156- return < Table data = { data } /> ;
208+ return < Table data = { data } rows = { rows } /> ;
157209} ;
0 commit comments