Skip to content

Instantly share code, notes, and snippets.

@uqmessias
Last active March 27, 2019 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uqmessias/254f48684aa60635b8a3aeba6a3f640b to your computer and use it in GitHub Desktop.
Save uqmessias/254f48684aa60635b8a3aeba6a3f640b to your computer and use it in GitHub Desktop.
Function to remove an single route from the navigation.route of react-navigation module
const removeAtRouteName = (state, routeName) => {
if (!routeName || !state.routes || !state.routes.length) {
return state;
}
const newState = { ...state };
let removed = false;
state.routes.forEach((currentRoute, index) => {
if (currentRoute.routeName === routeName) {
if (index <= newState.index) {
newState.index = index - 1;
}
const newRoutes = newState.routes.slice();
newRoutes.splice(index, 1);
newState.routes = newRoutes;
removed = true;
} else if (currentRoute.routes) {
const newRoute = removeAtRouteName(currentRoute, routeName);
if (newRoute !== currentRoute) {
const newRoutes = newState.routes.slice();
newRoutes[index] = newRoute;
newState.routes = newRoutes;
removed = true;
}
}
});
if (removed) {
return newState;
}
return state;
};
const compareTwo = (route1, route2) => {
if (route1 && route2) {
if (route1.index !== route2.index ||
route1.routeName !== route2.routeName ||
route1.key !== route2.key) {
console.log(`route1(${JSON.stringify(route1)})\n\nroute2(${JSON.stringify(route2)})`)
return false
}
if (route1.routes !== undefined && route1.routes !== undefined) {
return route1.routes.every((r1, index) => compareTwo(r1, route2.routes[index]))
}
return true
}
return false
}
const route = {
index: 1,
routes: [{
index: 1,
routes: [{
index: 0,
routes: [{
routeName: 'LoginOTPRequest',
key: 'Init-id-1508190306049-8',
},
{
routeName: 'LoginOTPCode',
key: 'Init-id-1508188871032-9',
}],
routeName: 'LoginNavigation',
key: 'Init-id-1508190306049-9',
},
{
key: 'id-1508190306049-12',
routeName: 'Menu',
}],
routeName: 'ScreenNavigation',
key: 'Init-id-1508190306049-11',
},
{
key: 'id-1508188871032-14',
routeName: 'Embedded_Webview',
}],
}
const routeWithOutLoginOTPRequest = {
index: 1,
routes: [{
index: 1,
routes: [{
index: 0,
routes: [{
routeName: 'LoginOTPCode',
key: 'Init-id-1508188871032-9',
}],
routeName: 'LoginNavigation',
key: 'Init-id-1508190306049-9',
},
{
key: 'id-1508190306049-12',
routeName: 'Menu',
}],
routeName: 'ScreenNavigation',
key: 'Init-id-1508190306049-11',
},
{
key: 'id-1508188871032-14',
routeName: 'Embedded_Webview',
}],
}
const routeWithOutLoginOTPCode = {
index: 1,
routes: [{
index: 1,
routes: [{
index: 0,
routes: [{
routeName: 'LoginOTPRequest',
key: 'Init-id-1508190306049-8',
}],
routeName: 'LoginNavigation',
key: 'Init-id-1508190306049-9',
},
{
key: 'id-1508190306049-12',
routeName: 'Menu',
}],
routeName: 'ScreenNavigation',
key: 'Init-id-1508190306049-11',
},
{
key: 'id-1508188871032-14',
routeName: 'Embedded_Webview',
}],
}
const routeWithOutLoginNavigation = {
index: 1,
routes: [{
index: 0,
routes: [{
key: 'id-1508190306049-12',
routeName: 'Menu',
}],
routeName: 'ScreenNavigation',
key: 'Init-id-1508190306049-11',
},
{
key: 'id-1508188871032-14',
routeName: 'Embedded_Webview',
}],
}
const routeWithOutMenu = {
index: 1,
routes: [{
index: 0,
routes: [{
index: 0,
routes: [{
routeName: 'LoginOTPRequest',
key: 'Init-id-1508190306049-8',
},
{
routeName: 'LoginOTPCode',
key: 'Init-id-1508188871032-9',
}],
routeName: 'LoginNavigation',
key: 'Init-id-1508190306049-9',
}],
routeName: 'ScreenNavigation',
key: 'Init-id-1508190306049-11',
},
{
key: 'id-1508188871032-14',
routeName: 'Embedded_Webview',
}],
}
const routeWithOutScreenNavigation = {
index: 0,
routes: [{
key: 'id-1508188871032-14',
routeName: 'Embedded_Webview',
}],
}
const routeWithOutEmbedded_Webview = {
index: 0,
routes: [{
index: 1,
routes: [{
index: 0,
routes: [{
routeName: 'LoginOTPRequest',
key: 'Init-id-1508190306049-8',
},
{
routeName: 'LoginOTPCode',
key: 'Init-id-1508188871032-9',
}],
routeName: 'LoginNavigation',
key: 'Init-id-1508190306049-9',
},
{
key: 'id-1508190306049-12',
routeName: 'Menu',
}],
routeName: 'ScreenNavigation',
key: 'Init-id-1508190306049-11',
}],
}
console.log(`comparewith LoginOTPRequest -> ${compareTwo(routeWithOutLoginOTPRequest, removeAtRouteName(route, 'LoginOTPRequest'))}\n\n\n\n`)
console.log(`comparewith LoginOTPCode -> ${compareTwo(routeWithOutLoginOTPCode, removeAtRouteName(route, 'LoginOTPCode'))}\n\n\n\n`)
console.log(`comparewith LoginNavigation -> ${compareTwo(routeWithOutLoginNavigation, removeAtRouteName(route, 'LoginNavigation'))}\n\n\n\n`)
console.log(`comparewith Menu -> ${compareTwo(routeWithOutMenu, removeAtRouteName(route, 'Menu'))}\n\n\n\n`)
console.log(`comparewith ScreenNavigation -> ${compareTwo(routeWithOutScreenNavigation, removeAtRouteName(route, 'ScreenNavigation'))}\n\n\n\n`)
console.log(`comparewith Embedded_Webview -> ${compareTwo(routeWithOutEmbedded_Webview, removeAtRouteName(route, 'Embedded_Webview'))}\n\n\n\n`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment