From 58b5502f77f97a780cd91e84bf6b53cce8d855c9 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Thu, 8 Oct 2020 22:06:44 +0100 Subject: [PATCH] Save Record Action --- .../EventsEditor/actions/SaveRecord.svelte | 138 +++++++++++++ .../EventsEditor/actions/index.js | 11 +- .../userInterface/temporaryPanelStructure.js | 36 +--- packages/client/src/api/index.js | 20 +- packages/client/src/state/eventHandlers.js | 4 +- packages/client/src/state/store.js | 2 +- packages/standard-components/components.json | 12 +- packages/standard-components/src/Form.svelte | 190 ++++-------------- .../standard-components/src/NewRecord.svelte | 12 +- .../src/RecordDetail.svelte | 2 + 10 files changed, 212 insertions(+), 215 deletions(-) create mode 100644 packages/builder/src/components/userInterface/EventsEditor/actions/SaveRecord.svelte diff --git a/packages/builder/src/components/userInterface/EventsEditor/actions/SaveRecord.svelte b/packages/builder/src/components/userInterface/EventsEditor/actions/SaveRecord.svelte new file mode 100644 index 0000000000..dcb9822c37 --- /dev/null +++ b/packages/builder/src/components/userInterface/EventsEditor/actions/SaveRecord.svelte @@ -0,0 +1,138 @@ + + +
+ {#if idFields.length === 0} +
+ Update record can only be used within a component that provides data, such + as a List +
+ {:else} + + + {/if} + + {#if parameters.contextPath} + + {/if} + +
+ + diff --git a/packages/builder/src/components/userInterface/EventsEditor/actions/index.js b/packages/builder/src/components/userInterface/EventsEditor/actions/index.js index e1a85e307e..85f90d124b 100644 --- a/packages/builder/src/components/userInterface/EventsEditor/actions/index.js +++ b/packages/builder/src/components/userInterface/EventsEditor/actions/index.js @@ -1,6 +1,5 @@ import NavigateTo from "./NavigateTo.svelte" -import UpdateRecord from "./UpdateRecord.svelte" -import CreateRecord from "./CreateRecord.svelte" +import SaveRecord from "./SaveRecord.svelte" // defines what actions are available, when adding a new one // the component is the setup panel for the action @@ -9,15 +8,11 @@ import CreateRecord from "./CreateRecord.svelte" export default [ { - name: "Create Record", - component: CreateRecord, + name: "Save Record", + component: SaveRecord, }, { name: "Navigate To", component: NavigateTo, }, - { - name: "Update Record", - component: UpdateRecord, - }, ] diff --git a/packages/builder/src/components/userInterface/temporaryPanelStructure.js b/packages/builder/src/components/userInterface/temporaryPanelStructure.js index 296ae26606..7b291251cb 100644 --- a/packages/builder/src/components/userInterface/temporaryPanelStructure.js +++ b/packages/builder/src/components/userInterface/temporaryPanelStructure.js @@ -583,23 +583,7 @@ export default { icon: "ri-file-edit-line", properties: { design: { ...all }, - settings: [ - { - label: "Table", - key: "model", - control: ModelSelect, - }, - { - label: "Title", - key: "title", - control: Input, - }, - { - label: "Button Text", - key: "buttonText", - control: Input, - }, - ], + settings: [], }, }, { @@ -608,23 +592,7 @@ export default { icon: "ri-file-edit-line", properties: { design: { ...all }, - settings: [ - { - label: "Table", - key: "model", - control: ModelSelect, - }, - { - label: "Title", - key: "title", - control: Input, - }, - { - label: "Button Text", - key: "buttonText", - control: Input, - }, - ], + settings: [], }, }, ], diff --git a/packages/client/src/api/index.js b/packages/client/src/api/index.js index 34441d563b..f66cfcf69b 100644 --- a/packages/client/src/api/index.js +++ b/packages/client/src/api/index.js @@ -52,14 +52,14 @@ const apiOpts = { delete: del, } -const createRecord = async params => +const saveRecord = async (params, state) => await post({ url: `/api/${params.modelId}/records`, - body: makeRecordRequestBody(params), + body: makeRecordRequestBody(params, state), }) -const updateRecord = async params => { - const record = makeRecordRequestBody(params) +const updateRecord = async (params, state) => { + const record = makeRecordRequestBody(params, state) record._id = params._id await patch({ url: `/api/${params.modelId}/records/${params._id}`, @@ -67,8 +67,14 @@ const updateRecord = async params => { }) } -const makeRecordRequestBody = parameters => { - const body = {} +const makeRecordRequestBody = (parameters, state) => { + // start with the record thats currently in context + const body = { ...(state.data || {}) } + + // dont send the model + if (body._model) delete body._model + + // then override with supplied parameters for (let fieldName in parameters.fields) { const field = parameters.fields[fieldName] @@ -95,6 +101,6 @@ const makeRecordRequestBody = parameters => { export default { authenticate: authenticate(apiOpts), - createRecord, + saveRecord, updateRecord, } diff --git a/packages/client/src/state/eventHandlers.js b/packages/client/src/state/eventHandlers.js index 05d8ef2fa3..fa80605b42 100644 --- a/packages/client/src/state/eventHandlers.js +++ b/packages/client/src/state/eventHandlers.js @@ -6,8 +6,8 @@ export const EVENT_TYPE_MEMBER_NAME = "##eventHandlerType" export const eventHandlers = routeTo => { const handlers = { "Navigate To": param => routeTo(param && param.url), - "Create Record": api.createRecord, "Update Record": api.updateRecord, + "Save Record": api.saveRecord, "Trigger Workflow": api.triggerWorkflow, } @@ -19,7 +19,7 @@ export const eventHandlers = routeTo => { const handler = handlers[action[EVENT_TYPE_MEMBER_NAME]] const parameters = createParameters(action.parameters, state) if (handler) { - await handler(parameters) + await handler(parameters, state) } } } diff --git a/packages/client/src/state/store.js b/packages/client/src/state/store.js index 6464589b03..cf284ec08e 100644 --- a/packages/client/src/state/store.js +++ b/packages/client/src/state/store.js @@ -95,7 +95,7 @@ const getState = contextStoreKey => contextStoreKey ? contextStores[contextStoreKey].state : rootState const getStore = contextStoreKey => - contextStoreKey ? contextStores[contextStoreKey] : rootStore + contextStoreKey ? contextStores[contextStoreKey].store : rootStore export default { subscribe, diff --git a/packages/standard-components/components.json b/packages/standard-components/components.json index e8715dfbef..b11dd86503 100644 --- a/packages/standard-components/components.json +++ b/packages/standard-components/components.json @@ -218,20 +218,12 @@ "dataform": { "description": "an HTML table that fetches data from a table or view and displays it.", "data": true, - "props": { - "model": "models", - "title": "string", - "buttonText": "string" - } + "props": {} }, "dataformwide": { "description": "an HTML table that fetches data from a table or view and displays it.", "data": true, - "props": { - "model": "models", - "title": "string", - "buttonText": "string" - } + "props": {} }, "datalist": { "description": "A configurable data list that attaches to your backend models.", diff --git a/packages/standard-components/src/Form.svelte b/packages/standard-components/src/Form.svelte index 937a58f271..c9777b52b4 100644 --- a/packages/standard-components/src/Form.svelte +++ b/packages/standard-components/src/Form.svelte @@ -1,168 +1,61 @@ -
- {#if title} -

{title}

- {/if} -
- - {#each fields as field} -
- {#if !(schema[field].type === 'boolean' && !wide)} - - {/if} - {#if schema[field].type === 'options'} - - {:else if schema[field].type === 'datetime'} - - {:else if schema[field].type === 'boolean'} - - {:else if schema[field].type === 'number'} - - {:else if schema[field].type === 'string'} - - {:else if schema[field].type === 'attachment'} - - {:else if schema[field].type === 'link'} - - {/if} -
- {/each} -
- +
+ + {#each fields as field} +
+ {#if !(schema[field].type === 'boolean' && !wide)} + + {/if} + {#if schema[field].type === 'options'} + + {:else if schema[field].type === 'datetime'} + + {:else if schema[field].type === 'boolean'} + + {:else if schema[field].type === 'number'} + + {:else if schema[field].type === 'string'} + + {:else if schema[field].type === 'attachment'} + + {:else if schema[field].type === 'link'} + + {/if}
-
- + {/each} +
diff --git a/packages/standard-components/src/NewRecord.svelte b/packages/standard-components/src/NewRecord.svelte index 923611d760..03b61081e5 100644 --- a/packages/standard-components/src/NewRecord.svelte +++ b/packages/standard-components/src/NewRecord.svelte @@ -1,6 +1,5 @@