@@ -9,27 +9,28 @@ import 'codemirror/theme/material-darker.css'
99//import 'codemirror/addon/display/autorefresh.js'
1010import { PROGRESS_BAR_COMPONENT , TERMINUS_SUCCESS } from "./constants"
1111import { Loading } from "./Loading"
12+ import { Alert } from "react-bootstrap"
1213import { MODEL_BUILDER_EDITOR_OPTIONS } from "./constants"
1314import { BiUndo } from "react-icons/bi"
1415import { FaRegEdit } from 'react-icons/fa'
15- import { Alerts } from "./Alerts"
16+ // import {Alerts} from "./Alerts"
1617import { TERMINUS_DANGER , DOCUMENT_PREFIX } from "./constants"
1718import { GRAPH_TAB } from "../pages/constants"
1819import { GraphContextObj } from "@terminusdb-live/tdb-react-components"
1920import { CopyButton } from "./utils"
2021import Card from "react-bootstrap/Card"
2122import { BsSave } from "react-icons/bs"
2223import Stack from "react-bootstrap/Stack"
24+ import { modelCallServerHook } from "@terminusdb-live/tdb-react-components"
2325
24- export const JSONModelBuilder = ( { tab, saveGraph, accessControlEditMode, setReportMessage} ) => {
26+ // we moved the save data at this level or we lost our change if there is an error
27+ export const JSONModelBuilder = ( { tab, accessControlEditMode} ) => {
2528
26- const { getSchemaGraph, mainGraphObj} = GraphContextObj ( ) ;
27- const { dataProduct} = WOQLClientObj ( )
28- const [ loading , setLoading ] = useState ( false )
29- const [ error , setError ] = useState ( false )
29+ const { getSchemaGraph, mainGraphObj, updateMainGraphData} = GraphContextObj ( ) ;
30+ const { dataProduct, woqlClient} = WOQLClientObj ( )
31+ // const [loading, setLoading] = useState(false)
3032 const [ commitMsg , setCommitMessage ] = useState ( "Add new schema" )
31- const [ report , setReport ] = useState ( false )
32-
33+
3334 const [ jsonSchema , setJsonSchema ] = useState ( false )
3435
3536 const [ editMode , setEditMode ] = useState ( false )
@@ -38,83 +39,81 @@ export const JSONModelBuilder = ({tab,saveGraph,accessControlEditMode, setReport
3839 let branch = "main"
3940 let ref = ""
4041
42+
43+ const { saveGraphChanges,
44+ reportMessage,
45+ setReport,
46+ callServerLoading :loading ,
47+ } = modelCallServerHook ( woqlClient , branch , ref , dataProduct )
48+
4149 const onBlurHandler = ( value ) => {
4250 setValue ( value )
4351 }
4452
45- async function getJSONSchema ( ) {
53+ async function loadSchema ( ) {
54+ setEditMode ( false ) ;
55+ setReport ( false )
4656 //get the schema from the mainGraphObj
4757 //the result is the schema in json format
48- const resultJson = getSchemaGraph ( ) // await woqlClient.getDocument(params, dataProduct)
58+ const resultJson = await getSchemaGraph ( ) // await woqlClient.getDocument(params, dataProduct)
4959 setJsonSchema ( resultJson )
5060 setValue ( resultJson )
5161 }
5262
5363 useEffect ( ( ) => {
5464 if ( tab == GRAPH_TAB ) return
55- setEditMode ( false )
56- getJSONSchema ( )
65+ loadSchema ( )
5766 } , [ tab , dataProduct , mainGraphObj ] )
5867
5968
6069
61-
6270 MODEL_BUILDER_EDITOR_OPTIONS . readOnly = ! editMode
6371
6472 function handleCommitMessage ( e ) {
6573 setCommitMessage ( e . target . value )
6674 }
6775
6876
69- async function saveChange ( ) {
70- setLoading ( < Loading message = { "Updating schema" } type = { PROGRESS_BAR_COMPONENT } /> )
71-
77+ async function saveGraph ( ) {
7278 if ( value ) {
73- try {
74- //save change in the server
75- await saveGraph ( value , commitMsg )
76- setLoading ( false )
77- //setEditMode(false)
78-
79- } catch ( err ) {
80- let jsonError = JSON . parse ( JSON . stringify ( err ) )
81- setLoading ( false )
82- if ( jsonError . data && jsonError . data [ "api:message" ] ) {
83- setReport ( < Alerts message = { jsonError . data [ "api:message" ] } type = { TERMINUS_DANGER } /> )
84- }
85- else setReport ( < Alerts message = { err . toString ( ) } type = { TERMINUS_DANGER } /> )
86- }
79+ const result = await saveGraphChanges ( value , commitMsg )
80+ if ( result ) {
81+ const resultJson = JSON . stringify ( result , null , 4 )
82+ updateMainGraphData ( result )
83+ setJsonSchema ( resultJson )
84+ setValue ( resultJson )
85+ setEditMode ( false ) ;
86+ }
8787 }
8888 }
8989
90- function handleUndo ( ) {
91- // sets report messagen from modelCallServerHook to false
92- if ( setReportMessage ) setReportMessage ( false )
93- setEditMode ( false ) ;
94- getJSONSchema ( ) ;
95- }
96-
9790 const editStyle = editMode ? { className :"border rounded border-warning position-sticky" } : { }
9891 const editMessage = editMode ? "Save schema or you will lose your changes" : ""
9992
10093 //console.log("editMode", editMode)
10194 return < >
10295 < label className = "text-warning mt-4" > { editMessage } </ label >
96+ { reportMessage && ! loading &&
97+ < Alert className = "mt-3" variant = "danger" dismissible onClose = { ( ) => setReport ( false ) } >
98+ < Alert . Heading > { reportMessage . message . title } </ Alert . Heading >
99+
100+ < p > { reportMessage . message . text } </ p >
101+ </ Alert > }
103102 < Card className = { `border border-secondary mt-4` } { ...editStyle } >
104- { loading && loading }
103+ { loading && < Loading message = { "Updating schema" } type = { PROGRESS_BAR_COMPONENT } /> }
105104 < Card . Header >
106105 < Stack direction = "horizontal" className = "w-100 justify-content-end" >
107106 { editMode && < div className = "w-100" >
108107 < div role = "group" className = "btn-group w-100" >
109108 < div className = "col-md-10 pr-0 pl-0" >
110109 < input id = "schema_save_description" placeholder = { "Enter a description to tag update" } type = "text" className = "form-control" onBlur = { handleCommitMessage } />
111110 </ div >
112- < button type = "button" id = "schema_save_button" className = "btn btn-sm bg-light text-dark" onClick = { saveChange } >
111+ < button type = "button" id = "schema_save_button" className = "btn btn-sm bg-light text-dark" onClick = { saveGraph } >
113112 < BsSave className = "small" /> { "Save" }
114113 </ button >
115114 < button type = "button"
116115 title = "Undo changes"
117- className = "btn btn-sm bg-danger text-white mr-2" onClick = { ( ) => { handleUndo ( ) } } >
116+ className = "btn btn-sm bg-danger text-white mr-2" onClick = { ( ) => { loadSchema ( ) } } >
118117 < BiUndo className = "h5" /> { "Undo" }
119118 </ button >
120119 </ div >
@@ -133,7 +132,6 @@ export const JSONModelBuilder = ({tab,saveGraph,accessControlEditMode, setReport
133132 </ Stack >
134133 </ Card . Header >
135134 < div className = "h-100" >
136- { report && < span className = "w-100 m-4" > { report } </ span > }
137135 < CodeMirror
138136 onBlur = { ( editor , data ) => {
139137 const editorValue = editor . doc . getValue ( )
0 commit comments