Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arnaudbesnier/8d285feca7cd9a7cf9072b98d0484428 to your computer and use it in GitHub Desktop.
Save arnaudbesnier/8d285feca7cd9a7cf9072b98d0484428 to your computer and use it in GitHub Desktop.
Forest Express Sequelize Dropdown Dynamic Options
// Step 1: Create the file below
// Step 2: Configure the dropdown in dynamic mode using this path: /forest/orders/shippingStatusOptions
// decorators/routes/orders.js
const Liana = require('forest-express-sequelize');
const models = require('../../models');
const STATUS_OPTION_DEFAULT = 'Being processed';
const STATUS_TRANSITIONS = {
'Being processed': ['Ready for shipping'],
'Ready for shipping': ['In transit'],
'In transit': ['Shipped', 'Failed'],
};
const getShippingStatusOptions = async (request, response) => {
const { context } = request.query;
const recordId = context && context.record && context.record.id;
let data = [STATUS_OPTION_DEFAULT];
if (recordId) {
const order = await models.order.findByPk(recordId);
const currentShippingStatus = order.shippingStatus;
if (STATUS_TRANSITIONS[currentShippingStatus]) {
data = STATUS_TRANSITIONS[currentShippingStatus];
}
}
response.send({ data });
};
module.exports = (app) => {
app.get('/orders/shippingStatusOptions', Liana.ensureAuthenticated, getShippingStatusOptions);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment