Hybrid
Il flow ibrido permette di utilizzare flower contemporaneamente sul frontend e sul backend (NODEJS).
Example Lambda/Atlas function
exports = function(nodeId, flow, value = {}) {
const CryptoJS = require('crypto-js');
const FlowerJs = require('@stackhouseos/flower-client-nodejs').default;
const bytes = CryptoJS.AES.decrypt(flow, 'PASSWORD');
const originalText = bytes.toString(CryptoJS.enc.Utf8);
const elements = JSON.parse(originalText);
const {user} = context
const {role, permission} = user.custom_data || {}
const components = {
save: async (props, done) => {
const result = await context.functions.execute('save', props.data.value)
done(result)
},
get: async(props, done) =>{
const result = await context.functions.execute('getProjectData', props.data.value)
done(result)
}
}
return FlowerJs({
startId: nodeId,
components,
elements,
initialData: {value, user: { role,permission }},
})
};
Example Component Portal with fetch call
export function Portal({ nodeId, flowName, dataIn = {}, onChange }) {
const dispatch = useDispatch()
useEffect(() => {
fetch('/api/flower', {
method: 'POST',
body: JSON.stringify({
flow: flow.elements,
nodeId, // nodo di partenza
value: dataIn // valori iniziali
})
})
.then((data) => data.json())
.then((data) => {
dispatch({
type: 'flower/node',
payload: { flowName, node: data.nodeId, value: data }
})
})
}, [dispatch, nodeId, flowName, dataIn, onChange])
return (
<div>
<div className="loader" />
</div>
)
}
Testing flow
const FlowerJs = require('@stackhouseos/flower-client-nodejs').default;
import crud from './crud-test-flower.json';
const components = {
error: (props, done) => {
done({
error: 'error'
})
},
delete: (props, done) => {
done({
delete: 1
})
},
read: (props, done) => {
done({
ok: true
})
},
update: (props, done) => {
done({
update: 1
})
},
};
describe('Testing Crud', () => {
it('Test crud read admin', async () => {
expect.assertions(1);
const state = {
role: 'admin',
action: 'read',
}
const res = await FlowerJs({
components,
elements: crud.elements,
initialData: state,
})
expect({
data: {
ok: true
},
nodeId: 'NODE3',
history: [
'START',
'NODE1',
'NODE2',
'NODE3',
]
}).toEqual(res)
})
})