tidy up and lint

This commit is contained in:
Martin McKeaveney 2020-08-18 17:14:26 +01:00
parent 33ec0cfa75
commit e57dda576b
13 changed files with 53 additions and 52 deletions

View File

@ -83,8 +83,10 @@ export const getBackendUiStore = () => {
delete: async model => {
await api.delete(`/api/models/${model._id}/${model._rev}`)
store.update(state => {
state.models = state.models.filter(existing => existing._id !== model._id)
state.selectedModel = state.models[0] || {}
state.models = state.models.filter(
existing => existing._id !== model._id
)
state.selectedModel = state.models[0] || {}
return state
})
},
@ -105,7 +107,7 @@ export const getBackendUiStore = () => {
store.actions.models.save(state.draftModel)
return state
})
}
},
},
views: {
select: view =>
@ -126,7 +128,9 @@ export const getBackendUiStore = () => {
await api.post(`/api/views`, view)
store.update(state => {
const viewModel = state.models.find(model => model._id === view.modelId)
const viewModel = state.models.find(
model => model._id === view.modelId
)
// TODO: Cleaner?
if (!viewModel.views) viewModel.views = {}
if (view.originalName) delete viewModel.views[view.originalName]
@ -136,7 +140,7 @@ export const getBackendUiStore = () => {
state.selectedView = view
return state
})
}
},
},
users: {
create: user =>

View File

@ -27,7 +27,10 @@
let search
$: {
if ($backendUiStore.selectedView && $backendUiStore.selectedView.name.startsWith("all_")) {
if (
$backendUiStore.selectedView &&
$backendUiStore.selectedView.name.startsWith("all_")
) {
api.fetchDataForView($backendUiStore.selectedView).then(records => {
data = records || []
})

View File

@ -30,37 +30,36 @@
},
{
name: "min",
key: "value.min"
key: "value.min",
},
{
name: "max",
key: "value.max"
key: "value.max",
},
{
name: "sumsqr",
key: "value.sumsqr"
key: "value.sumsqr",
},
{
name: "count",
key: "value.count"
key: "value.count",
},
{
name: "avg",
key: "value.avg"
}
key: "value.avg",
},
]
export let view = {}
let data = []
$: viewName = view.name
$: !viewName.startsWith("all_") && fetchViewData(viewName)
$: !view.name.startsWith("all_") && fetchViewData(view)
async function fetchViewData(viewName) {
let QUERY_VIEW_URL = `/api/views/${viewName}?stats=true`
if (view.groupBy) {
QUERY_VIEW_URL += `&group=${view.groupBy}`
async function fetchViewData({ name, groupBy }) {
let QUERY_VIEW_URL = `/api/views/${name}?stats=true`
if (groupBy) {
QUERY_VIEW_URL += `&group=${groupBy}`
}
const response = await api.get(QUERY_VIEW_URL)
@ -68,11 +67,7 @@
}
</script>
<Table
title={decodeURI(view.name)}
columns={COLUMNS}
{data}
>
<CalculationPopover {view} />
<GroupByPopover {view} />
</Table>
<Table title={decodeURI(view.name)} columns={COLUMNS} {data}>
<CalculationPopover {view} />
<GroupByPopover {view} />
</Table>

View File

@ -19,8 +19,7 @@
$: viewModel = $backendUiStore.models.find(
({ _id }) => _id === $backendUiStore.selectedView.modelId
)
$: fields =
viewModel && Object.keys(viewModel.schema)
$: fields = viewModel && Object.keys(viewModel.schema)
function saveView() {
backendUiStore.actions.views.save(view)
@ -40,7 +39,7 @@
<div class="input-group-row">
<p>Group By</p>
<Select secondary thin bind:value={view.groupBy}>
<option value={false}>None</option>
<option value={false}>Remove Group By</option>
{#each fields as field}
<option value={field}>{field}</option>
{/each}

View File

@ -14,12 +14,17 @@
$: fields = Object.keys($backendUiStore.selectedModel.schema).filter(key => {
return $backendUiStore.selectedModel.schema[key].type === "number"
})
$: views = $backendUiStore.models.flatMap(model => Object.keys(model.views))
function saveView() {
if (views.includes(name)) {
notifier.danger(`View exists with name ${name}.`)
return
}
backendUiStore.actions.views.save({
name,
modelId: $backendUiStore.selectedModel._id,
field
field,
})
notifier.success(`View ${name} created`)
dropdown.hide()

View File

@ -40,7 +40,7 @@
function save() {
backendUiStore.actions.views.save({
originalName,
...view
...view,
})
notifier.success("Renamed View Successfully.")
hideEditor()

View File

@ -31,4 +31,4 @@
width: 300px;
height: 100%;
}
</style>
</style>

View File

@ -21,6 +21,7 @@ exports.save = async function(ctx) {
const modelToSave = {
type: "model",
_id: newid(),
views: {},
...ctx.request.body,
}

View File

@ -83,14 +83,14 @@ exports.fetchView = async function(ctx) {
const { stats, group } = ctx.query
const response = await db.query(`database/${ctx.params.viewName}`, {
include_docs: !stats,
group
group,
})
if (stats) {
for (row of response.rows) {
for (let row of response.rows) {
row.value = {
...row.value,
avg: row.value.sum / row.value.count
avg: row.value.sum / row.value.count,
}
}
} else {
@ -98,7 +98,6 @@ exports.fetchView = async function(ctx) {
}
ctx.body = response.rows
}
exports.fetchModelRecords = async function(ctx) {

View File

@ -1,5 +1,5 @@
const CouchDB = require("../../../db")
const statsViewTemplate = require("./viewBuilder");
const statsViewTemplate = require("./viewBuilder")
const controller = {
fetch: async ctx => {
@ -29,7 +29,7 @@ const controller = {
const designDoc = await db.get("_design/database")
const view = statsViewTemplate(newView)
const view = statsViewTemplate(newView)
designDoc.views = {
...designDoc.views,
@ -43,12 +43,11 @@ const controller = {
await db.put(designDoc)
// add views to model document
const model = await db.get(ctx.request.body.modelId)
model.views = {
...(model.views ? model.views : {}),
[newView.name]: view.meta
[newView.name]: view.meta,
}
if (originalName) {

View File

@ -1,8 +1,4 @@
function statsViewTemplate({
field,
modelId,
groupBy
}) {
function statsViewTemplate({ field, modelId, groupBy }) {
return {
meta: {
field,
@ -14,16 +10,16 @@ function statsViewTemplate({
max: "number",
count: "number",
sumsqr: "number",
avg: "number"
}
avg: "number",
},
},
map: `function (doc) {
if (doc.modelId === "${modelId}") {
emit(doc["${groupBy || "_id"}"], doc["${field}"]);
}
}`,
reduce: "_stats"
reduce: "_stats",
}
}
module.exports = statsViewTemplate
module.exports = statsViewTemplate

View File

@ -45,7 +45,7 @@ const server = http.createServer(app.callback())
server.on("close", () => console.log("Server Closed"))
process.on('SIGINT', () => process.exit(1));
process.on("SIGINT", () => process.exit(1))
module.exports = server.listen(env.PORT || 4001, () => {
console.log(`Budibase running on ${JSON.stringify(server.address())}`)

View File

@ -36,8 +36,8 @@ async function startApp() {
async function createWindow() {
app.server = require("./app")
win = new BrowserWindow({
width: 1920,
win = new BrowserWindow({
width: 1920,
height: 1080,
})
win.setTitle(APP_TITLE)