-
Notifications
You must be signed in to change notification settings - Fork 31
/
index-2.js
183 lines (168 loc) · 4.76 KB
/
index-2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React from 'react';
import ReactDOM from 'react-dom';
import TerminusClient from "@terminusdb/terminusdb-client";
import './index.css';
class Content extends React.Component {
constructor(props) {
super(props);
this.state = {
status: "Connected",
title: "",
contentText: "",
};
this.handleContentChange = this.handleContentChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleContentChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
}
handleSubmit(event) {
if (this.state.title && this.state.contentText) {
const lastUpdate = new Date();
const entryObj = {"@type": "Entry",
"title": this.state.title,
"content": this.state.contentText,
"created_date": lastUpdate,
"last_update": lastUpdate,
};
this.props.handleEntry(entryObj).then(() => {
this.setState({
status: "Entry Added",
});}
)
} else {
alert('Missing one or more input.' );
}
event.preventDefault();
}
render() {
if (this.props.schema) {
return (
<div>
<h2> {this.state.status} </h2>
<form onSubmit={this.handleSubmit}>
<label>
Title:
<input name="title" type="text" value={this.state.title} onChange={this.handleContentChange} />
</label>
<br/>
<label>
Content:
<br/>
<textarea name="contentText" type="text" value={this.state.contentText} onChange={this.handleContentChange} />
</label>
<br/>
<input type="submit" value="Add New Entry"/>
</form>
</div>
)
} else {
return (<h2> Not connected </h2>)
}
}
}
class Page extends React.Component {
constructor(props) {
super(props);
this.state = {
endpoint: "",
user: "",
team: "",
apiKey: "",
client: null,
schema: null,
entries: [],
};
this.handleLoginChange = this.handleLoginChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
async makeConnection(){
try{
const serverUrl = this.state.endpoint.concat("/", this.state.team, "/");
const client = new TerminusClient.WOQLClient(
serverUrl,{
user:this.state.user,
organization:this.state.team,
token: this.state.apiKey
}
);
await client.connect()
const schema = await client.getSchema("blog_app", "main")
console.log("Schema");
console.log(schema);
const entries = await client.getDocument({"graph_type":"instance","as_list":true,"type":"Entry"})
console.log("Entries");
console.log(entries);
this.setState({
client: client,
schema: schema,
entries: entries,
});
}catch(err){
console.error(err.message)
}
}
async addAndUpdateEntry(entryObj){
try{
const client = this.state.client;
await client.addDocument(entryObj);
}catch(err){
console.error(err.message)
}
}
handleLoginChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
}
handleSubmit(event) {
if (this.state.endpoint && this.state.user && this.state.team && this.state.apiKey) {
this.makeConnection();
} else {
alert('Missing one or more input.' )
}
event.preventDefault();
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>
End Point:
<input name="endpoint" type="text" value={this.state.endpoint} onChange={this.handleLoginChange} />
</label>
<label>
User:
<input name="user" type="text" value={this.state.user} onChange={this.handleLoginChange} />
</label>
<label>
Team:
<input name="team" type="text" value={this.state.team} onChange={this.handleLoginChange} />
</label>
<label>
Api Key:
<input name="apiKey" type="text" value={this.state.apiKey} onChange={this.handleLoginChange} />
</label>
<input type="submit" value="Submit"/>
</form>
<hr />
< Content schema={this.state.schema}
entries={this.state.entries}
handleEntry={(entryObj) => this.addAndUpdateEntry(entryObj)}/>
</div>
);
}
}
// ========================================
ReactDOM.render(
<Page />,
document.getElementById('root')
);