Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL redirect && dispatch to return promise #23

Closed
zhming0 opened this issue Mar 10, 2016 · 7 comments
Closed

URL redirect && dispatch to return promise #23

zhming0 opened this issue Mar 10, 2016 · 7 comments

Comments

@zhming0
Copy link

zhming0 commented Mar 10, 2016

I am trying to think of a way to do redirection.
Here are some examples, of which one requires dispatch to return promise.

// Redirect / to /home
{on("/", async() => {
     const {state, component} = await this.dispatch("/home");
     return component;
})}

Or provide a new api redirect (same level as on)?

redirct("/", "/home");

Apparently second one is better. Maybe adding this will be good?

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/31733774-url-redirect-dispatch-to-return-promise?utm_campaign=plugin&utm_content=tracker%2F18115217&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F18115217&utm_medium=issues&utm_source=github).
@Palgie
Copy link

Palgie commented Mar 14, 2016

I'd prefer API if there is no loss of scope when used alongside conditionals.

@koistya
Copy link
Member

koistya commented Mar 14, 2016

What do you think about this API? Where a route with redirect looks like this:

const router = new Router()
  .route('/', ({ redirect }) => redirect('/home'));

Another example, when you need to do some async stuff before doing a redirect:

const router = new Router()
  .router('/p/:id', async action({ redirect }, { id }) {
    const url = `/graphql?query={post(id:"${id}"){slug,author{username}}}`;
    const resp = await fetch(url, { method: 'post' });
    const { data } = await resp.json();
    if (data.post) {
      redirect(`/${data.post.author.username}/${data.post.slug}`);
    }
  });

@Palgie
Copy link

Palgie commented Mar 14, 2016

@koistya Yep - That would work for my use case!

@koistya
Copy link
Member

koistya commented Mar 25, 2016

@zhming0 yep, adding router.redirect(fromPath, toPath) method for configuring static redirects would be nice. PR is welcome!

@v3yr0n
Copy link

v3yr0n commented Jun 8, 2016

Was this done? If yes, how can i use it? :)

@JeongtaekLim
Copy link

JeongtaekLim commented Mar 15, 2017

Thanks @koistya for pretty simple example. I've tried to follow as above.

import {resolve} from 'universal-router';
import Router from 'universal-router';

....

request.post({
  url: 'http://localhost:8000/account/create/',
  form
}, (err, res, body) => {
  const router = new Router().route('/', ({redirect}) => redirect('/login'));
  }
});

but I got an error about webpack.

TypeError: __WEBPACK_IMPORTED_MODULE_4_universal_router___default.a is not a constructor

I'm using react-starter-kit-react-redux.
What I want to do is just change the user registration page into login page.
Did I misuse universal-router?

@frenzzy frenzzy mentioned this issue Mar 26, 2017
20 tasks
@frenzzy
Copy link
Member

frenzzy commented Oct 8, 2017

@JeongtaekLim answer regarding your error: #127 (comment)

Docs about redirects added. Closing.

@frenzzy frenzzy closed this as completed Oct 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants