users as table working end to end

This commit is contained in:
Martin McKeaveney 2020-11-24 17:00:15 +00:00
parent a92d54f9a7
commit 2f996d4e68
5 changed files with 23 additions and 12 deletions

View File

@ -7,8 +7,8 @@ export async function createUser(user) {
} }
export async function saveRow(row, tableId) { export async function saveRow(row, tableId) {
const SAVE_ROWS_URL = `/api/${tableId}/rows` const SAVE_ROW_URL = `/api/${tableId}/rows`
const response = await api.post(SAVE_ROWS_URL, row) const response = await api.post(SAVE_ROW_URL, row)
return await response.json() return await response.json()
} }

View File

@ -32,11 +32,6 @@
) )
$: required = !!field?.constraints?.presence || primaryDisplay $: required = !!field?.constraints?.presence || primaryDisplay
$: uneditable = $backendUiStore.selectedTable?._id === TableNames.USERS && UNEDITABLE_USER_FIELDS.includes(field.name) $: uneditable = $backendUiStore.selectedTable?._id === TableNames.USERS && UNEDITABLE_USER_FIELDS.includes(field.name)
$: {
console.log($backendUiStore.selectedTable)
console.log(field.name)
console.log(uneditable)
}
async function saveColumn() { async function saveColumn() {
backendUiStore.update(state => { backendUiStore.update(state => {

View File

@ -1,5 +1,6 @@
<script> <script>
import { backendUiStore } from "builderStore" import { backendUiStore } from "builderStore"
import { TableNames } from "constants"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import RowFieldControl from "../RowFieldControl.svelte" import RowFieldControl from "../RowFieldControl.svelte"
import * as api from "../api" import * as api from "../api"
@ -21,9 +22,10 @@
{ ...row, tableId: table._id }, { ...row, tableId: table._id },
table._id table._id
) )
if (rowResponse.errors) { if (rowResponse.errors) {
errors = Object.keys(rowResponse.errors) errors = Object.entries(rowResponse.errors)
.map(k => ({ dataPath: k, message: rowResponse.errors[k] })) .map(([key, error]) => ({ dataPath: key, message: error }))
.flat() .flat()
// Prevent modal closing if there were errors // Prevent modal closing if there were errors
return false return false
@ -38,6 +40,9 @@
confirmText={creating ? 'Create Row' : 'Save Row'} confirmText={creating ? 'Create Row' : 'Save Row'}
onConfirm={saveRow}> onConfirm={saveRow}>
<ErrorsBox {errors} /> <ErrorsBox {errors} />
{#if creating && table._id === TableNames.USERS}
<RowFieldControl meta={{ name: "password", type: "password" }} bind:value={row.password} />
{/if}
{#each tableSchema as [key, meta]} {#each tableSchema as [key, meta]}
<div> <div>
<RowFieldControl {meta} bind:value={row[key]} /> <RowFieldControl {meta} bind:value={row[key]} />

View File

@ -4,7 +4,6 @@
import { store, backendUiStore } from "builderStore" import { store, backendUiStore } from "builderStore"
import api from "builderStore/api" import api from "builderStore/api"
// import * as api from "../api"
let username = "" let username = ""
let password = "" let password = ""

View File

@ -6,7 +6,9 @@ const {
generateRowID, generateRowID,
DocumentTypes, DocumentTypes,
SEPARATOR, SEPARATOR,
ViewNames,
} = require("../../db/utils") } = require("../../db/utils")
const usersController = require("./user")
const { cloneDeep } = require("lodash") const { cloneDeep } = require("lodash")
const TABLE_VIEW_BEGINS_WITH = `all${SEPARATOR}${DocumentTypes.TABLE}${SEPARATOR}` const TABLE_VIEW_BEGINS_WITH = `all${SEPARATOR}${DocumentTypes.TABLE}${SEPARATOR}`
@ -118,6 +120,16 @@ exports.save = async function(ctx) {
table, table,
}) })
// Creation of a new user goes to the user controller
if (!existingRow && row.tableId === ViewNames.USERS) {
try {
await usersController.create(ctx)
} catch (err) {
ctx.body = { errors: [err.message] }
}
return
}
if (existingRow) { if (existingRow) {
const response = await db.put(row) const response = await db.put(row)
row._rev = response.rev row._rev = response.rev
@ -315,8 +327,8 @@ exports.fetchEnrichedRow = async function(ctx) {
ctx.status = 200 ctx.status = 200
} }
function coerceRowValues(rec, table) { function coerceRowValues(record, table) {
const row = cloneDeep(rec) const row = cloneDeep(record)
for (let [key, value] of Object.entries(row)) { for (let [key, value] of Object.entries(row)) {
const field = table.schema[key] const field = table.schema[key]
if (!field) continue if (!field) continue