Fluxible-router redirect form one route to another

132 views Asked by At

We have two routes in fluxible-router. I need that if legacyDek route is accessed, then after an action completed, it would redirect to the deck route.

deck: {
        path: '/deck/:id(\\d+|\\d+-\\d+)/:stype?/:sid?/:spath?/:mode?/:theme?',
        method: 'get',
        page: 'deck',
        handler: require('../components/Deck/Deck'),
        action: (context, payload, done) => {
            async.series([
                (callback) => {
                    context.executeAction(loadDeck, payload, callback);
                },
                (callback) => {
                    context.executeAction(loadPresentation, payload, callback);
                }
            ],
            (err) => {
                if(err) console.log(err);
                done();
            });
        }
    },
legacydeck: {
        path: '/deck/:oldid(\\d+_\\w+.*)',
        method: 'get',
        action: (context, payload, done) => {
            context.executeAction(loadLegacy, payload, (err, result) => {
                if (err) console.log(err);
                context.executeAction(navigateAction, {'url' : '/deck/' +result}, done);
            });
        }
    },

The redirect itself does work properly, however the url line stays the same. Can someone explain - why is this and how to do the redirect completely?

1

There are 1 answers

0
vassilsha On

For now the only solution is the following: 1. Change the action which triggers the redirect in such a manner, that it returns an error object, in my example:

export default function loadLegacy(context, payload, done) {
    log.info(context);
    context.service.read('deck.legacy', {'oldid': parseInt(payload.params.oldid)}, {timeout: 20 * 1000}, (err, res) => {
      //  console.log('Executing loadPresentation action');
        if (err) {
            log.error(context, {filepath: __filename});
            context.executeAction(serviceUnavailable, payload, done);
            //context.dispatch('LOAD_FEATURED_FAILURE', err);
        } else {
            done({'statusCode':'301','redirectURL': '/deck/' + res.new_id});
        }
    });
}

  1. Change the server.js file, adding there lines to capture '301' errors.

context.getActionContext().executeAction(navigateAction, {url: req.url, reqId: req.reqId}, 
  (err) => {
      if (err) {         
          if (err.statusCode && err.statusCode === '301') {  //handling redirect             
              res.redirect(err.redirectURL);
          }else{ //handling other errors          
          ...
          }
      }else{  //usual rendering     
      ...
      }
})

  1. The route is now looking like the following:

legacydeck: {
        path: '/deck/:oldid(\\d+_\\w+.*)',
        method: 'get',
        action: (context, payload, done) => {
            context.executeAction(loadLegacy, payload, done);
        }
    },