forked from RCMast3r/data_acq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React, {useEffect, useState} from "react"; | ||
|
||
export function DropdownForm({fields, data, setData, index, recording, serverAddr}) { | ||
|
||
const [options, setOptions] = useState([]) | ||
const [showAdd, setShowAdd] = useState(false) | ||
const [addInput, setAddInput] = useState('') | ||
|
||
async function updateOptions() { | ||
// const fetchResponse = await fetch(serverAddr + '/read/' + fields[index].name, { | ||
// method: 'GET', | ||
// headers: { | ||
// 'Accept': 'application/json', | ||
// 'Content-Type': 'application/json' | ||
// } | ||
// }) | ||
// const json = await fetchResponse.json() | ||
const json = '["","True","False"]' | ||
setOptions(JSON.parse(json)) | ||
} | ||
|
||
useEffect(() => { | ||
updateOptions().then() | ||
}, []) | ||
|
||
useEffect(() => { | ||
updateOptions().then() | ||
}, [serverAddr]) | ||
|
||
async function addOption() { | ||
const fetchResponse = await fetch(serverAddr + '/add/' + fields[index].name, { | ||
method: 'POST', | ||
body: JSON.stringify(addInput), | ||
headers: { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
fetchResponse.then(setShowAdd(false)); | ||
} | ||
|
||
function handleChange(e) { | ||
const newData = [...data]; | ||
newData[index] = e.target.value; | ||
setData(newData); | ||
} | ||
|
||
function getNoAdd() { | ||
return ( | ||
<select value={data[index]} className={"select select-bordered w-80"} onChange={handleChange} | ||
disabled={recording}> | ||
{options.map((option) => <option value={option}>{option}</option>)} | ||
</select> | ||
) | ||
} | ||
|
||
function getAddForm() { | ||
return ( | ||
<div className={"flex flex-row items-center w-80 -mt-3"}> | ||
<input value={addInput} onChange={e => setAddInput(e.target.value)} className={"input input-bordered w-64"} disabled={recording}/> | ||
<div className={"grow w-max"}/> | ||
<button className={"btn"} onClick={() => addOption} disabled={addInput === ''}> | ||
Ok | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
function getAdd() { | ||
return ( | ||
<div className={"flex flex-row items-center w-80"}> | ||
<select value={data[index]} className={"select select-bordered w-64"} onChange={handleChange} disabled={recording}> | ||
{options.map((option) => <option value={option}>{option}</option>)} | ||
</select> | ||
<div className={"grow w-max"}/> | ||
<button className={"btn btn-square"} onClick={()=>setShowAdd(!showAdd)}> | ||
{showAdd ? "-" : "+"} | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
return ( | ||
<> | ||
{fields[index].type === "boolean" ? getNoAdd() : getAdd()} | ||
{showAdd ? getAddForm() : null} | ||
</> | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters