Commit 9753714d647474529a117b3536bc687a81f8aa08
/query : allow initialQuery to be passed in!
mixmix committed on 7/17/2018, 7:53:33 AMParent: 9b9ae32751d87f8c75ee8da7d23f22f5b9250a74
Files changed
app/page/query.js | changed |
app/page/query.js | |||
---|---|---|---|
@@ -37,19 +37,19 @@ | |||
37 | 37 … | } catch (err) { | |
38 | 38 … | // console.error(err) | |
39 | 39 … | return err | |
40 | 40 … | } | |
41 | - if (!Array.isArray(query)) return | ||
42 | - if (!query.map(q => Object.keys(q)[0]).every(q => ['$filter', '$map', '$reduce'].includes(q))) return | ||
43 | - activateQuery() | ||
41 … | + if (isValidQuery(query)) activateQuery() | ||
44 | 42 … | }) | |
45 | - const query = Value(json5.parse(initial())) | ||
46 | 43 … | ||
44 … | + const { initialQuery, initialValue } = getInitialState(location) | ||
45 … | + const query = Value(initialQuery) | ||
46 … | + | ||
47 | 47 … | const activateQuery = () => query.set(json5.parse(input())) | |
48 | 48 … | ||
49 | 49 … | return h('Query', { title: '/query' }, [ | |
50 | 50 … | h('section.query', [ | |
51 | - h('textarea', { 'ev-input': ev => input.set(ev.target.value), value: initial() }), | ||
51 … | + h('textarea', { 'ev-input': ev => input.set(ev.target.value), value: initialValue }), | ||
52 | 52 … | h('button', { | |
53 | 53 … | className: when(error, '', '-primary'), | |
54 | 54 … | disabled: when(error, 'disabled'), | |
55 | 55 … | 'ev-click': activateQuery | |
@@ -82,10 +82,18 @@ | |||
82 | 82 … | }) | |
83 | 83 … | } | |
84 | 84 … | } | |
85 | 85 … | ||
86 | -function initial () { | ||
87 | - return `[{ | ||
86 … | +function getInitialState (location) { | ||
87 … | + const { initialQuery, initialValue } = location | ||
88 … | + if (isValidQuery(initialQuery)) { | ||
89 … | + return { | ||
90 … | + initialQuery, | ||
91 … | + initialValue: initialValue || json5.stringify(initialQuery, null, 2) | ||
92 … | + } | ||
93 … | + } | ||
94 … | + | ||
95 … | + const defaultValue = `[{ | ||
88 | 96 … | $filter: { | |
89 | 97 … | value: { | |
90 | 98 … | timestamp: {$gt: 0}, | |
91 | 99 … | content: { | |
@@ -107,5 +115,16 @@ | |||
107 | 115 … | // $filter - used to prune down results. This must be the first entry, as ssb-query uses it to determine the most optimal index for fast lookup. | |
108 | 116 … | ||
109 | 117 … | // $map - optional, can be used to pluck data you want out. Doing this reduces the amount of data sent over muxrpc, which speeds up loading | |
110 | 118 … | ` | |
119 … | + return { | ||
120 … | + initialQuery: json5.parse(defaultValue), | ||
121 … | + initialValue: defaultValue | ||
122 … | + } | ||
111 | 123 … | } | |
124 … | + | ||
125 … | +function isValidQuery (query) { | ||
126 … | + if (!Array.isArray(query)) return false | ||
127 … | + if (!query.map(q => Object.keys(q)[0]).every(q => ['$filter', '$map', '$reduce'].includes(q))) return false | ||
128 … | + | ||
129 … | + return true | ||
130 … | +} |
Built with git-ssb-web