Replace other usages of manual row actions API calls with store usage

This commit is contained in:
Andrew Kingston 2024-09-03 19:47:12 +01:00
parent 53ff7e1167
commit 872446e524
No known key found for this signature in database
6 changed files with 48 additions and 52 deletions

View File

@ -1,5 +1,5 @@
<script>
import { automationStore } from "stores/builder"
import { automationStore, rowActions } from "stores/builder"
import {
notifications,
Icon,
@ -26,12 +26,11 @@
async function saveAutomation() {
try {
await API.rowActions.update({
rowActionId: automation.definition.trigger.inputs.rowActionId,
tableId: automation.definition.trigger.inputs.tableId,
name,
})
await automationStore.actions.fetch()
await rowActions.rename(
automation.definition.trigger.inputs.tableId,
automation.definition.trigger.inputs.rowActionId,
name
)
notifications.success(`Row action updated successfully`)
hide()
} catch (error) {

View File

@ -1,14 +1,11 @@
<script>
import { Select, Label, Checkbox, Body } from "@budibase/bbui"
import { tables, viewsV2 } from "stores/builder"
import { Select, Label, Checkbox } from "@budibase/bbui"
import { tables, viewsV2, rowActions } from "stores/builder"
import DrawerBindableInput from "components/common/bindings/DrawerBindableInput.svelte"
import { API } from "api"
export let parameters
export let bindings = []
let rowActions = []
$: tableOptions = $tables.list.map(table => ({
label: table.name,
resourceId: table._id,
@ -20,35 +17,12 @@
}))
$: datasourceOptions = [...(tableOptions || []), ...(viewOptions || [])]
$: resourceId = parameters.resourceId
$: fetchRowActions(resourceId)
$: rowActionOptions = rowActions.map(action => ({
$: rowActions.refreshRowActions(resourceId)
$: enabledRowActions = $rowActions[resourceId] || []
$: rowActionOptions = enabledRowActions.map(action => ({
label: action.name,
value: action.id,
}))
const fetchRowActions = async resourceId => {
if (!resourceId) {
rowActions = []
return
}
try {
const isView = resourceId.startsWith("view_")
let tableId = resourceId
if (isView) {
tableId = viewOptions.find(x => x.resourceId === resourceId).tableId
}
const res = await API.rowActions.fetch(tableId)
rowActions = Object.values(res || {}).filter(action => {
return !isView || action.allowedViews?.includes(resourceId)
})
} catch (err) {
console.error(err)
rowActions = []
}
// Auto select first action
parameters.rowActionId = rowActions[0]?.id
}
</script>
<div class="root">

View File

@ -28,11 +28,7 @@
return (rowActions || []).map(action => ({
text: action.name,
onClick: async row => {
await API.rowActions.trigger({
rowActionId: action.id,
sourceId: id,
rowId: row._id,
})
await rowActions.trigger(id, action.id, row._id)
},
}))
}

View File

@ -66,11 +66,7 @@
return (rowActions || []).map(action => ({
text: action.name,
onClick: async row => {
await API.rowActions.trigger({
rowActionId: action.id,
sourceId: id,
rowId: row._id,
})
await rowActions.trigger(id, action.id, row._id)
},
}))
}

View File

@ -7,6 +7,7 @@ import { notifications } from "@budibase/bbui"
import { updateReferencesInObject } from "dataBinding"
import { AutomationTriggerStepId } from "@budibase/types"
import { sdk } from "@budibase/shared-core"
import { rowActions } from "./rowActions"
const initialAutomationState = {
automations: [],
@ -126,10 +127,10 @@ const automationActions = store => ({
delete: async automation => {
const isRowAction = sdk.automations.isRowAction(automation)
if (isRowAction) {
await API.rowActions.delete({
tableId: automation.definition.trigger.inputs.tableId,
rowActionId: automation.definition.trigger.inputs.rowActionId,
})
await rowActions.delete(
automation.definition.trigger.inputs.tableId,
automation.definition.trigger.inputs.rowActionId
)
} else {
await API.deleteAutomation({
automationId: automation?._id,

View File

@ -86,6 +86,36 @@ export class RowActionStore extends BudiStore {
})
await this.refreshRowActions(tableId)
}
rename = async (tableId, rowActionId, name) => {
await API.rowActions.update({
tableId,
rowActionId,
name,
})
await Promise.all([
this.refreshRowActions(tableId),
automationStore.actions.fetch(),
])
}
delete = async (tableId, rowActionId) => {
await API.rowActions.delete({
tableId,
rowActionId,
})
await this.refreshRowActions(tableId)
// We don't need to refresh automations as we can only delete row actions
// from the automations store, so we already handle the state update there
}
trigger = async (sourceId, rowActionId, rowId) => {
await API.rowActions.trigger({
sourceId,
rowActionId,
rowId,
})
}
}
const store = new RowActionStore()