Last active
April 9, 2019 08:26
-
-
Save arnaudbesnier/9caf53012a33ef22846134926f20eea2 to your computer and use it in GitHub Desktop.
Forest Express Mongoose 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 express = require('express'); | |
const router = express.Router(); | |
const Liana = require('forest-express-mongoose'); | |
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.findById(recordId); | |
const currentShippingStatus = order.shippingStatus; | |
if (STATUS_TRANSITIONS[currentShippingStatus]) { | |
data = STATUS_TRANSITIONS[currentShippingStatus]; | |
} | |
} | |
response.send({ data }); | |
}; | |
router.get('/orders/shippingStatusOptions', Liana.ensureAuthenticated, getShippingStatusOptions); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment