简体   繁体   中英

Redux Saga Redirect to Wordpress Page

I have a React App as a submodule in a Wordpress repo. The React App is responsible for inventory and all its pages while Wordpress is responsible for all other pages.

I have a saga function that fetches inventory details. If there's no response, I want to redirect it to another page. yield put(push('/sales') . The problem is that this page exists on Wordpress, not my React app. So when this is executed, it tries to find a route in react, doesn't find it, and goes to a 404 page. When I refresh, however, it loads the Wordpress page. I tried adding an absolute URL inside yield put(push(URL)) , but it sent me on an infinite redirect loop. I was wondering if it was possible to just redirect to a Wordpress Page in redux saga.

function* requestInventoryDetails(action) {
  try {
    if (response && response.stockNumber) {
      .
      .
      .
    } else {
      yield put(push('/sales'))
      // window.location.reload()
    }
  } catch (err) {
    yield put(SET_ERROR('There was an error retrieving the inventory details'))
    throw err
  }
}

If you're navigating outside of the react app perhaps do window.location.assign(absoluteUrl); ? Your normal redux router doesn't need to be involved. If you want to keep things testable you could put that in a separate function so it could be mocked in your saga tests.

function navigateToExternalPage(url) {
    window.location.assign(url);
}

function* requestInventoryDetails(action) {
  try {
    if (response && response.stockNumber) {
      .
      .
      .
    } else {
      yield call(navigateToExternalPage, externalUrl);
    }
  } catch (err) {
    yield put(SET_ERROR('There was an error retrieving the inventory details'))
    throw err
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM