2024-03-12 18:01:07 +01:00
|
|
|
import sanitizeUrl from "helpers/sanitizeUrl"
|
|
|
|
import { Screen } from "./Screen"
|
|
|
|
import { Component } from "./Component"
|
|
|
|
import { generate } from "shortid"
|
|
|
|
import { makePropSafe as safe } from "@budibase/string-templates"
|
|
|
|
import { Utils } from "@budibase/frontend-core"
|
|
|
|
|
2024-07-29 13:59:24 +02:00
|
|
|
const gridDetailsUrl = tableOrView => sanitizeUrl(`/${tableOrView.name}`)
|
2024-03-12 18:01:07 +01:00
|
|
|
|
2024-07-29 13:59:24 +02:00
|
|
|
const createScreen = (tableOrView, permissions) => {
|
2024-03-12 18:01:07 +01:00
|
|
|
/*
|
|
|
|
Create Row
|
|
|
|
*/
|
|
|
|
const createRowSidePanel = new Component(
|
|
|
|
"@budibase/standard-components/sidepanel"
|
|
|
|
).instanceName("New row side panel")
|
|
|
|
|
|
|
|
const buttonGroup = new Component("@budibase/standard-components/buttongroup")
|
|
|
|
const createButton = new Component("@budibase/standard-components/button")
|
|
|
|
|
2024-03-21 12:16:19 +01:00
|
|
|
createButton.customProps({
|
2024-03-12 18:01:07 +01:00
|
|
|
onClick: [
|
|
|
|
{
|
|
|
|
id: 0,
|
|
|
|
"##eventHandlerType": "Open Side Panel",
|
|
|
|
parameters: {
|
|
|
|
id: createRowSidePanel._json._id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
text: "Create row",
|
|
|
|
type: "cta",
|
|
|
|
})
|
|
|
|
|
2024-07-29 13:59:24 +02:00
|
|
|
buttonGroup.instanceName(`${tableOrView.name} - Create`).customProps({
|
2024-03-12 18:01:07 +01:00
|
|
|
hAlign: "right",
|
|
|
|
buttons: [createButton.json()],
|
|
|
|
})
|
|
|
|
|
|
|
|
const gridHeader = new Component("@budibase/standard-components/container")
|
|
|
|
.instanceName("Heading container")
|
|
|
|
.customProps({
|
|
|
|
direction: "row",
|
|
|
|
hAlign: "stretch",
|
|
|
|
})
|
|
|
|
|
|
|
|
const heading = new Component("@budibase/standard-components/heading")
|
|
|
|
.instanceName("Table heading")
|
|
|
|
.customProps({
|
2024-07-29 13:59:24 +02:00
|
|
|
text: tableOrView.name,
|
2024-03-12 18:01:07 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
gridHeader.addChild(heading)
|
|
|
|
gridHeader.addChild(buttonGroup)
|
|
|
|
|
|
|
|
const createFormBlock = new Component(
|
|
|
|
"@budibase/standard-components/formblock"
|
|
|
|
)
|
2024-03-21 12:16:19 +01:00
|
|
|
createFormBlock.instanceName("Create row form block").customProps({
|
2024-07-29 13:59:24 +02:00
|
|
|
dataSource: tableOrView.clientData,
|
2024-03-12 18:01:07 +01:00
|
|
|
labelPosition: "left",
|
|
|
|
buttonPosition: "top",
|
|
|
|
actionType: "Create",
|
|
|
|
title: "Create row",
|
|
|
|
buttons: Utils.buildFormBlockButtonConfig({
|
|
|
|
_id: createFormBlock._json._id,
|
|
|
|
showDeleteButton: false,
|
|
|
|
showSaveButton: true,
|
|
|
|
saveButtonLabel: "Save",
|
|
|
|
actionType: "Create",
|
2024-07-29 13:59:24 +02:00
|
|
|
dataSource: tableOrView.clientData,
|
2024-03-12 18:01:07 +01:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
createRowSidePanel.addChild(createFormBlock)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Edit Row
|
|
|
|
*/
|
|
|
|
const stateKey = `ID_${generate()}`
|
|
|
|
const detailsSidePanel = new Component(
|
|
|
|
"@budibase/standard-components/sidepanel"
|
|
|
|
).instanceName("Edit row side panel")
|
|
|
|
|
|
|
|
const editFormBlock = new Component("@budibase/standard-components/formblock")
|
2024-03-21 12:16:19 +01:00
|
|
|
editFormBlock.instanceName("Edit row form block").customProps({
|
2024-07-29 13:59:24 +02:00
|
|
|
dataSource: tableOrView.clientData,
|
2024-03-12 18:01:07 +01:00
|
|
|
labelPosition: "left",
|
|
|
|
buttonPosition: "top",
|
|
|
|
actionType: "Update",
|
|
|
|
title: "Edit",
|
|
|
|
rowId: `{{ ${safe("state")}.${safe(stateKey)} }}`,
|
|
|
|
buttons: Utils.buildFormBlockButtonConfig({
|
|
|
|
_id: editFormBlock._json._id,
|
|
|
|
showDeleteButton: true,
|
|
|
|
showSaveButton: true,
|
|
|
|
saveButtonLabel: "Save",
|
|
|
|
deleteButtonLabel: "Delete",
|
|
|
|
actionType: "Update",
|
2024-07-29 13:59:24 +02:00
|
|
|
dataSource: tableOrView.clientData,
|
2024-03-12 18:01:07 +01:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
detailsSidePanel.addChild(editFormBlock)
|
|
|
|
|
|
|
|
const gridBlock = new Component("@budibase/standard-components/gridblock")
|
|
|
|
gridBlock
|
|
|
|
.customProps({
|
2024-07-29 13:59:24 +02:00
|
|
|
table: tableOrView.clientData,
|
2024-03-12 18:01:07 +01:00
|
|
|
allowAddRows: false,
|
|
|
|
allowEditRows: false,
|
|
|
|
allowDeleteRows: false,
|
|
|
|
onRowClick: [
|
|
|
|
{
|
|
|
|
id: 0,
|
|
|
|
"##eventHandlerType": "Update State",
|
|
|
|
parameters: {
|
|
|
|
key: stateKey,
|
|
|
|
type: "set",
|
|
|
|
persist: false,
|
|
|
|
value: `{{ ${safe("eventContext")}.${safe("row")}._id }}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
"##eventHandlerType": "Open Side Panel",
|
|
|
|
parameters: {
|
|
|
|
id: detailsSidePanel._json._id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
2024-07-29 13:59:24 +02:00
|
|
|
.instanceName(`${tableOrView.name} - Table`)
|
2024-03-12 18:01:07 +01:00
|
|
|
|
|
|
|
return new Screen()
|
2024-07-29 13:59:24 +02:00
|
|
|
.route(gridDetailsUrl(tableOrView))
|
|
|
|
.instanceName(`${tableOrView.name} - List and details`)
|
|
|
|
.role(permissions.write)
|
|
|
|
.autoTableId(tableOrView.resourceId)
|
2024-03-12 18:01:07 +01:00
|
|
|
.addChild(gridHeader)
|
|
|
|
.addChild(gridBlock)
|
|
|
|
.addChild(createRowSidePanel)
|
|
|
|
.addChild(detailsSidePanel)
|
|
|
|
.json()
|
|
|
|
}
|
2024-07-29 13:59:24 +02:00
|
|
|
|
|
|
|
export default createScreen
|