Last active
April 9, 2019 08:01
-
-
Save arnaudbesnier/8d285feca7cd9a7cf9072b98d0484428 to your computer and use it in GitHub Desktop.
Forest Express Sequelize Dropdown Dynamic Options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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