-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (132 loc) · 3.96 KB
/
index.js
File metadata and controls
144 lines (132 loc) · 3.96 KB
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
const materialRoot = require("uiks/reaks-material/root")
const renderFullscreen = require("uiks/reaks/renderFullscreen")
const swap = require("uiks/reaks/swap")
const align = require("uiks/reaks/align")
const scroll = require("uiks/reaks/scroll")
const command = require("uiks/core/command")
const json5 = require("json5")
const formatJson = require("format-json").plain
const rdbClient = require("./rdbClient")
const ctxAssign = require("uiks/core/assign")
const assignObservable = require("uiks/core/assignObservable")
const assignCtx = require("uiks/core/assign")
const loginForm = require("./loginForm")
const materialColors = require("material-colors")
const colors = {
primary: materialColors.yellow[500],
textOnPrimary: "black",
fadedTextOnPrimary: materialColors.brown[500],
secondary: materialColors.grey[900],
textOnSecondary: materialColors.white,
darkText: "black",
fadedDarkText: materialColors.grey[600],
}
const vFlex = require("uiks/reaks/vFlex")
const hPile = require("uiks/reaks/hPile")
const backgroundColor = require("uiks/reaks/backgroundColor")
const value = require("uiks/core/value")
const formatValue = require("uiks/core/formatValue")
const dynamicQuery = require("uiks/reactivedb/dynamicQuery")
const displayIf = require("uiks/reaks/displayIf")
const loadingSwitch = require("uiks/reaks/loadingSwitch")
const button = require("uiks/reaks-material/button")
const commandButton = require("uiks/reaks-material/commandButton")
const label = require("uiks/reaks/label")
const size = require("uiks/reaks/size")
var ace = require("brace")
require("brace/mode/javascript")
const child = require("reaks/child")
const text = require("reaks/text")
const jsEditor = (ctx) => {
return (domNode) => {
var editor = ace.edit(domNode)
editor.getSession().setMode("ace/mode/javascript")
editor.setFontSize(16)
editor.setValue(ctx.queryEditorValue)
editor.on("change", () => {
ctx.queryEditorValue = editor.getValue()
localStorage.setItem("lastReactiveDbAdminQuery", ctx.queryEditorValue)
})
return () => editor.destroy()
}
}
const codeDisplayer = (ctx) =>
child(text(ctx.value), () => document.createElement("pre"))
const app = assignCtx(
{
queryEditorValue:
localStorage.getItem("lastReactiveDbAdminQuery") || "[{}]",
},
assignObservable(
{
validatedQuery: null,
},
vFlex([
["fixed", size({ h: 300 }, jsEditor)],
[
"fixed",
backgroundColor(
"#EEE",
hPile([
button("Exécuter la requête", (ctx) => () => {
ctx.validatedQuery(json5.parse(ctx.queryEditorValue))
}),
commandButton(
"Exécuter comme patch",
(ctx) => () => ctx.patch(json5.parse(ctx.queryEditorValue))
),
])
),
],
displayIf(
"validatedQuery",
value(
dynamicQuery((ctx) => ctx.validatedQuery()),
loadingSwitch({
loaded: scroll(formatValue(formatJson, codeDisplayer)),
loading: label("chargement ..."),
})
)
),
])
)
)
const login = (config) =>
command(
(ctx) => () =>
rdbClient({
config,
username: ctx.username(),
password: ctx.password(),
}),
ctxAssign(
(ctx) => {
// dev!!
// if (process.env.NODE_ENV === "dev") {
// ctx.username("")
// ctx.password("")
// ctx.command.trigger()
// }
},
swap((ctx) => () => {
if (ctx.command.status() === "success") {
return ctxAssign(ctx.command.result, app)
} else {
return align({ v: "center", h: "center" }, loginForm)
}
})
)
)
module.exports = (config) =>
renderFullscreen(
materialRoot(
{ getFileUrl: () => (filename) => "assets/" + filename },
assignObservable(
{
username: "admin",
password: "",
},
login(config)
)
)
)({ colors })