Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit 042ee29

Browse files
author
Paul Korzhyk
committed
Import dgraph-js-http and read todos from Dgraph
1 parent b9603a8 commit 042ee29

File tree

6 files changed

+65
-50
lines changed

6 files changed

+65
-50
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"dependencies": {
66
"classnames": "^2.2.6",
7+
"dgraph-js-http": "^0.2.0",
78
"history": "^4.7.2",
89
"react": "^16.8.4",
910
"react-dom": "^16.8.4",

src/TodoApp.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default class TodoApp extends React.Component {
6969
this.props.model.destroy(todo)
7070

7171
edit = todo =>
72-
this.setState({ editing: todo.id })
72+
this.setState({ editing: todo.uid })
7373

7474
save = (todoToSave, text) => {
7575
this.props.model.save(todoToSave, text)
@@ -99,12 +99,12 @@ export default class TodoApp extends React.Component {
9999

100100
const todoItems = shownTodos.map(todo => (
101101
<TodoItem
102-
key={todo.id}
102+
key={todo.uid}
103103
todo={todo}
104104
onToggle={() => this.toggle(todo)}
105105
onDestroy={() => this.destroy(todo)}
106106
onEdit={() => this.edit(todo)}
107-
editing={editing === todo.id}
107+
editing={editing === todo.uid}
108108
onSave={text => this.save(todo, text)}
109109
onCancel={this.cancel}
110110
/>

src/TodoModel.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
import Utils from './Utils'
22

3+
import * as dgraph from 'dgraph-js-http'
4+
35
export default class TodoModel {
4-
constructor(key) {
5-
this.key = key
6-
this.todos = Utils.store(key)
6+
constructor() {
7+
const clientStub = new dgraph.DgraphClientStub("http://localhost:8080")
8+
this.dgraph = new dgraph.DgraphClient(clientStub)
9+
10+
this.todos = []
11+
this.fetchAndInform()
12+
}
13+
14+
async fetchAndInform() {
15+
this.todos = await this.fetchTodos()
16+
this.inform()
17+
}
18+
19+
async fetchTodos() {
20+
const query = `{
21+
todos(func: has(is_todo))
22+
{
23+
uid
24+
title
25+
completed
26+
}
27+
}`
28+
const res = await this.dgraph.newTxn().query(query)
29+
return res.data.todos || []
730
}
831

932
onChanges = []
@@ -12,13 +35,12 @@ export default class TodoModel {
1235
this.onChanges.push(onChange)
1336

1437
inform = () => {
15-
Utils.store(this.key, this.todos)
1638
this.onChanges.forEach(cb => cb())
1739
}
1840

1941
addTodo = title => {
2042
this.todos = this.todos.concat({
21-
id: Utils.uuid(),
43+
uid: 123,
2244
title: title,
2345
completed: false,
2446
})

src/Utils.js

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,5 @@
11
export default {
2-
uuid: () => {
3-
var i, random;
4-
var uuid = '';
5-
6-
for (i = 0; i < 32; i++) {
7-
random = Math.random() * 16 | 0;
8-
if (i === 8 || i === 12 || i === 16 || i === 20) {
9-
uuid += '-';
10-
}
11-
uuid += (i === 12 ? 4 : (i === 16 ? (random & 3 | 8) : random))
12-
.toString(16);
13-
}
14-
15-
return uuid;
16-
},
17-
182
pluralize: (count, word) => {
193
return count === 1 ? word : word + 's';
204
},
21-
22-
store: (namespace, data) => {
23-
if (data) {
24-
return localStorage.setItem(namespace, JSON.stringify(data));
25-
}
26-
27-
var store = localStorage.getItem(namespace);
28-
return (store && JSON.parse(store)) || [];
29-
},
30-
31-
extend: function() {
32-
var newObj = {};
33-
for (var i = 0; i < arguments.length; i++) {
34-
var obj = arguments[i];
35-
for (var key in obj) {
36-
if (obj.hasOwnProperty(key)) {
37-
newObj[key] = obj[key];
38-
}
39-
}
40-
}
41-
return newObj;
42-
},
435
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'todomvc-app-css/index.css'
55
import TodoApp from './TodoApp'
66
import TodoModel from './TodoModel'
77

8-
const model = new TodoModel('react-todos')
8+
const model = new TodoModel()
99

1010
function render() {
1111
ReactDOM.render(

yarn.lock

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,6 +3078,13 @@ detect-port-alt@1.1.6:
30783078
address "^1.0.1"
30793079
debug "^2.6.0"
30803080

3081+
dgraph-js-http@^0.2.0:
3082+
version "0.2.0"
3083+
resolved "https://registry.yarnpkg.com/dgraph-js-http/-/dgraph-js-http-0.2.0.tgz#e424f7286698c7785017039894a89c1546281263"
3084+
integrity sha512-94BUbAE9S263pwlEeINuTAbMsptdqpQt/3X58Kws/NgGwXvkkWh0YohCb8tXtB4TlrWzp1KSCUFPnOwIRRmbEA==
3085+
dependencies:
3086+
isomorphic-fetch "^2.2.1"
3087+
30813088
diff@^3.2.0:
30823089
version "3.5.0"
30833090
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
@@ -3273,6 +3280,13 @@ encodeurl@~1.0.2:
32733280
resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59"
32743281
integrity sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=
32753282

3283+
encoding@^0.1.11:
3284+
version "0.1.12"
3285+
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.12.tgz#538b66f3ee62cd1ab51ec323829d1f9480c74beb"
3286+
integrity sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=
3287+
dependencies:
3288+
iconv-lite "~0.4.13"
3289+
32763290
end-of-stream@^1.0.0, end-of-stream@^1.1.0:
32773291
version "1.4.1"
32783292
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.1.tgz#ed29634d19baba463b6ce6b80a37213eab71ec43"
@@ -4594,7 +4608,7 @@ iconv-lite@0.4.23:
45944608
dependencies:
45954609
safer-buffer ">= 2.1.2 < 3"
45964610

4597-
iconv-lite@0.4.24, iconv-lite@^0.4.24, iconv-lite@^0.4.4:
4611+
iconv-lite@0.4.24, iconv-lite@^0.4.24, iconv-lite@^0.4.4, iconv-lite@~0.4.13:
45984612
version "0.4.24"
45994613
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
46004614
integrity sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==
@@ -5102,7 +5116,7 @@ is-root@2.0.0:
51025116
resolved "https://registry.yarnpkg.com/is-root/-/is-root-2.0.0.tgz#838d1e82318144e5a6f77819d90207645acc7019"
51035117
integrity sha512-F/pJIk8QD6OX5DNhRB7hWamLsUilmkDGho48KbgZ6xg/lmAZXHxzXQ91jzB3yRSw5kdQGGGc4yz8HYhTYIMWPg==
51045118

5105-
is-stream@^1.1.0:
5119+
is-stream@^1.0.1, is-stream@^1.1.0:
51065120
version "1.1.0"
51075121
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"
51085122
integrity sha1-EtSj3U5o4Lec6428hBc66A2RykQ=
@@ -5170,6 +5184,14 @@ isobject@^3.0.0, isobject@^3.0.1:
51705184
resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df"
51715185
integrity sha1-TkMekrEalzFjaqH5yNHMvP2reN8=
51725186

5187+
isomorphic-fetch@^2.2.1:
5188+
version "2.2.1"
5189+
resolved "https://registry.yarnpkg.com/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz#611ae1acf14f5e81f729507472819fe9733558a9"
5190+
integrity sha1-YRrhrPFPXoH3KVB0coGf6XM1WKk=
5191+
dependencies:
5192+
node-fetch "^1.0.1"
5193+
whatwg-fetch ">=0.10.0"
5194+
51735195
isstream@~0.1.2:
51745196
version "0.1.2"
51755197
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
@@ -6374,6 +6396,14 @@ no-case@^2.2.0:
63746396
dependencies:
63756397
lower-case "^1.1.1"
63766398

6399+
node-fetch@^1.0.1:
6400+
version "1.7.3"
6401+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef"
6402+
integrity sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==
6403+
dependencies:
6404+
encoding "^0.1.11"
6405+
is-stream "^1.0.1"
6406+
63776407
node-forge@0.7.5:
63786408
version "0.7.5"
63796409
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.7.5.tgz#6c152c345ce11c52f465c2abd957e8639cd674df"
@@ -9873,7 +9903,7 @@ whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.3:
98739903
dependencies:
98749904
iconv-lite "0.4.24"
98759905

9876-
whatwg-fetch@3.0.0:
9906+
whatwg-fetch@3.0.0, whatwg-fetch@>=0.10.0:
98779907
version "3.0.0"
98789908
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz#fc804e458cc460009b1a2b966bc8817d2578aefb"
98799909
integrity sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q==

0 commit comments

Comments
 (0)