From 5ec88738d01e3ad75d8563c5f3502fba91e674fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E4=BA=8C=E7=8C=9B?= Date: Sun, 8 Dec 2019 13:23:48 +0800 Subject: [PATCH] A web update --- spug_web/src/pages/config/setting/JSONView.js | 60 +++++++++++++++---- spug_web/src/pages/config/setting/TextView.js | 44 +++++++++++++- spug_web/src/pages/config/setting/index.js | 17 ++++-- spug_web/src/pages/config/setting/store.js | 2 +- 4 files changed, 102 insertions(+), 21 deletions(-) diff --git a/spug_web/src/pages/config/setting/JSONView.js b/spug_web/src/pages/config/setting/JSONView.js index 49dea8f..63f9674 100644 --- a/spug_web/src/pages/config/setting/JSONView.js +++ b/spug_web/src/pages/config/setting/JSONView.js @@ -1,8 +1,10 @@ import React from 'react'; import { observer } from 'mobx-react'; +import { Button, message} from 'antd'; import Editor from 'react-ace'; import 'ace-builds/src-noconflict/mode-json'; import 'ace-builds/src-noconflict/theme-tomorrow'; +import { http } from 'libs'; import store from './store'; @observer @@ -10,31 +12,63 @@ class JSONView extends React.Component { constructor(props) { super(props); this.state = { + loading: false, + readOnly: true, body: '' } } componentDidMount() { + this.updateValue() + } + + updateValue = () => { const body = {}; for (let item of store.records) { body[item.key] = item.value } - this.setState({body: JSON.stringify(body, null, 2)}) - } + this.setState({readOnly: true, body: JSON.stringify(body, null, 2)}) + }; + + handleSubmit = () => { + try { + const data = JSON.parse(this.state.body); + this.setState({loading: true}); + const formData = {type: store.type, o_id: store.id, env_id: store.env.id, data}; + http.post('/api/config/parse/json/', formData) + .then(res => { + message.success('保存成功') + }) + .finally(() => this.setState({loading: false})) + } catch (err) { + message.error('解析JSON失败,请检查输入内容') + } + }; render() { + const {body, readOnly, loading} = this.state; return ( - this.setState({body: v})} - /> +
+ this.setState({body: v})}/> + {readOnly && } + {readOnly || } +
) } } diff --git a/spug_web/src/pages/config/setting/TextView.js b/spug_web/src/pages/config/setting/TextView.js index 84a66a4..8de2242 100644 --- a/spug_web/src/pages/config/setting/TextView.js +++ b/spug_web/src/pages/config/setting/TextView.js @@ -4,27 +4,65 @@ import Editor from 'react-ace'; import 'ace-builds/src-noconflict/mode-space'; import 'ace-builds/src-noconflict/theme-tomorrow'; import store from './store'; +import { http } from "libs"; +import { Button, message } from "antd"; @observer class TextView extends React.Component { constructor(props) { super(props); this.state = { + loading: false, + readOnly: true, body: '' } } componentDidMount() { + this.updateValue() + } + + updateValue = () => { let body = ''; for (let item of store.records) { body += `${item.key} = ${item.value}\n` } - this.setState({body}) - } + this.setState({readOnly: true, body}) + }; + + handleSubmit = () => { + this.setState({loading: true}); + const formData = {type: store.type, o_id: store.id, env_id: store.env.id, data: this.state.body}; + http.post('/api/config/parse/text/', formData) + .then(res => { + message.success('保存成功') + }) + .finally(() => this.setState({loading: false})) + }; render() { + const {body, loading, readOnly} = this.state; return ( - +
+ this.setState({body: v})}/> + {readOnly && } + {readOnly || } +
) } } diff --git a/spug_web/src/pages/config/setting/index.js b/spug_web/src/pages/config/setting/index.js index 71400a4..5da3a50 100644 --- a/spug_web/src/pages/config/setting/index.js +++ b/spug_web/src/pages/config/setting/index.js @@ -14,6 +14,8 @@ import store from './store'; class Index extends React.Component { constructor(props) { super(props); + this.textView = null; + this.JSONView = null; this.state = { view: '1' } @@ -32,7 +34,14 @@ class Index extends React.Component { updateEnv = (env) => { store.env = env || envStore.records[0]; - store.fetchRecords() + this.handleRefresh() + }; + + handleRefresh = () => { + store.fetchRecords().then(() => { + if (this.textView) this.textView.updateValue(); + if (this.JSONView) this.JSONView.updateValue(); + }) }; render() { @@ -63,7 +72,7 @@ class Index extends React.Component { store.f_name = e.target.value} placeholder="请输入"/> - +