@Guillaume_Cisco Thanks for this nice suggestion. I have tried to implement as you explained.
actions: [{
name: 'Promocode Send',
type: 'bulk',
fields: [{
field: 'promocodes',
type: 'Enum',
enums: []
}],
hooks: {
load: async ({ fields, request }) => {
console.log('>>>>>>>>>>>LOAD HOOKS>>>>>>>>>>>>');
const promocodesField = fields.find(field => field.field === 'promocodes');
const promocodesDetails = await promocodes.findAll({ attributes :['id', 'name']});
console.log(">>>>>>>>>>>>>PROMOCODE DETAILS>>>>>>>>>>>>>>>>.");
console.log(promocodesDetails);
promocodesField.enums = promocodesDetails;
return fields;
}
}
}],
I have implemented here. It won’t console even first line.
I have tried to refresh whole page, re-run the project and all. When this hook will load.
Here is output. It seems empty.
Moreover, In the documentation provide sample code. I am confused with first line. What should I write in the my-own-helper file?
const { getEnumsFromDatabaseForThisRecord } = require('./my-own-helper');
const { getZipCodeFromCity } = require('...');
const { collection } = require('forest-express-sequelize');
const { customers } = require('../models');
collection('customers', {
actions: [{
name: 'Send invoice',
type: 'single',
fields: [
{
field: 'country',
type: 'Enum',
enums: []
},
{
field: 'city',
type: 'String',
hook: 'onCityChange'
},
{
field: 'zip code',
type: 'String',
hook: 'onZipCodeChange'
},
],
hooks: {
load: async ({ fields, request }) => {
const country = fields.find(field => field.field === 'country');
const id = request.body.data.attributes.ids[0];
const customer = await customers.findByPk(id);
country.enums = getEnumsFromDatabaseForThisRecord(customer);
return fields;
},
change: {
onCityChange: async ({ fields, request, changedField }) => {
const zipCode = fields.find(field => field.field === 'zip code');
const id = request.body.data.attributes.ids[0];
const customer = await customers.findByPk(id);
zipCode.value = getZipCodeFromCity(
customer,
changedField.value
);
return fields;
},
onZipCodeChange: async ({ fields, request, changedField }) => {
const city = fields.find(field => field.field === 'city');
const id = request.body.data.attributes.ids[0];
const customer = await customers.findByPk(id);
city.value = getCityFromZipCode(
customer,
changedField.value
);
return fields;
},
},
},
}],
fields: [],
segments: [],
});
Please suggest me. Is the way I used perfect or not?