Update usages of utils and helpers in the builder

This commit is contained in:
Andrew Kingston 2022-01-20 11:19:37 +00:00
parent e3b8e44eeb
commit 3a3f4f0538
19 changed files with 120 additions and 116 deletions

View File

@ -67,6 +67,7 @@
"dependencies": { "dependencies": {
"@budibase/bbui": "^1.0.44-alpha.7", "@budibase/bbui": "^1.0.44-alpha.7",
"@budibase/client": "^1.0.44-alpha.7", "@budibase/client": "^1.0.44-alpha.7",
"@budibase/frontend-core": "^1.0.44-alpha.7",
"@budibase/colorpicker": "1.1.2", "@budibase/colorpicker": "1.1.2",
"@budibase/string-templates": "^1.0.44-alpha.7", "@budibase/string-templates": "^1.0.44-alpha.7",
"@sentry/browser": "5.19.1", "@sentry/browser": "5.19.1",

View File

@ -1,6 +1,6 @@
import { store } from "./index" import { store } from "./index"
import { get as svelteGet } from "svelte/store" import { get as svelteGet } from "svelte/store"
import { removeCookie, Cookies } from "./cookies" import { CookieUtils, Constants } from "@budibase/frontend-core"
const apiCall = const apiCall =
method => method =>
@ -16,7 +16,7 @@ const apiCall =
if (url.includes("/api/templates")) { if (url.includes("/api/templates")) {
return { json: () => [] } return { json: () => [] }
} }
removeCookie(Cookies.Auth) CookieUtils.removeCookie(Constants.Cookies.Auth)
// reload after removing cookie, go to login // reload after removing cookie, go to login
if (!url.includes("self") && !url.includes("login")) { if (!url.includes("self") && !url.includes("login")) {
location.reload() location.reload()

View File

@ -15,10 +15,7 @@ import {
encodeJSBinding, encodeJSBinding,
} from "@budibase/string-templates" } from "@budibase/string-templates"
import { TableNames } from "../constants" import { TableNames } from "../constants"
import { import { JSONUtils } from "@budibase/frontend-core"
convertJSONSchemaToTableSchema,
getJSONArrayDatasourceSchema,
} from "./jsonUtils"
import ActionDefinitions from "components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/manifest.json" import ActionDefinitions from "components/design/PropertiesPanel/PropertyControls/ButtonActionEditor/manifest.json"
// Regex to match all instances of template strings // Regex to match all instances of template strings
@ -439,7 +436,7 @@ export const getSchemaForDatasource = (asset, datasource, isForm = false) => {
else if (type === "jsonarray") { else if (type === "jsonarray") {
table = tables.find(table => table._id === datasource.tableId) table = tables.find(table => table._id === datasource.tableId)
let tableSchema = table?.schema let tableSchema = table?.schema
schema = getJSONArrayDatasourceSchema(tableSchema, datasource) schema = JSONUtils.getJSONArrayDatasourceSchema(tableSchema, datasource)
} }
// Otherwise we assume we're targeting an internal table or a plus // Otherwise we assume we're targeting an internal table or a plus
@ -471,9 +468,12 @@ export const getSchemaForDatasource = (asset, datasource, isForm = false) => {
Object.keys(schema).forEach(fieldKey => { Object.keys(schema).forEach(fieldKey => {
const fieldSchema = schema[fieldKey] const fieldSchema = schema[fieldKey]
if (fieldSchema?.type === "json") { if (fieldSchema?.type === "json") {
const jsonSchema = convertJSONSchemaToTableSchema(fieldSchema, { const jsonSchema = JSONUtils.convertJSONSchemaToTableSchema(
fieldSchema,
{
squashObjects: true, squashObjects: true,
}) }
)
Object.keys(jsonSchema).forEach(jsonKey => { Object.keys(jsonSchema).forEach(jsonKey => {
jsonAdditions[`${fieldKey}.${jsonKey}`] = { jsonAdditions[`${fieldKey}.${jsonKey}`] = {
type: jsonSchema[jsonKey].type, type: jsonSchema[jsonKey].type,

View File

@ -27,7 +27,7 @@ import {
findComponent, findComponent,
getComponentSettings, getComponentSettings,
} from "../componentUtils" } from "../componentUtils"
import { uuid } from "../uuid" import { Helpers } from "@budibase/bbui"
import { removeBindings } from "../dataBinding" import { removeBindings } from "../dataBinding"
const INITIAL_FRONTEND_STATE = { const INITIAL_FRONTEND_STATE = {
@ -397,7 +397,7 @@ export const getFrontendStore = () => {
} }
return { return {
_id: uuid(), _id: Helpers.uuid(),
_component: definition.component, _component: definition.component,
_styles: { normal: {}, hover: {}, active: {} }, _styles: { normal: {}, hover: {}, active: {} },
_instanceName: `New ${definition.name}`, _instanceName: `New ${definition.name}`,
@ -551,7 +551,7 @@ export const getFrontendStore = () => {
if (!component) { if (!component) {
return return
} }
component._id = uuid() component._id = Helpers.uuid()
component._children?.forEach(randomizeIds) component._children?.forEach(randomizeIds)
} }
randomizeIds(componentToPaste) randomizeIds(componentToPaste)
@ -664,7 +664,7 @@ export const getFrontendStore = () => {
newLink = cloneDeep(nav._children[0]) newLink = cloneDeep(nav._children[0])
// Set our new props // Set our new props
newLink._id = uuid() newLink._id = Helpers.uuid()
newLink._instanceName = `${title} Link` newLink._instanceName = `${title} Link`
newLink.url = url newLink.url = url
newLink.text = title newLink.text = title

View File

@ -1,4 +1,4 @@
import { uuid } from "builderStore/uuid" import { Helpers } from "@budibase/bbui"
import { BaseStructure } from "./BaseStructure" import { BaseStructure } from "./BaseStructure"
export class Component extends BaseStructure { export class Component extends BaseStructure {
@ -6,7 +6,7 @@ export class Component extends BaseStructure {
super(false) super(false)
this._children = [] this._children = []
this._json = { this._json = {
_id: uuid(), _id: Helpers.uuid(),
_component: name, _component: name,
_styles: { _styles: {
normal: {}, normal: {},

View File

@ -1,5 +1,5 @@
import { BaseStructure } from "./BaseStructure" import { BaseStructure } from "./BaseStructure"
import { uuid } from "builderStore/uuid" import { Helpers } from "@budibase/bbui"
export class Screen extends BaseStructure { export class Screen extends BaseStructure {
constructor() { constructor() {
@ -7,7 +7,7 @@ export class Screen extends BaseStructure {
this._json = { this._json = {
layoutId: "layout_private_master", layoutId: "layout_private_master",
props: { props: {
_id: uuid(), _id: Helpers.uuid(),
_component: "@budibase/standard-components/container", _component: "@budibase/standard-components/container",
_styles: { _styles: {
normal: {}, normal: {},

View File

@ -1,4 +1,4 @@
import { localStorageStore } from "./localStorage" import { createLocalStorageStore } from "@budibase/frontend-core"
export const getThemeStore = () => { export const getThemeStore = () => {
const themeElement = document.documentElement const themeElement = document.documentElement
@ -6,7 +6,7 @@ export const getThemeStore = () => {
theme: "darkest", theme: "darkest",
options: ["lightest", "light", "dark", "darkest"], options: ["lightest", "light", "dark", "darkest"],
} }
const store = localStorageStore("bb-theme", initialValue) const store = createLocalStorageStore("bb-theme", initialValue)
// Update theme class when store changes // Update theme class when store changes
store.subscribe(state => { store.subscribe(state => {

View File

@ -28,7 +28,7 @@
import ModalBindableInput from "components/common/bindings/ModalBindableInput.svelte" import ModalBindableInput from "components/common/bindings/ModalBindableInput.svelte"
import FilterDrawer from "components/design/PropertiesPanel/PropertyControls/FilterEditor/FilterDrawer.svelte" import FilterDrawer from "components/design/PropertiesPanel/PropertyControls/FilterEditor/FilterDrawer.svelte"
// need the client lucene builder to convert to the structure API expects // need the client lucene builder to convert to the structure API expects
import { buildLuceneQuery } from "helpers/lucene" import { LuceneUtils } from "@budibase/frontend-core"
export let block export let block
export let testData export let testData
@ -131,7 +131,7 @@
} }
function saveFilters(key) { function saveFilters(key) {
const filters = buildLuceneQuery(tempFilters) const filters = LuceneUtils.buildLuceneQuery(tempFilters)
const defKey = `${key}-def` const defKey = `${key}-def`
inputData[key] = filters inputData[key] = filters
inputData[defKey] = tempFilters inputData[defKey] = tempFilters

View File

@ -2,7 +2,7 @@
import { Table, Modal, Layout, ActionButton } from "@budibase/bbui" import { Table, Modal, Layout, ActionButton } from "@budibase/bbui"
import AuthTypeRenderer from "./AuthTypeRenderer.svelte" import AuthTypeRenderer from "./AuthTypeRenderer.svelte"
import RestAuthenticationModal from "./RestAuthenticationModal.svelte" import RestAuthenticationModal from "./RestAuthenticationModal.svelte"
import { uuid } from "builderStore/uuid" import { Helpers } from "@budibase/bbui"
export let configs = [] export let configs = []
@ -29,7 +29,7 @@
return c return c
}) })
} else { } else {
config._id = uuid() config._id = Helpers.uuid()
configs = [...configs, config] configs = [...configs, config]
} }
} }

View File

@ -9,7 +9,7 @@
Body, Body,
} from "@budibase/bbui" } from "@budibase/bbui"
import { tables } from "stores/backend" import { tables } from "stores/backend"
import { uuid } from "builderStore/uuid" import { Helpers } from "@budibase/bbui"
import { writable } from "svelte/store" import { writable } from "svelte/store"
export let save export let save
@ -151,7 +151,7 @@
const manyToMany = const manyToMany =
fromRelationship.relationshipType === RelationshipTypes.MANY_TO_MANY fromRelationship.relationshipType === RelationshipTypes.MANY_TO_MANY
// main is simply used to know this is the side the user configured it from // main is simply used to know this is the side the user configured it from
const id = uuid() const id = Helpers.uuid()
if (!manyToMany) { if (!manyToMany) {
delete fromRelationship.through delete fromRelationship.through
delete toRelationship.through delete toRelationship.through

View File

@ -12,7 +12,7 @@
import { dndzone } from "svelte-dnd-action" import { dndzone } from "svelte-dnd-action"
import { generate } from "shortid" import { generate } from "shortid"
import DrawerBindableInput from "components/common/bindings/DrawerBindableInput.svelte" import DrawerBindableInput from "components/common/bindings/DrawerBindableInput.svelte"
import { OperatorOptions, getValidOperatorsForType } from "constants/lucene" import { LuceneUtils, Constants } from "@budibase/frontend-core"
import { selectedComponent } from "builderStore" import { selectedComponent } from "builderStore"
import { getComponentForSettingType } from "./componentSettings" import { getComponentForSettingType } from "./componentSettings"
import PropertyControl from "./PropertyControl.svelte" import PropertyControl from "./PropertyControl.svelte"
@ -83,7 +83,7 @@
valueType: "string", valueType: "string",
id: generate(), id: generate(),
action: "hide", action: "hide",
operator: OperatorOptions.Equals.value, operator: Constants.OperatorOptions.Equals.value,
}, },
] ]
} }
@ -108,13 +108,13 @@
} }
const getOperatorOptions = condition => { const getOperatorOptions = condition => {
return getValidOperatorsForType(condition.valueType) return LuceneUtils.getValidOperatorsForType(condition.valueType)
} }
const onOperatorChange = (condition, newOperator) => { const onOperatorChange = (condition, newOperator) => {
const noValueOptions = [ const noValueOptions = [
OperatorOptions.Empty.value, Constants.OperatorOptions.Empty.value,
OperatorOptions.NotEmpty.value, Constants.OperatorOptions.NotEmpty.value,
] ]
condition.noValue = noValueOptions.includes(newOperator) condition.noValue = noValueOptions.includes(newOperator)
if (condition.noValue) { if (condition.noValue) {
@ -127,9 +127,12 @@
condition.referenceValue = null condition.referenceValue = null
// Ensure a valid operator is set // Ensure a valid operator is set
const validOperators = getValidOperatorsForType(newType).map(x => x.value) const validOperators = LuceneUtils.getValidOperatorsForType(newType).map(
x => x.value
)
if (!validOperators.includes(condition.operator)) { if (!validOperators.includes(condition.operator)) {
condition.operator = validOperators[0] ?? OperatorOptions.Equals.value condition.operator =
validOperators[0] ?? Constants.OperatorOptions.Equals.value
onOperatorChange(condition, condition.operator) onOperatorChange(condition, condition.operator)
} }
} }

View File

@ -13,7 +13,7 @@
import DrawerBindableInput from "components/common/bindings/DrawerBindableInput.svelte" import DrawerBindableInput from "components/common/bindings/DrawerBindableInput.svelte"
import ClientBindingPanel from "components/common/bindings/ClientBindingPanel.svelte" import ClientBindingPanel from "components/common/bindings/ClientBindingPanel.svelte"
import { generate } from "shortid" import { generate } from "shortid"
import { getValidOperatorsForType, OperatorOptions } from "constants/lucene" import { LuceneUtils, Constants } from "@budibase/frontend-core"
import { getFields } from "helpers/searchFields" import { getFields } from "helpers/searchFields"
export let schemaFields export let schemaFields
@ -32,7 +32,7 @@
{ {
id: generate(), id: generate(),
field: null, field: null,
operator: OperatorOptions.Equals.value, operator: Constants.OperatorOptions.Equals.value,
value: null, value: null,
valueType: "Value", valueType: "Value",
}, },
@ -54,11 +54,12 @@
expression.type = enrichedSchemaFields.find(x => x.name === field)?.type expression.type = enrichedSchemaFields.find(x => x.name === field)?.type
// Ensure a valid operator is set // Ensure a valid operator is set
const validOperators = getValidOperatorsForType(expression.type).map( const validOperators = LuceneUtils.getValidOperatorsForType(
x => x.value expression.type
) ).map(x => x.value)
if (!validOperators.includes(expression.operator)) { if (!validOperators.includes(expression.operator)) {
expression.operator = validOperators[0] ?? OperatorOptions.Equals.value expression.operator =
validOperators[0] ?? Constants.OperatorOptions.Equals.value
onOperatorChange(expression, expression.operator) onOperatorChange(expression, expression.operator)
} }
@ -73,8 +74,8 @@
const onOperatorChange = (expression, operator) => { const onOperatorChange = (expression, operator) => {
const noValueOptions = [ const noValueOptions = [
OperatorOptions.Empty.value, Constants.OperatorOptions.Empty.value,
OperatorOptions.NotEmpty.value, Constants.OperatorOptions.NotEmpty.value,
] ]
expression.noValue = noValueOptions.includes(operator) expression.noValue = noValueOptions.includes(operator)
if (expression.noValue) { if (expression.noValue) {
@ -110,7 +111,7 @@
/> />
<Select <Select
disabled={!filter.field} disabled={!filter.field}
options={getValidOperatorsForType(filter.type)} options={LuceneUtils.getValidOperatorsForType(filter.type)}
bind:value={filter.operator} bind:value={filter.operator}
on:change={e => onOperatorChange(filter, e.detail)} on:change={e => onOperatorChange(filter, e.detail)}
placeholder={null} placeholder={null}

View File

@ -4,7 +4,7 @@
// add a core package. // add a core package.
import { writable, derived, get } from "svelte/store" import { writable, derived, get } from "svelte/store"
import * as API from "../builderStore/api" import * as API from "../builderStore/api"
import { buildLuceneQuery } from "./lucene" import { LuceneUtils } from "@budibase/frontend-core"
const defaultOptions = { const defaultOptions = {
tableId: null, tableId: null,
@ -113,7 +113,7 @@ export const fetchTableData = opts => {
sortType = type === "number" ? "number" : "string" sortType = type === "number" ? "number" : "string"
// Build the lucene query // Build the lucene query
query = buildLuceneQuery(filters) query = LuceneUtils.buildLuceneQuery(filters)
// Actually fetch data // Actually fetch data
const page = await fetchPage() const page = await fetchPage()

View File

@ -2,12 +2,7 @@
import { isActive, redirect, params } from "@roxi/routify" import { isActive, redirect, params } from "@roxi/routify"
import { admin, auth } from "stores/portal" import { admin, auth } from "stores/portal"
import { onMount } from "svelte" import { onMount } from "svelte"
import { import { CookieUtils, Constants } from "@budibase/frontend-core"
Cookies,
getCookie,
removeCookie,
setCookie,
} from "builderStore/cookies"
let loaded = false let loaded = false
@ -79,7 +74,7 @@
loaded && loaded &&
apiReady && apiReady &&
!$auth.user && !$auth.user &&
!getCookie(Cookies.ReturnUrl) && !CookieUtils.getCookie(Constants.Cookies.ReturnUrl) &&
// logout triggers a page refresh, so we don't want to set the return url // logout triggers a page refresh, so we don't want to set the return url
!$auth.postLogout && !$auth.postLogout &&
// don't set the return url on pre-login pages // don't set the return url on pre-login pages
@ -88,7 +83,7 @@
!$isActive("./admin") !$isActive("./admin")
) { ) {
const url = window.location.pathname const url = window.location.pathname
setCookie(Cookies.ReturnUrl, url) CookieUtils.setCookie(Constants.Cookies.ReturnUrl, url)
} }
// if tenant is not set go to it // if tenant is not set go to it
@ -122,9 +117,9 @@
} }
// lastly, redirect to the return url if it has been set // lastly, redirect to the return url if it has been set
else if (loaded && apiReady && $auth.user) { else if (loaded && apiReady && $auth.user) {
const returnUrl = getCookie(Cookies.ReturnUrl) const returnUrl = CookieUtils.getCookie(Constants.Cookies.ReturnUrl)
if (returnUrl) { if (returnUrl) {
removeCookie(Cookies.ReturnUrl) CookieUtils.removeCookie(Constants.Cookies.ReturnUrl)
window.location.href = returnUrl window.location.href = returnUrl
} }
} }

View File

@ -22,7 +22,7 @@
import { onMount } from "svelte" import { onMount } from "svelte"
import api from "builderStore/api" import api from "builderStore/api"
import { organisation, admin } from "stores/portal" import { organisation, admin } from "stores/portal"
import { uuid } from "builderStore/uuid" import { Helpers } from "@budibase/bbui"
import analytics, { Events } from "analytics" import analytics, { Events } from "analytics"
const ConfigTypes = { const ConfigTypes = {
@ -164,7 +164,7 @@
//Add a UUID here so each config is distinguishable when it arrives at the login page //Add a UUID here so each config is distinguishable when it arrives at the login page
for (let config of element.config.configs) { for (let config of element.config.configs) {
if (!config.uuid) { if (!config.uuid) {
config.uuid = uuid() config.uuid = Helpers.uuid()
} }
// callback urls shouldn't be included // callback urls shouldn't be included
delete config.callbackURL delete config.callbackURL

View File

@ -11,12 +11,11 @@
Select, Select,
} from "@budibase/bbui" } from "@budibase/bbui"
import { generate } from "shortid" import { generate } from "shortid"
import { LuceneUtils } from "@budibase/frontend-core" import { LuceneUtils, Constants } from "@budibase/frontend-core"
export let schemaFields export let schemaFields
export let filters = [] export let filters = []
const operatorOptions = LuceneUtils.OperatorOptions
const BannedTypes = ["link", "attachment", "formula", "json"] const BannedTypes = ["link", "attachment", "formula", "json"]
$: fieldOptions = (schemaFields ?? []) $: fieldOptions = (schemaFields ?? [])
@ -29,7 +28,7 @@
{ {
id: generate(), id: generate(),
field: null, field: null,
operator: operatorOptions.Equals.value, operator: Constants.OperatorOptions.Equals.value,
value: null, value: null,
valueType: "Value", valueType: "Value",
}, },
@ -55,7 +54,8 @@
expression.type expression.type
).map(x => x.value) ).map(x => x.value)
if (!validOperators.includes(expression.operator)) { if (!validOperators.includes(expression.operator)) {
expression.operator = validOperators[0] ?? operatorOptions.Equals.value expression.operator =
validOperators[0] ?? Constants.OperatorOptions.Equals.value
onOperatorChange(expression, expression.operator) onOperatorChange(expression, expression.operator)
} }
@ -70,8 +70,8 @@
const onOperatorChange = (expression, operator) => { const onOperatorChange = (expression, operator) => {
const noValueOptions = [ const noValueOptions = [
operatorOptions.Empty.value, Constants.OperatorOptions.Empty.value,
operatorOptions.NotEmpty.value, Constants.OperatorOptions.NotEmpty.value,
] ]
expression.noValue = noValueOptions.includes(operator) expression.noValue = noValueOptions.includes(operator)
if (expression.noValue) { if (expression.noValue) {

View File

@ -1,10 +1,65 @@
/**
* Operator options for lucene queries
*/
export const OperatorOptions = {
Equals: {
value: "equal",
label: "Equals",
},
NotEquals: {
value: "notEqual",
label: "Not equals",
},
Empty: {
value: "empty",
label: "Is empty",
},
NotEmpty: {
value: "notEmpty",
label: "Is not empty",
},
StartsWith: {
value: "string",
label: "Starts with",
},
Like: {
value: "fuzzy",
label: "Like",
},
MoreThan: {
value: "rangeLow",
label: "More than",
},
LessThan: {
value: "rangeHigh",
label: "Less than",
},
Contains: {
value: "equal",
label: "Contains",
},
NotContains: {
value: "notEqual",
label: "Does Not Contain",
},
}
// Cookie names
export const Cookies = {
Auth: "budibase:auth",
CurrentApp: "budibase:currentapp",
ReturnUrl: "budibase:returnurl",
}
// Table names
export const TableNames = { export const TableNames = {
USERS: "ta_users", USERS: "ta_users",
} }
export const ApiVersion = "1"
/** /**
* API Version Changelog * API version header attached to all requests.
* Version changelog:
* v1: * v1:
* - Coerce types for search endpoint * - Coerce types for search endpoint
*/ */
export const ApiVersion = "1"

View File

@ -1,9 +1,3 @@
export const Cookies = {
Auth: "budibase:auth",
CurrentApp: "budibase:currentapp",
ReturnUrl: "budibase:returnurl",
}
export function setCookie(name, value) { export function setCookie(name, value) {
if (getCookie(name)) { if (getCookie(name)) {
removeCookie(name) removeCookie(name)

View File

@ -1,50 +1,5 @@
import { Helpers } from "@budibase/bbui" import { Helpers } from "@budibase/bbui"
import { OperatorOptions } from "../constants"
/**
* Operator options for lucene queries
*/
export const OperatorOptions = {
Equals: {
value: "equal",
label: "Equals",
},
NotEquals: {
value: "notEqual",
label: "Not equals",
},
Empty: {
value: "empty",
label: "Is empty",
},
NotEmpty: {
value: "notEmpty",
label: "Is not empty",
},
StartsWith: {
value: "string",
label: "Starts with",
},
Like: {
value: "fuzzy",
label: "Like",
},
MoreThan: {
value: "rangeLow",
label: "More than",
},
LessThan: {
value: "rangeHigh",
label: "Less than",
},
Contains: {
value: "equal",
label: "Contains",
},
NotContains: {
value: "notEqual",
label: "Does Not Contain",
},
}
/** /**
* Returns the valid operator options for a certain data type * Returns the valid operator options for a certain data type