Skip to content

Commit

Permalink
Simplify returned value from modify:edit actions
Browse files Browse the repository at this point in the history
  • Loading branch information
zackatbrightbeam committed Aug 2, 2024
1 parent 25358fe commit a749efc
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 218 deletions.
164 changes: 86 additions & 78 deletions dist/voxgig-model-react.es.js

Large diffs are not rendered by default.

164 changes: 86 additions & 78 deletions dist/voxgig-model-react.umd.js

Large diffs are not rendered by default.

22 changes: 6 additions & 16 deletions src/lib/BasicEntityEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,18 @@ function BasicEntityEdit (props: any) {

useEffect(() => {
const fetchData = async () => {
console.log(
'BasicEntityEdit',
'useEffect',
'modify:edit',
'init',
'view',
name,
'item',
item?.title
)
console.log('BEE', 'effect', 'mod:edit', 'init', 'view', name)
console.log('BEE', 'effect', 'mod:edit', 'init', 'item', item?.title)
if (item && name) {
// console.log('BasicEntityEdit', 'useEffect', 'modify:edit', 'init')
const res = await seneca.direct('aim:app,on:BasicLed,modify:edit', {
// console.log('BEE', 'effect', 'modify:edit', 'init')
item = await seneca.direct('aim:app,on:BasicLed,modify:edit', {
view: name,
item,
fields,
})
item = res.item
// console.log('BasicEntityEdit', 'useEffect', 'modify:edit', 'got-item')
// console.log('BasicEntityEdit', 'useEffect', 'resetting')
console.log('BEE', 'effect', 'resetting', item)
reset(item)
}
reset(item)
}

fetchData()
Expand Down
49 changes: 26 additions & 23 deletions src/lib/BasicLed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Box } from '@mui/material'

import { Gubu } from 'gubu'


import type { BasicProps, Spec } from './basic-types'

import { BasicEntityList } from './BasicEntityList'
Expand All @@ -16,50 +15,54 @@ import { BasicLoading } from './BasicLoading'

import { VxgBasicLedPlugin } from './VxgBasicLedPlugin'


const CMPNAME = 'BasicLed'


// BasicLed renders a list of entities (with BasicList) or a form to edit them (with BasicEdit)
function BasicLed (props: any) {
const { ctx } = props
const { seneca } = ctx()
const name = props.spec.name
// TODO: const cid = seneca.util.nid() -
// tag = props.spec.name +

const navigate = useNavigate()
const led = useSelector((state:any)=>state.main.view[name])

const led = useSelector((state: any) => state.main.view[name])
const ready = true === led?.ready
if(!ready) {

if (!ready) {
seneca.use({
tag: name,
define: VxgBasicLedPlugin,
options:{
options: {
spec: props.spec,
navigate,
}
},
})
}

const { head, list, edit, foot } = seneca.export('VxgBasicLedPlugin$'+name+'/spec') || {}
const { head, list, edit, foot } =
seneca.export('VxgBasicLedPlugin$' + name + '/spec') || {}

return (
ready ?
<Box className="vxg-BasicLed">
{ head.active && <BasicLedHead ctx={ctx} spec={head} /> }
return ready ? (
<Box className='vxg-BasicLed'>
{head.active && <BasicLedHead ctx={ctx} spec={head} />}
<Routes>
<Route path="/" element={<BasicEntityList ctx={ctx} spec={list} />} />
<Route path="/edit/:item" element={<BasicEntityEdit ctx={ctx} spec={edit} />} />
<Route path="/add" element={<BasicEntityEdit ctx={ctx} spec={edit} />} />
<Route path='/' element={<BasicEntityList ctx={ctx} spec={list} />} />
<Route
path='/edit/:item'
element={<BasicEntityEdit ctx={ctx} spec={edit} />}
/>
<Route
path='/add'
element={<BasicEntityEdit ctx={ctx} spec={edit} />}
/>
</Routes>
{ foot.active && <BasicLedFoot ctx={ctx} spec={foot} /> }
{foot.active && <BasicLedFoot ctx={ctx} spec={foot} />}
</Box>
:
<BasicLoading />
) : (
<BasicLoading />
)
}

export {
BasicLed
}
export { BasicLed }
28 changes: 11 additions & 17 deletions src/lib/VxgBasicEntityEditPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ function VxgBasicEntityEditPlugin (this: any, options: any) {
async function modify_edit_Date (this: any, msg: any) {
const out = await this.prior(msg)

let { item } = out

item = { ...item }
let item = { ...out }

if (!item[field.name + '_orig$']) {
const dt = util.dateTimeFromUTC(item[field.name])
Expand All @@ -47,7 +45,7 @@ function VxgBasicEntityEditPlugin (this: any, options: any) {
item[field.name] = dt.localt
}

return { ...msg, item }
return item
}
)
} else if ('Time' === field.ux.kind) {
Expand All @@ -56,9 +54,7 @@ function VxgBasicEntityEditPlugin (this: any, options: any) {
async function modify_edit_Time (this: any, msg: any) {
const out = await this.prior(msg)

let { item } = out

item = { ...item }
let item = { ...out }

if (!item[field.name + '_orig$']) {
const dt = util.dateTimeFromUTC(item[field.name])
Expand All @@ -67,7 +63,7 @@ function VxgBasicEntityEditPlugin (this: any, options: any) {
item[field.name] = dt.localt
}

return { ...msg, item }
return item
}
)
} else if ('DateTime' === field.ux.kind) {
Expand All @@ -76,9 +72,7 @@ function VxgBasicEntityEditPlugin (this: any, options: any) {
async function modify_edit_Datetime (this: any, msg: any) {
const out = await this.prior(msg)

let { item } = out

item = { ...item }
let item = { ...out }

if (!item[field.name + '_orig$']) {
const dt = util.dateTimeFromUTC(item[field.name])
Expand All @@ -87,26 +81,26 @@ function VxgBasicEntityEditPlugin (this: any, options: any) {
item[field.name] = dt.locald + 'T' + dt.localt
}

return { ...msg, item }
return item
}
)
} else if ('Slider' === field.ux.kind) {
// console.log('VxgBasicEntityEditPlugin', 'Slider')
seneca.add(
'aim:app,on:BasicLed,modify:edit,view:' + spec.name,
'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

item = { ...item }
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 { ...msg, item }
return item
}
)
}
Expand Down
10 changes: 4 additions & 6 deletions src/lib/VxgBasicLedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function VxgBasicLedPlugin (this: any, options: any) {

item = { ...item }

return { ...msg, item }
return item
})

.add('aim:app,on:BasicLed,modify:save', function modify_save (msg: any) {
Expand All @@ -112,8 +112,8 @@ function VxgBasicLedPlugin (this: any, options: any) {
// 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)
// console.log('VxgBasicLedPlugin', 'modify:save', 'field', field)
// console.log('VxgBasicLedPlugin', 'modify:save', 'item', item)
item[field.name] = Number(item[field.name]) * 60
}
}
Expand Down Expand Up @@ -160,9 +160,7 @@ function VxgBasicLedPlugin (this: any, options: any) {
.reduce((a: any, n: any[]) => ((a[n[0]] = msg.data[n[0]]), a), {})

const item = await seneca.entity(entCanon).save$(data)
// TODO: navigate to edit view
// navigate('/view/' + name + '/edit/' + item.id)
navigate('/view/' + name)
navigate('/view/' + name + '/edit/' + item.id)
}
)

Expand Down

0 comments on commit a749efc

Please sign in to comment.