refactor: rename names to routeNames for clarity

This commit is contained in:
satyajit.happy
2019-07-14 14:57:02 +02:00
parent 86890b2b33
commit 43bc406c00
4 changed files with 22 additions and 22 deletions

View File

@@ -76,10 +76,10 @@ const StackRouter: Router<CommonAction | Action> = {
};
}
if (state.names === undefined || state.key === undefined) {
if (state.routeNames === undefined || state.key === undefined) {
state = {
...state,
names: state.names || routeNames,
routeNames: state.routeNames || routeNames,
key: state.key || `stack-${shortid()}`,
};
}
@@ -90,7 +90,7 @@ const StackRouter: Router<CommonAction | Action> = {
getStateForAction(state, action) {
switch (action.type) {
case 'PUSH':
if (state.names.includes(action.payload.name)) {
if (state.routeNames.includes(action.payload.name)) {
return {
...state,
index: state.index + 1,
@@ -128,7 +128,7 @@ const StackRouter: Router<CommonAction | Action> = {
});
case 'NAVIGATE':
if (state.names.includes(action.payload.name)) {
if (state.routeNames.includes(action.payload.name)) {
// If the route already exists, navigate to that
let index = -1;
@@ -199,7 +199,7 @@ const StackRouter: Router<CommonAction | Action> = {
return {
...action.payload,
key: state.key,
names: state.names,
routeNames: state.routeNames,
};
}

View File

@@ -60,10 +60,10 @@ const TabRouter: Router<Action | CommonAction> = {
};
}
if (state.names === undefined || state.key === undefined) {
if (state.routeNames === undefined || state.key === undefined) {
state = {
...state,
names: state.names || routeNames,
routeNames: state.routeNames || routeNames,
key: state.key || `tab-${shortid()}`,
};
}
@@ -75,7 +75,7 @@ const TabRouter: Router<Action | CommonAction> = {
switch (action.type) {
case 'JUMP_TO':
case 'NAVIGATE':
if (state.names.includes(action.payload.name)) {
if (state.routeNames.includes(action.payload.name)) {
const index = state.routes.findIndex(
route => route.name === action.payload.name
);
@@ -125,7 +125,7 @@ const TabRouter: Router<Action | CommonAction> = {
return {
...action.payload,
key: state.key,
names: state.names,
routeNames: state.routeNames,
};
}

View File

@@ -24,10 +24,10 @@ const MockRouter: Router<{ type: string }> = {
};
}
if (state.names === undefined || state.key === undefined) {
if (state.routeNames === undefined || state.key === undefined) {
state = {
...state,
names: state.names || routeNames,
routeNames: state.routeNames || routeNames,
key: state.key || 'root',
};
}
@@ -95,7 +95,7 @@ it('initializes state for a navigator on navigation', () => {
expect(onStateChange).toBeCalledWith({
index: 0,
key: 'root',
names: ['foo', 'bar', 'baz'],
routeNames: ['foo', 'bar', 'baz'],
routes: [
{ key: 'foo', name: 'foo' },
{ key: 'bar', name: 'bar' },
@@ -148,7 +148,7 @@ it('initializes state for nested navigator on navigation', () => {
expect(onStateChange).toBeCalledWith({
index: 2,
key: 'root',
names: ['foo', 'bar', 'baz'],
routeNames: ['foo', 'bar', 'baz'],
routes: [
{ key: 'foo', name: 'foo' },
{ key: 'bar', name: 'bar' },
@@ -158,7 +158,7 @@ it('initializes state for nested navigator on navigation', () => {
state: {
index: 0,
key: 'root',
names: ['qux'],
routeNames: ['qux'],
routes: [{ key: 'qux', name: 'qux' }],
},
},
@@ -308,7 +308,7 @@ it("lets parent handle the action if child didn't", () => {
expect(onStateChange).toBeCalledWith({
index: 2,
key: 'root',
names: ['foo', 'bar', 'baz'],
routeNames: ['foo', 'bar', 'baz'],
routes: [
{ key: 'baz', name: 'baz' },
{ key: 'bar', name: 'bar' },
@@ -360,7 +360,7 @@ it('allows arbitrary state updates by dispatching a function', () => {
expect(onStateChange).toBeCalledWith({
index: 1,
key: 'root',
names: ['foo', 'bar'],
routeNames: ['foo', 'bar'],
routes: [{ key: 'bar', name: 'bar' }, { key: 'foo', name: 'foo' }],
});
});
@@ -403,7 +403,7 @@ it('updates route params with setParams', () => {
expect(onStateChange).lastCalledWith({
index: 0,
key: 'root',
names: ['foo', 'bar'],
routeNames: ['foo', 'bar'],
routes: [
{ key: 'foo', name: 'foo', params: { username: 'alice' } },
{ key: 'bar', name: 'bar' },
@@ -416,7 +416,7 @@ it('updates route params with setParams', () => {
expect(onStateChange).lastCalledWith({
index: 0,
key: 'root',
names: ['foo', 'bar'],
routeNames: ['foo', 'bar'],
routes: [
{ key: 'foo', name: 'foo', params: { username: 'alice', age: 25 } },
{ key: 'bar', name: 'bar' },

View File

@@ -12,18 +12,18 @@ export type NavigationState = {
*/
index: number;
/**
* List if valid route names.
* List if valid route names as defined in the screen components.
*/
names: string[];
routeNames: string[];
/**
* List of rendered routes.
*/
routes: Array<Route & { state?: NavigationState }>;
};
export type InitialState = Omit<Omit<NavigationState, 'names'>, 'key'> & {
export type InitialState = Omit<Omit<NavigationState, 'routeNames'>, 'key'> & {
key?: undefined;
names?: undefined;
routeNames?: undefined;
state?: InitialState;
};