From 388b3f9e5a85be71960a7b83c7f924fc0ec4b3a3 Mon Sep 17 00:00:00 2001 From: Zack Braksa Date: Fri, 2 Aug 2024 16:36:03 +0100 Subject: [PATCH] Move modify:save to VxgBasicEntityEditPlugin --- dist/VxgBasicEntityEditPlugin.d.ts | 2 + dist/voxgig-model-react.es.js | 123 ++++++++++++--- dist/voxgig-model-react.umd.js | 123 ++++++++++++--- src/lib/BasicEntityEdit.tsx | 17 ++- src/lib/VxgBasicEntityEditPlugin.ts | 229 +++++++++++++++++++++------- src/lib/VxgBasicLedPlugin.ts | 12 -- 6 files changed, 382 insertions(+), 124 deletions(-) diff --git a/dist/VxgBasicEntityEditPlugin.d.ts b/dist/VxgBasicEntityEditPlugin.d.ts index 77d9312..1c29b3f 100644 --- a/dist/VxgBasicEntityEditPlugin.d.ts +++ b/dist/VxgBasicEntityEditPlugin.d.ts @@ -7,6 +7,8 @@ declare function VxgBasicEntityEditPlugin(this: any, options: any): { }; util: { dateTimeFromUTC: (utc: number, tz?: string) => any; + localTimeToUTC: (timeString: string, tz?: string) => number; + localDateTimeToUTC: (dateOrDateTimeString: string, tz?: string) => number; }; }; }; diff --git a/dist/voxgig-model-react.es.js b/dist/voxgig-model-react.es.js index e05345f..f9e15f3 100644 --- a/dist/voxgig-model-react.es.js +++ b/dist/voxgig-model-react.es.js @@ -64878,10 +64878,10 @@ function BasicEntitySliderField(props) { const { spec } = props; const basicEntityAutocompleteField = BasicEntitySliderFieldSpecShape(spec); const { control, field, getValues, errors } = basicEntityAutocompleteField; - const val = getValues(field.name + "_uival$"); + const val = getValues(field.name); const err = errors[field.name]; const { field: controllerField } = useController({ - name: field.name + "_uival$", + name: field.name, control, defaultValue: val || field.ux.min }); @@ -65671,11 +65671,24 @@ function VxgBasicEntityEditPlugin(options) { const dt = util$1.dateTimeFromUTC(item[field.name]); item[field.name + "_orig$"] = item[field.name]; item[field.name + "_udm$"] = dt.udm; - item[field.name] = dt.localt; + item[field.name] = dt.locald; + console.log("modify_edit_Date", item[field.name]); } return item; }); } + ).add( + "aim:app,on:BasicLed,modify:save", + { view: spec.name }, + function modify_save_Date(msg) { + return __async(this, null, function* () { + const out = yield this.prior(msg); + let item = __spreadValues({}, out); + const dt = util$1.localDateTimeToUTC(item[field.name]); + item[field.name] = dt; + return item; + }); + } ); } else if ("Time" === field.ux.kind) { seneca.add( @@ -65689,10 +65702,23 @@ function VxgBasicEntityEditPlugin(options) { item[field.name + "_orig$"] = item[field.name]; item[field.name + "_udm$"] = dt.udm; item[field.name] = dt.localt; + console.log("modify_edit_Time", item[field.name]); } return item; }); } + ).add( + "aim:app,on:BasicLed,modify:save", + { view: spec.name }, + function modify_save_Time(msg) { + return __async(this, null, function* () { + const out = yield this.prior(msg); + let item = __spreadValues({}, out); + const dt = util$1.localTimeToUTC(item[field.name]); + item[field.name] = dt; + return item; + }); + } ); } else if ("DateTime" === field.ux.kind) { seneca.add( @@ -65700,7 +65726,6 @@ function VxgBasicEntityEditPlugin(options) { function modify_edit_Datetime(msg) { return __async(this, null, function* () { const out = yield this.prior(msg); - console.log("out", out); let item = __spreadValues({}, out); if (!item[field.name + "_orig$"]) { const dt = util$1.dateTimeFromUTC(item[field.name]); @@ -65711,6 +65736,18 @@ function VxgBasicEntityEditPlugin(options) { return item; }); } + ).add( + "aim:app,on:BasicLed,modify:save", + { view: spec.name }, + function modify_save_Datetime(msg) { + return __async(this, null, function* () { + const out = yield this.prior(msg); + let item = __spreadValues({}, out); + const dt = util$1.localDateTimeToUTC(item[field.name]); + item[field.name] = dt; + return item; + }); + } ); } else if ("Slider" === field.ux.kind) { seneca.add( @@ -65720,12 +65757,24 @@ function VxgBasicEntityEditPlugin(options) { return __async(this, null, function* () { const out = yield this.prior(msg); let item = __spreadValues({}, out); - if (!item[field.name + "_uival$"]) { - item[field.name + "_uival$"] = Number(item[field.name]) / 60; + if (!item[field.name + "_orig$"]) { + item[field.name + "_orig$"] = item[field.name]; + item[field.name] = Number(item[field.name]) / 60; } return item; }); } + ).add( + "aim:app,on:BasicLed,modify:save", + { view: spec.name }, + function modify_save_Slider(msg) { + return __async(this, null, function* () { + const out = yield this.prior(msg); + let item = __spreadValues({}, out); + item[field.name] = Number(item[field.name]) * 60; + return item; + }); + } ); } } @@ -65780,6 +65829,41 @@ const util$1 = { out.locald = `${year}-${month}-${day}`; out.localt = `${hour}:${minute}:${second}`; return out; + }, + localTimeToUTC: (timeString, tz) => { + tz = tz || Intl.DateTimeFormat().resolvedOptions().timeZone; + const now = /* @__PURE__ */ new Date(); + const [hours, minutes, seconds] = timeString.split(":").map(Number); + const localDate = new Date( + now.getFullYear(), + now.getMonth(), + now.getDate(), + hours, + minutes, + seconds + ); + const utcTimestamp = Date.UTC( + localDate.getFullYear(), + localDate.getMonth(), + localDate.getDate(), + localDate.getHours(), + localDate.getMinutes(), + localDate.getSeconds() + ); + const tzOffset = new Date(utcTimestamp).getTimezoneOffset() * 6e4; + return utcTimestamp - tzOffset; + }, + localDateTimeToUTC: (dateOrDateTimeString, tz) => { + const date = new Date(dateOrDateTimeString); + if (isNaN(date.getTime())) { + throw new Error("Invalid date or datetime string"); + } + if (tz) { + const tzDate = new Date(date.toLocaleString("en-GB", { timeZone: tz })); + const offset2 = date.getTime() - tzDate.getTime(); + return date.getTime() + offset2; + } + return date.getTime(); } }; Object.assign(VxgBasicEntityEditPlugin, { @@ -65797,7 +65881,6 @@ const makeResolver = (seneca, spec) => useCallback( const { ent, name } = spec; const view = name; let entity = seneca.entity(ent); - console.log("makeResolver", "data", data); entity = entity.make$().data$(data); let errors = entity.valid$({ errors: true }); seneca.act("aim:app,on:BasicLed,entity:valid", { @@ -65889,18 +65972,21 @@ function BasicEntityEdit(props) { mode: "onChange", resolver }); - const onSubmit = (data) => { + const onSubmit = (data) => __async(this, null, function* () { console.log("BasicEntityEdit", "onSubmit", "data", data); - const modifiedData = seneca.direct("aim:app,on:BasicLed,modify:save", { - view: name, - data, - fields - }); + const modifiedData = yield seneca.direct( + "aim:app,on:BasicLed,modify:save", + { + view: name, + data, + fields + } + ); seneca.act("aim:app,on:BasicLed,save:item", { view: name, data: modifiedData }); - }; + }); return /* @__PURE__ */ jsxRuntimeExports.jsx(Box$2, { className: "vxg-BasicEntityEdit", children: item ? /* @__PURE__ */ jsxRuntimeExports.jsxs( "form", { @@ -66078,17 +66164,8 @@ function VxgBasicLedPlugin(options) { return item; }).add("aim:app,on:BasicLed,modify:save", function modify_save(msg) { let item = msg.data; - let fields = msg.fields; if (null == item) return item; item = __spreadValues({}, item); - for (const field of fields) { - if ("Slider" === field.ux.kind) { - console.log("VxgBasicLedPlugin", "modify:save", "field", field); - console.log("VxgBasicLedPlugin", "modify:save", "item", item); - item[field.name] = Number(item[field.name + "_uival$"]) * 60; - } - } - console.log("modify:save", "item", item); return item; }).message( "aim:app,on:BasicLed,edit:item,redux$:true", diff --git a/dist/voxgig-model-react.umd.js b/dist/voxgig-model-react.umd.js index cb0c7da..c524b48 100644 --- a/dist/voxgig-model-react.umd.js +++ b/dist/voxgig-model-react.umd.js @@ -64890,10 +64890,10 @@ To suppress this warning, you need to explicitly provide the \`palette.${key}Cha const { spec } = props; const basicEntityAutocompleteField = BasicEntitySliderFieldSpecShape(spec); const { control, field, getValues, errors } = basicEntityAutocompleteField; - const val = getValues(field.name + "_uival$"); + const val = getValues(field.name); const err = errors[field.name]; const { field: controllerField } = useController({ - name: field.name + "_uival$", + name: field.name, control, defaultValue: val || field.ux.min }); @@ -65683,11 +65683,24 @@ To suppress this warning, you need to explicitly provide the \`palette.${key}Cha const dt = util$1.dateTimeFromUTC(item[field.name]); item[field.name + "_orig$"] = item[field.name]; item[field.name + "_udm$"] = dt.udm; - item[field.name] = dt.localt; + item[field.name] = dt.locald; + console.log("modify_edit_Date", item[field.name]); } return item; }); } + ).add( + "aim:app,on:BasicLed,modify:save", + { view: spec.name }, + function modify_save_Date(msg) { + return __async(this, null, function* () { + const out = yield this.prior(msg); + let item = __spreadValues({}, out); + const dt = util$1.localDateTimeToUTC(item[field.name]); + item[field.name] = dt; + return item; + }); + } ); } else if ("Time" === field.ux.kind) { seneca.add( @@ -65701,10 +65714,23 @@ To suppress this warning, you need to explicitly provide the \`palette.${key}Cha item[field.name + "_orig$"] = item[field.name]; item[field.name + "_udm$"] = dt.udm; item[field.name] = dt.localt; + console.log("modify_edit_Time", item[field.name]); } return item; }); } + ).add( + "aim:app,on:BasicLed,modify:save", + { view: spec.name }, + function modify_save_Time(msg) { + return __async(this, null, function* () { + const out = yield this.prior(msg); + let item = __spreadValues({}, out); + const dt = util$1.localTimeToUTC(item[field.name]); + item[field.name] = dt; + return item; + }); + } ); } else if ("DateTime" === field.ux.kind) { seneca.add( @@ -65712,7 +65738,6 @@ To suppress this warning, you need to explicitly provide the \`palette.${key}Cha function modify_edit_Datetime(msg) { return __async(this, null, function* () { const out = yield this.prior(msg); - console.log("out", out); let item = __spreadValues({}, out); if (!item[field.name + "_orig$"]) { const dt = util$1.dateTimeFromUTC(item[field.name]); @@ -65723,6 +65748,18 @@ To suppress this warning, you need to explicitly provide the \`palette.${key}Cha return item; }); } + ).add( + "aim:app,on:BasicLed,modify:save", + { view: spec.name }, + function modify_save_Datetime(msg) { + return __async(this, null, function* () { + const out = yield this.prior(msg); + let item = __spreadValues({}, out); + const dt = util$1.localDateTimeToUTC(item[field.name]); + item[field.name] = dt; + return item; + }); + } ); } else if ("Slider" === field.ux.kind) { seneca.add( @@ -65732,12 +65769,24 @@ To suppress this warning, you need to explicitly provide the \`palette.${key}Cha return __async(this, null, function* () { const out = yield this.prior(msg); let item = __spreadValues({}, out); - if (!item[field.name + "_uival$"]) { - item[field.name + "_uival$"] = Number(item[field.name]) / 60; + if (!item[field.name + "_orig$"]) { + item[field.name + "_orig$"] = item[field.name]; + item[field.name] = Number(item[field.name]) / 60; } return item; }); } + ).add( + "aim:app,on:BasicLed,modify:save", + { view: spec.name }, + function modify_save_Slider(msg) { + return __async(this, null, function* () { + const out = yield this.prior(msg); + let item = __spreadValues({}, out); + item[field.name] = Number(item[field.name]) * 60; + return item; + }); + } ); } } @@ -65792,6 +65841,41 @@ To suppress this warning, you need to explicitly provide the \`palette.${key}Cha out.locald = `${year}-${month}-${day}`; out.localt = `${hour}:${minute}:${second}`; return out; + }, + localTimeToUTC: (timeString, tz) => { + tz = tz || Intl.DateTimeFormat().resolvedOptions().timeZone; + const now = /* @__PURE__ */ new Date(); + const [hours, minutes, seconds] = timeString.split(":").map(Number); + const localDate = new Date( + now.getFullYear(), + now.getMonth(), + now.getDate(), + hours, + minutes, + seconds + ); + const utcTimestamp = Date.UTC( + localDate.getFullYear(), + localDate.getMonth(), + localDate.getDate(), + localDate.getHours(), + localDate.getMinutes(), + localDate.getSeconds() + ); + const tzOffset = new Date(utcTimestamp).getTimezoneOffset() * 6e4; + return utcTimestamp - tzOffset; + }, + localDateTimeToUTC: (dateOrDateTimeString, tz) => { + const date = new Date(dateOrDateTimeString); + if (isNaN(date.getTime())) { + throw new Error("Invalid date or datetime string"); + } + if (tz) { + const tzDate = new Date(date.toLocaleString("en-GB", { timeZone: tz })); + const offset2 = date.getTime() - tzDate.getTime(); + return date.getTime() + offset2; + } + return date.getTime(); } }; Object.assign(VxgBasicEntityEditPlugin, { @@ -65809,7 +65893,6 @@ To suppress this warning, you need to explicitly provide the \`palette.${key}Cha const { ent, name } = spec; const view = name; let entity = seneca.entity(ent); - console.log("makeResolver", "data", data); entity = entity.make$().data$(data); let errors = entity.valid$({ errors: true }); seneca.act("aim:app,on:BasicLed,entity:valid", { @@ -65901,18 +65984,21 @@ To suppress this warning, you need to explicitly provide the \`palette.${key}Cha mode: "onChange", resolver }); - const onSubmit = (data) => { + const onSubmit = (data) => __async(this, null, function* () { console.log("BasicEntityEdit", "onSubmit", "data", data); - const modifiedData = seneca.direct("aim:app,on:BasicLed,modify:save", { - view: name, - data, - fields - }); + const modifiedData = yield seneca.direct( + "aim:app,on:BasicLed,modify:save", + { + view: name, + data, + fields + } + ); seneca.act("aim:app,on:BasicLed,save:item", { view: name, data: modifiedData }); - }; + }); return /* @__PURE__ */ jsxRuntimeExports.jsx(material.Box, { className: "vxg-BasicEntityEdit", children: item ? /* @__PURE__ */ jsxRuntimeExports.jsxs( "form", { @@ -66090,17 +66176,8 @@ To suppress this warning, you need to explicitly provide the \`palette.${key}Cha return item; }).add("aim:app,on:BasicLed,modify:save", function modify_save(msg) { let item = msg.data; - let fields = msg.fields; if (null == item) return item; item = __spreadValues({}, item); - for (const field of fields) { - if ("Slider" === field.ux.kind) { - console.log("VxgBasicLedPlugin", "modify:save", "field", field); - console.log("VxgBasicLedPlugin", "modify:save", "item", item); - item[field.name] = Number(item[field.name + "_uival$"]) * 60; - } - } - console.log("modify:save", "item", item); return item; }).message( "aim:app,on:BasicLed,edit:item,redux$:true", diff --git a/src/lib/BasicEntityEdit.tsx b/src/lib/BasicEntityEdit.tsx index 003a981..a435d15 100644 --- a/src/lib/BasicEntityEdit.tsx +++ b/src/lib/BasicEntityEdit.tsx @@ -163,14 +163,17 @@ function BasicEntityEdit (props: any) { resolver, }) - const onSubmit = (data: any) => { + const onSubmit = async (data: any) => { console.log('BasicEntityEdit', 'onSubmit', 'data', data) - const modifiedData = seneca.direct('aim:app,on:BasicLed,modify:save', { - view: name, - data, - fields, - }) - // console.log('BasicEntityEdit', 'onSubmit', 'formItem', formItem) + const modifiedData = await seneca.direct( + 'aim:app,on:BasicLed,modify:save', + { + view: name, + data, + fields, + } + ) + // console.log('BasicEntityEdit', 'onSubmit', 'modifiedData', modifiedData) seneca.act('aim:app,on:BasicLed,save:item', { view: name, data: modifiedData, diff --git a/src/lib/VxgBasicEntityEditPlugin.ts b/src/lib/VxgBasicEntityEditPlugin.ts index 2e303f5..200d64f 100644 --- a/src/lib/VxgBasicEntityEditPlugin.ts +++ b/src/lib/VxgBasicEntityEditPlugin.ts @@ -31,78 +31,143 @@ function VxgBasicEntityEditPlugin (this: any, options: any) { for (const field of fields) { if ('Date' === field.ux.kind) { - seneca.add( - 'aim:app,on:BasicLed,modify:edit,view:' + spec.name, - async function modify_edit_Date (this: any, msg: any) { - const out = await this.prior(msg) - - let item = { ...out } - - if (!item[field.name + '_orig$']) { - const dt = util.dateTimeFromUTC(item[field.name]) - item[field.name + '_orig$'] = item[field.name] - item[field.name + '_udm$'] = dt.udm - item[field.name] = dt.localt + seneca + .add( + 'aim:app,on:BasicLed,modify:edit,view:' + spec.name, + async function modify_edit_Date (this: any, msg: any) { + const out = await this.prior(msg) + + let item = { ...out } + + if (!item[field.name + '_orig$']) { + const dt = util.dateTimeFromUTC(item[field.name]) + item[field.name + '_orig$'] = item[field.name] + item[field.name + '_udm$'] = dt.udm + item[field.name] = dt.locald + console.log('modify_edit_Date', item[field.name]) + } + + return item } + ) - return item - } - ) + .add( + 'aim:app,on:BasicLed,modify:save', + { view: spec.name }, + async function modify_save_Date (this: any, msg: any) { + const out = await this.prior(msg) + + let item = { ...out } + + const dt = util.localDateTimeToUTC(item[field.name]) + item[field.name] = dt + + return item + } + ) } else if ('Time' === field.ux.kind) { - seneca.add( - 'aim:app,on:BasicLed,modify:edit,view:' + spec.name, - async function modify_edit_Time (this: any, msg: any) { - const out = await this.prior(msg) - - let item = { ...out } - - if (!item[field.name + '_orig$']) { - const dt = util.dateTimeFromUTC(item[field.name]) - item[field.name + '_orig$'] = item[field.name] - item[field.name + '_udm$'] = dt.udm - item[field.name] = dt.localt + seneca + .add( + 'aim:app,on:BasicLed,modify:edit,view:' + spec.name, + async function modify_edit_Time (this: any, msg: any) { + const out = await this.prior(msg) + + let item = { ...out } + + if (!item[field.name + '_orig$']) { + const dt = util.dateTimeFromUTC(item[field.name]) + item[field.name + '_orig$'] = item[field.name] + item[field.name + '_udm$'] = dt.udm + item[field.name] = dt.localt + console.log('modify_edit_Time', item[field.name]) + } + + return item } + ) + + .add( + 'aim:app,on:BasicLed,modify:save', + { view: spec.name }, + async function modify_save_Time (this: any, msg: any) { + const out = await this.prior(msg) + + let item = { ...out } - return item - } - ) + const dt = util.localTimeToUTC(item[field.name]) + item[field.name] = dt + + return item + } + ) } else if ('DateTime' === field.ux.kind) { - seneca.add( - 'aim:app,on:BasicLed,modify:edit,view:' + spec.name, - async function modify_edit_Datetime (this: any, msg: any) { - const out = await this.prior(msg) - - let item = { ...out } - - if (!item[field.name + '_orig$']) { - const dt = util.dateTimeFromUTC(item[field.name]) - item[field.name + '_orig$'] = item[field.name] - item[field.name + '_udm$'] = dt.udm - item[field.name] = dt.locald + 'T' + dt.localt + seneca + .add( + 'aim:app,on:BasicLed,modify:edit,view:' + spec.name, + async function modify_edit_Datetime (this: any, msg: any) { + const out = await this.prior(msg) + + let item = { ...out } + + if (!item[field.name + '_orig$']) { + const dt = util.dateTimeFromUTC(item[field.name]) + item[field.name + '_orig$'] = item[field.name] + item[field.name + '_udm$'] = dt.udm + item[field.name] = dt.locald + 'T' + dt.localt + } + + return item } + ) + + .add( + 'aim:app,on:BasicLed,modify:save', + { view: spec.name }, + async function modify_save_Datetime (this: any, msg: any) { + const out = await this.prior(msg) - return item - } - ) + let item = { ...out } + + const dt = util.localDateTimeToUTC(item[field.name]) + item[field.name] = dt + + return item + } + ) } else if ('Slider' === field.ux.kind) { // console.log('VxgBasicEntityEditPlugin', 'Slider') - seneca.add( - 'aim:app,on:BasicLed,modify:edit', - { view: spec.name }, - async function modify_edit_Slider (this: any, msg: any) { - const out = await this.prior(msg) + seneca + .add( + 'aim:app,on:BasicLed,modify:edit', + { view: spec.name }, + async function modify_edit_Slider (this: any, msg: any) { + const out = await this.prior(msg) + + let item = { ...out } + + if (!item[field.name + '_orig$']) { + item[field.name + '_orig$'] = item[field.name] + item[field.name] = Number(item[field.name]) / 60 + } + + // return { ...msg, item } + return item + } + ) - let item = { ...out } + .add( + 'aim:app,on:BasicLed,modify:save', + { view: spec.name }, + async function modify_save_Slider (this: any, msg: any) { + const out = await this.prior(msg) - if (!item[field.name + '_orig$']) { - item[field.name + '_orig$'] = item[field.name] - item[field.name] = Number(item[field.name]) / 60 - } + let item = { ...out } + + item[field.name] = Number(item[field.name]) * 60 - // return { ...msg, item } - return item - } - ) + return item + } + ) } } @@ -175,6 +240,52 @@ const util = { return out }, + localTimeToUTC: (timeString: string, tz?: string) => { + tz = tz || Intl.DateTimeFormat().resolvedOptions().timeZone + const now = new Date() + + const [hours, minutes, seconds] = timeString.split(':').map(Number) + + const localDate = new Date( + now.getFullYear(), + now.getMonth(), + now.getDate(), + hours, + minutes, + seconds + ) + + const utcTimestamp = Date.UTC( + localDate.getFullYear(), + localDate.getMonth(), + localDate.getDate(), + localDate.getHours(), + localDate.getMinutes(), + localDate.getSeconds() + ) + + const tzOffset = new Date(utcTimestamp).getTimezoneOffset() * 60000 + + return utcTimestamp - tzOffset + }, + localDateTimeToUTC: (dateOrDateTimeString: string, tz?: string) => { + const date = new Date(dateOrDateTimeString) + + // Check if the date is valid + if (isNaN(date.getTime())) { + throw new Error('Invalid date or datetime string') + } + + // If a timezone is provided, adjust the date + if (tz) { + const tzDate = new Date(date.toLocaleString('en-GB', { timeZone: tz })) + const offset = date.getTime() - tzDate.getTime() + return date.getTime() + offset + } + + // If no timezone is provided, assume the input is already in local time + return date.getTime() + }, } Object.assign(VxgBasicEntityEditPlugin, { diff --git a/src/lib/VxgBasicLedPlugin.ts b/src/lib/VxgBasicLedPlugin.ts index ad8820d..6c84201 100644 --- a/src/lib/VxgBasicLedPlugin.ts +++ b/src/lib/VxgBasicLedPlugin.ts @@ -103,23 +103,11 @@ function VxgBasicLedPlugin (this: any, options: any) { .add('aim:app,on:BasicLed,modify:save', function modify_save (msg: any) { let item = msg.data - let fields = msg.fields if (null == item) return item item = { ...item } - // This code does not belong here - for (const field of fields) { - if ('Slider' === field.ux.kind) { - // console.log('VxgBasicLedPlugin', 'modify:save', 'field', field) - // console.log('VxgBasicLedPlugin', 'modify:save', 'item', item) - item[field.name] = Number(item[field.name]) * 60 - } - } - - console.log('modify:save', 'item', item) - return item })