more filtering updates

This commit is contained in:
Martin McKeaveney 2020-10-15 12:09:41 +01:00
parent 11927d2340
commit 3ba8f19e7f
9 changed files with 42 additions and 36 deletions

View File

@ -23,7 +23,6 @@
const params = new URLSearchParams() const params = new URLSearchParams()
if (calculation) { if (calculation) {
params.set("field", field) params.set("field", field)
// todo, maybe won't work
params.set("calculation", calculation) params.set("calculation", calculation)
} }
if (groupBy) { if (groupBy) {
@ -38,6 +37,8 @@
<Table title={decodeURI(name)} schema={view.schema} {data}> <Table title={decodeURI(name)} schema={view.schema} {data}>
<FilterButton {view} /> <FilterButton {view} />
<CalculateButton {view} /> <CalculateButton {view} />
<GroupByButton {view} /> {#if view.calculation}
<GroupByButton {view} />
{/if}
<ExportButton {view} /> <ExportButton {view} />
</Table> </Table>

View File

@ -28,9 +28,10 @@
$: fields = $: fields =
viewTable && viewTable &&
Object.keys(viewTable.schema).filter( Object.keys(viewTable.schema).filter(
field => viewTable.schema[field].type === "number" field =>
view.calculation === "count" ||
viewTable.schema[field].type === "number"
) )
$: valid = view.calculation === "count" || view.field
function saveView() { function saveView() {
if (!view.calculation) view.calculation = "stats" if (!view.calculation) view.calculation = "stats"
@ -51,21 +52,19 @@
<option value={calculation.key}>{calculation.name}</option> <option value={calculation.key}>{calculation.name}</option>
{/each} {/each}
</Select> </Select>
<p>of</p> {#if view.calculation}
<Select secondary thin bind:value={view.field}> <p>of</p>
<option value={''}> <Select secondary thin bind:value={view.field}>
{#if view.calculation === 'count'} <option value={''}>You must choose an option</option>
All Rows {#each fields as field}
{:else}You must choose an option{/if} <option value={field}>{field}</option>
</option> {/each}
{#each fields as field} </Select>
<option value={field}>{field}</option> {/if}
{/each}
</Select>
</div> </div>
<div class="footer"> <div class="footer">
<Button secondary on:click={onClosed}>Cancel</Button> <Button secondary on:click={onClosed}>Cancel</Button>
<Button primary on:click={saveView} disabled={!valid}>Save</Button> <Button primary on:click={saveView} disabled={!view.field}>Save</Button>
</div> </div>
</div> </div>

View File

@ -74,7 +74,7 @@
<Label extraSmall grey>Current Users</Label> <Label extraSmall grey>Current Users</Label>
{#await fetchUsersPromise} {#await fetchUsersPromise}
Loading... Loading...
{:then [object Object]} {:then users}
<ul> <ul>
{#each users as user} {#each users as user}
<li> <li>
@ -84,7 +84,7 @@
<li>No Users found</li> <li>No Users found</li>
{/each} {/each}
</ul> </ul>
{:catch [object Object]} {:catch err}
Something went wrong when trying to fetch users. Please refresh (CMD + R / Something went wrong when trying to fetch users. Please refresh (CMD + R /
CTRL + R) the page and try again. CTRL + R) the page and try again.
{/await} {/await}

View File

@ -24,7 +24,7 @@
<div class="spinner-container"> <div class="spinner-container">
<Spinner size="30" /> <Spinner size="30" />
</div> </div>
{:then [object Object]} {:then apps}
<div class="inner"> <div class="inner">
<div> <div>
<div> <div>
@ -36,7 +36,7 @@
</div> </div>
</div> </div>
</div> </div>
{:catch [object Object]} {:catch err}
<h1 style="color:red">{err}</h1> <h1 style="color:red">{err}</h1>
{/await} {/await}
</div> </div>

View File

@ -20,7 +20,7 @@
<div class="spinner-container"> <div class="spinner-container">
<Spinner size="30" /> <Spinner size="30" />
</div> </div>
{:then [object Object]} {:then templates}
<div class="templates"> <div class="templates">
{#each templates as template} {#each templates as template}
<div class="templates-card"> <div class="templates-card">
@ -39,7 +39,7 @@
</div> </div>
{/each} {/each}
</div> </div>
{:catch [object Object]} {:catch err}
<h1 style="color:red">{err}</h1> <h1 style="color:red">{err}</h1>
{/await} {/await}
</div> </div>

View File

@ -80,9 +80,9 @@
{#await promise} {#await promise}
<!-- This should probably be some kind of loading state? --> <!-- This should probably be some kind of loading state? -->
<div /> <div />
{:then [object Object]} {:then _}
<slot /> <slot />
{:catch [object Object]} {:catch error}
<p>Something went wrong: {error.message}</p> <p>Something went wrong: {error.message}</p>
{/await} {/await}
</div> </div>

View File

@ -11,6 +11,12 @@ const { cloneDeep } = require("lodash")
const TABLE_VIEW_BEGINS_WITH = `all${SEPARATOR}${DocumentTypes.TABLE}${SEPARATOR}` const TABLE_VIEW_BEGINS_WITH = `all${SEPARATOR}${DocumentTypes.TABLE}${SEPARATOR}`
const CALCULATION_TYPES = {
SUM: "sum",
COUNT: "count",
STATS: "stats"
}
validateJs.extend(validateJs.validators.datetime, { validateJs.extend(validateJs.validators.datetime, {
parse: function(value) { parse: function(value) {
return new Date(value).getTime() return new Date(value).getTime()
@ -152,13 +158,12 @@ exports.fetchView = async function(ctx) {
group, group,
}) })
// TODO: create constants for calculation types
if (!calculation) { if (!calculation) {
response.rows = response.rows.map(row => row.doc) response.rows = response.rows.map(row => row.doc)
ctx.body = await linkRows.attachLinkInfo(instanceId, response.rows) ctx.body = await linkRows.attachLinkInfo(instanceId, response.rows)
} }
if (calculation === "stats") { if (calculation === CALCULATION_TYPES.STATS) {
response.rows = response.rows.map(row => ({ response.rows = response.rows.map(row => ({
group: row.key, group: row.key,
field, field,
@ -168,13 +173,15 @@ exports.fetchView = async function(ctx) {
ctx.body = response.rows ctx.body = response.rows
} }
if (calculation === "count" || calculation === "sum") { if (
ctx.body = field calculation === CALCULATION_TYPES.COUNT ||
? response.rows.map(row => ({ calculation === CALCULATION_TYPES.SUM
field, ) {
value: row.value, ctx.body = response.rows.map(row => ({
})) group: row.key,
: [{ field: "All Rows", value: response.total_rows }] field,
value: row.value,
}))
} }
} }

View File

@ -89,8 +89,7 @@ function parseFilterExpression(filters) {
* @param {String?} groupBy - field to group calculation results on, if any * @param {String?} groupBy - field to group calculation results on, if any
*/ */
function parseEmitExpression(field, groupBy) { function parseEmitExpression(field, groupBy) {
if (field) return `emit(doc["${groupBy || "_id"}"], doc["${field}"]);` return `emit(doc["${groupBy || "_id"}"], doc["${field}"]);`
return `emit(doc._id, 1);`
} }
/** /**

View File

@ -35,7 +35,7 @@
{#await _appPromise} {#await _appPromise}
loading loading
{:then [object Object]} {:then _}
<div id="current_component" bind:this={currentComponent} /> <div id="current_component" bind:this={currentComponent} />
{/await} {/await}