How would you redirect when something isn't present in the DB?

I’m wondering how to do this the “redwood” way:

A user enters the app on a Today page, but if there is no “Today” ready for that given user I want to redirect them to a page where they can create the “Today”.

I’m currently loading a “TodayCell” on the “TodayPage”, should I chuck the navigate in the empty block of that cell? Or would it maybe be better to actually just perform the GraphQL call on the TodayPage itself?

I suppose that when there is no “Today”, your TodayCell will render export const Empty = () => <div>Empty</div>. If so, you can simply replace <div>Empty</div> by <Redirect to={routes.createTodayPage()} />

Don’t forget to import { Redirect, routes } from '@redwoodjs/router' at the top of your cell.

3 Likes

EDIT : @simoncrypta beat me to it ^^
Maybe render a <Redirect> in your empty case

1 Like

:see_no_evil: I came across a couple of time, but somehow my brain had tricked me to think it was just to be used in the Router file…

<Redirect> is definitly a better solution then what I had:

export const Empty = () => {
  React.useEffect(() => {
    navigate(routes.newDay())
  })
  return null
}
1 Like

Did you see the <Redirect> implementation?

const Redirect = ({ to }: RedirectProps) => {
  useEffect(() => navigate(to), [to])
  return null
}

:rofl:

:see_no_evil: Well at least my solution wasn’t as weird as I thought it was haha