1+ import React , { useState , useEffect } from "react"
2+ import { FrameViewer , DiffViewer } from '@terminusdb/terminusdb-documents-ui'
3+ import { InitObj } from "./init"
4+ import { VIEW , CARD_OUTPUT_TITLE , CREATE } from "./constants"
5+ import Card from 'react-bootstrap/Card'
6+ import { SELECT_OPTIONS , ORIGINAL_LIST , CHANGED_LIST , ORIGINAL_TEST_LIST , CHANGED_TEST_LIST , ORIGINAL_DIFF_MANDATORY_DOCUMENT , CHANGED_DIFF_MANDATORY_DOCUMENT } from "./data.constants"
7+ import Stack from 'react-bootstrap/Stack'
8+ import { Button , Col } from "react-bootstrap"
9+ import { FiCode } from "react-icons/fi"
10+ import { DIFF_VIEWER , MULTI_LANGUAGE } from "./menu.constants"
11+ import Form from 'react-bootstrap/Form'
12+
13+ const FormView = ( ) => {
14+ const {
15+ type,
16+ uiFrames,
17+ data,
18+ mode,
19+ setData,
20+ frames,
21+ language
22+ } = InitObj ( )
23+
24+ if ( ! frames ) return "LOADING ..."
25+ if ( ! type ) return "LOADING ..."
26+ if ( ! mode ) return "LOADING ..."
27+ if ( mode !== CREATE && Object . keys ( data ) . length === 0 ) return "LOADING ..."
28+
29+ function handleSubmit ( data ) {
30+ setData ( data )
31+ }
32+
33+ function handleSelect ( inp ) {
34+ let options = SELECT_OPTIONS , matched = [ ]
35+ options . map ( item => {
36+ if ( item . label . toUpperCase ( ) . startsWith ( inp . toUpperCase ( ) ) ) {
37+ //if(item.label.toUpperCase() === inp.toUpperCase()){
38+ matched . push ( item )
39+ }
40+ } )
41+ return matched
42+ }
43+
44+ return < FrameViewer
45+ frame = { frames }
46+ uiFrame = { uiFrames }
47+ type = { type }
48+ formData = { data }
49+ onSelect = { handleSelect }
50+ mode = { mode }
51+ hideSubmit = { mode === VIEW ? true : false }
52+ //onTraverse={handleTraverse}
53+ //submitButton={submitJSON}
54+ language = { language }
55+ onSubmit = { handleSubmit }
56+ />
57+ }
58+
59+ export const Diff = ( ) => {
60+
61+ const {
62+ frames,
63+ type,
64+ tdbClient,
65+ diffPatch,
66+ setDiffPatch
67+ } = InitObj ( )
68+
69+ if ( ! frames ) return "LOADING ..."
70+ if ( ! type ) return "LOADING ..."
71+
72+ useEffect ( ( ) => {
73+ async function getDiffs ( tdbClient ) {
74+ let result_patch = await tdbClient . getJSONDiff ( ORIGINAL_TEST_LIST , CHANGED_TEST_LIST )
75+ setDiffPatch ( result_patch )
76+ console . log ( "result_patch" , result_patch )
77+ }
78+ if ( tdbClient ) {
79+ getDiffs ( tdbClient )
80+ }
81+ } , [ ] )
82+
83+ return < div className = "w-100" >
84+ { diffPatch && frames && < DiffViewer
85+ oldValue = { ORIGINAL_TEST_LIST }
86+ newValue = { CHANGED_TEST_LIST }
87+ frame = { frames }
88+ type = { "ComputerStudent" }
89+ diffPatch = { diffPatch } /> }
90+ </ div >
91+ }
92+
93+ const TestTextArea = ( ) => {
94+ const {
95+ data
96+ } = InitObj ( )
97+
98+ return < textarea data-cy = "data-reader"
99+ className = "opacity-0"
100+ value = { JSON . stringify ( data , null , 2 ) } >
101+ </ textarea >
102+ }
103+
104+
105+ export const Output = ( ) => {
106+ const {
107+ menuItem,
108+ type,
109+ setShowMoreInfo,
110+ setShowCode,
111+ setLanguage
112+ } = InitObj ( )
113+
114+ function handleMoreInfo ( e ) {
115+ setShowCode ( Date . now ( ) )
116+ setShowMoreInfo ( true )
117+ }
118+
119+ function handleLanguageChange ( event ) {
120+ event . preventDefault ( )
121+ event . stopPropagation ( )
122+ setLanguage ( event . target . value )
123+ }
124+
125+ let label = type
126+ if ( ! type ) label = menuItem
127+
128+ if ( menuItem === DIFF_VIEWER ) {
129+ return < Diff />
130+ }
131+
132+ return < React . Fragment >
133+ { menuItem === MULTI_LANGUAGE && < Form >
134+ < Form . Group as = { Col } md = "12" className = "tdb__input mb-3" >
135+ < Form . Control type = "text" placeholder = "Enter language code" onBlur = { handleLanguageChange } />
136+ </ Form . Group >
137+ </ Form > }
138+ < Card >
139+ < Card . Header className = "bg-light text-dark" >
140+ < Stack direction = "horizontal" gap = { 3 } >
141+ < div className = "bg-light" > { `${ CARD_OUTPUT_TITLE } - ${ label } ` } </ div >
142+ < div className = "" > </ div >
143+ < div className = "bg-light ms-auto" >
144+ < Button variant = "primary btn btn-sm" title = "View Code" onClick = { handleMoreInfo } >
145+ < FiCode />
146+ </ Button >
147+ </ div >
148+ </ Stack >
149+ </ Card . Header >
150+ < Card . Body >
151+ < FormView />
152+ < TestTextArea />
153+ </ Card . Body >
154+ </ Card >
155+ </ React . Fragment >
156+ }
0 commit comments