From ef2c6f879824cb12a0bea61dcc853257f03032d6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 18 Nov 2021 19:56:08 -0600 Subject: rough in edit controls --- src/web/src/components/System/Options.js | 13 ----- src/web/src/components/System/Options/Edit.js | 82 +++++++++++++++++++++++++++ src/web/src/components/System/Options/View.js | 11 ++++ src/web/src/components/System/System.js | 6 +- 4 files changed, 97 insertions(+), 15 deletions(-) delete mode 100644 src/web/src/components/System/Options.js create mode 100644 src/web/src/components/System/Options/Edit.js create mode 100644 src/web/src/components/System/Options/View.js (limited to 'src') diff --git a/src/web/src/components/System/Options.js b/src/web/src/components/System/Options.js deleted file mode 100644 index e822b11d..00000000 --- a/src/web/src/components/System/Options.js +++ /dev/null @@ -1,13 +0,0 @@ -import React, { Component } from 'react'; - -class Options extends Component { - render = () => { - return ( -
-
{JSON.stringify(this.props.options, null, 2)}
-
- ); - } -} - -export default Options; \ No newline at end of file diff --git a/src/web/src/components/System/Options/Edit.js b/src/web/src/components/System/Options/Edit.js new file mode 100644 index 00000000..78ad3c80 --- /dev/null +++ b/src/web/src/components/System/Options/Edit.js @@ -0,0 +1,82 @@ +import React, { useState, useEffect } from 'react'; + +import { Button, Icon, Message } from 'semantic-ui-react'; +import CodeEditor from '@uiw/react-textarea-code-editor'; + +import { getYaml, validateYaml, updateYaml } from '../../../lib/options'; +import PlaceholderSegment from '../../Shared/PlaceholderSegment'; + +const Edit = ({ options }) => { + const [{ loading, error }, setLoading] = useState({ loading: true, error: false }); + const [{ yaml, isDirty }, setYaml] = useState({ yaml: undefined, isDirty: false }); + const [yamlError, setYamlError] = useState(); + + useEffect(() => { + get(); + }, []) + + const get = async () => { + setLoading({ loading: true, error: false }); + + try { + const yaml = await getYaml(); + setYaml({ yaml: yaml, isDirty: false }) + setLoading({ loading: false, error: false }) + } catch (error) { + setLoading({ loading: false, error: error.message }) + } + } + + const update = async (yaml) => { + setYaml({ yaml, isDirty: true }); + validate(yaml); + } + + const validate = async (yaml) => { + const response = await validateYaml({ yaml }); + setYamlError(response); + } + + const save = async (yaml) => { + await validate(yaml); + + if (!yamlError) { + await updateYaml({ yaml }); + await get(); + } + } + + const cancel = () => get(); + + if (loading) { + return + } + + if (error) { + return + } + + return ( + <> +
+ + {isDirty && } +
+ {yamlError && {yamlError}} + update(e.target.value)} + padding={10} + style={{ + marginTop: 14, + border: '1px solid #d4d4d5', + backgroundColor: '#f5f5f5', + fontFamily: 'ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace', + }} + /> + + ); +} + +export default Edit; \ No newline at end of file diff --git a/src/web/src/components/System/Options/View.js b/src/web/src/components/System/Options/View.js new file mode 100644 index 00000000..ba7cf426 --- /dev/null +++ b/src/web/src/components/System/Options/View.js @@ -0,0 +1,11 @@ +import React from 'react'; + +const View = ({ options }) => { + return ( +
+
{JSON.stringify(options, null, 2)}
+
+ ); +} + +export default View; \ No newline at end of file diff --git a/src/web/src/components/System/System.js b/src/web/src/components/System/System.js index bbba292d..037e6ea3 100644 --- a/src/web/src/components/System/System.js +++ b/src/web/src/components/System/System.js @@ -3,7 +3,8 @@ import { Segment, Tab } from 'semantic-ui-react'; import './System.css'; import Info from './Info'; import Logs from './Logs'; -import Options from './Options'; +import OptionsView from './Options/View'; +import OptionsEdit from './Options/Edit'; class System extends Component { componentDidMount = () => { @@ -19,7 +20,8 @@ class System extends Component { }, - { menuItem: 'Options', render: () => }, + { menuItem: 'Options', render: () => }, + { menuItem: 'Edit', render: () => }, { menuItem: 'Logs', render: () => }, ]}/> -- cgit v1.2.3