Conditional Smart Action

Hey guys,

I would like to implement a smart action that is similar to the one you have in your documentation: Create and manage Smart Actions - Documentation

However, I would like to make it as “conditional smart action” where i could either set it as “alive” or the reverse, set it as “dead”.

The thing is, if my item is marked as “alived”, i should then have a smart action with “Mark as Dead”, and so the reverse when this one isn’t alived :slight_smile:

As “possible” example (using the doc mentioned above):

const { collection } = require('forest-express-mongoose');

collection('companies', {
  actions: [{ 
    name: item.IsAlived ? "Mark as Dead" : "Mark as Alive"
  }],
});

Otherwise, if this isn’t possible, could I at least have action depending on a value? so I either put an action or the other? As of today, i have the following:

actions: [
        {
            name: 'Mark as alive',
            endpoint: '/forest/item/markAsAlive',
            httpMethod: 'POST'
        },
        {
            name: 'Mark as dead',
            endpoint: '/forest/item/markAsDead',
            httpMethod: 'POST'
        },
    ]

I hope I am clear :slight_smile: I will be checking this topic time to time, feel free to ask more details :slight_smile: best!

Max

Hello @Emixam23 and welcome to the Forest Admin community,

Action names are static and not customisable for a specific item, sorry about that. I’ll share your input with our team and see if we can have it later.

For now, you could change your action name to “Switch status”, detect the proper value to set within the action code, and send a custom success response text to ensure the new status is the wanted one.

You can also try to have your action open a form and pre fill it with the next expected status, but that my be a bit more work.

I’ll share you feedback with the team to see if we can have this added in the future.

Hey :slight_smile:

I so created two actions, however, they don’t seem to appear for some reason(s):

forest/items.js

...
  actions: [{
    name: 'Mark as alive',
    endpoint: '/forest/actions/items/mark-as-alive',
    httpMethod: 'POST',
    type: 'global'
  }, {
    name: 'Mark as dead',
    endpoint: '/forest/actions/items/mark-as-dead',
    httpMethod: 'POST',
    type: 'global'
  }],
...

routes/items.js

router.post('/forest/actions/items/mark-as-alive', permissionMiddlewareCreator.smartAction(), (req, res) => {
  return new RecordsGetter(items).getIdsFromRequest(req)
      .then((itemIds) => {
        return items
            .update({ is_alive: true, updated_at: Date.now() }, { where: { id: itemIds }})
            .then(() => {
              res.send({ success: 'item marked as alive!' });
            });
      });
});

router.post('/forest/actions/items/mark-as-dead', permissionMiddlewareCreator.smartAction(), (req, res) => {
  return new RecordsGetter(items).getIdsFromRequest(req)
      .then((itemIds) => {
        return items
            .update({ is_alive: false, updated_at: 0 }, { where: { id: itemIds }})
            .then(() => {
              res.send({ success: 'item marked as dead.' });
            });
      });
});

It should be working but yep, nothing :confused: I tried to regenerate the .forestadmin-schema.json and it’s inside, it just doesn’t appear on screen :slight_smile:

Thanks

1 Like

Hi @Emixam23 :wave:t3:

Did you try to view the Smart Actions tab in your collection settings and see if the actions are set as visible there?