Skip to content

Commit

Permalink
feat(stark-core): include url hash part when landing on site
Browse files Browse the repository at this point in the history
  • Loading branch information
carlo-nomes committed Sep 11, 2019
1 parent f3a4be1 commit ba6985a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,16 @@ describe("Service: StarkRoutingService", () => {
const stateDeclarations: StarkStateConfigWithParams = <StarkStateConfigWithParams>routingService.getStateConfigByUrlPath(url);
expect(stateDeclarations.state).toBe(statesConfig[1]);
});

it("should return the state of the requested url with a hash as parameter", () => {
const statesConfig: StateDeclaration[] = $state.get();
expect(statesConfig.length).toBe(numberOfMockStates);
const url: string = <string>statesConfig[1].url + "#some-hash";

const stateDeclarations: StarkStateConfigWithParams = <StarkStateConfigWithParams>routingService.getStateConfigByUrlPath(url);
expect(stateDeclarations.state).toBe(statesConfig[1]);
expect(stateDeclarations.paramValues["#"]).toBe("some-hash");
});
});

describe("getStateDeclarationByStateName", () => {
Expand Down
29 changes: 15 additions & 14 deletions packages/stark-core/src/modules/routing/services/routing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,17 @@ export class StarkRoutingServiceImpl implements StarkRoutingService {
public getStateConfigByUrlPath(urlPath: string): StarkStateConfigWithParams | undefined {
let targetRoute: StarkStateConfigWithParams | undefined;

// separate path from hash
const [, path = urlPath, hash]: string[] = urlPath.match(/(.*)#(.*)/) || [];

const matchedState: StateDeclaration[] = this.getStatesConfig().filter((state: StateDeclaration) => {
return (<Function>state.$$state)().url && (<Function>state.$$state)().url.exec(urlPath);
return (<Function>state.$$state)().url && (<Function>state.$$state)().url.exec(path, undefined, hash);
});

if (matchedState.length) {
targetRoute = {
state: matchedState[0],
paramValues: (<Function>matchedState[0].$$state)().url.exec(urlPath)
paramValues: (<Function>matchedState[0].$$state)().url.exec(path, undefined, hash)
};
}

Expand Down Expand Up @@ -387,20 +390,18 @@ export class StarkRoutingServiceImpl implements StarkRoutingService {

// provide custom default error handler
// https://ui-router.github.io/ng1/docs/latest/classes/state.stateservice.html#defaulterrorhandler
this.$state.defaultErrorHandler(
(error: any): void => {
let stringError: string;
if (error instanceof Rejection) {
stringError = error.toString();
} else {
stringError = String(error);
}
this.$state.defaultErrorHandler((error: any): void => {
let stringError: string;
if (error instanceof Rejection) {
stringError = error.toString();
} else {
stringError = String(error);
}

if (!this.knownRejectionCausesRegex.test(stringError)) {
this.defaultErrorHandler.handleError(Error(stringError));
}
if (!this.knownRejectionCausesRegex.test(stringError)) {
this.defaultErrorHandler.handleError(Error(stringError));
}
);
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/stark-core/src/modules/session/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function resolveTargetRoute(
}

// get the path of the current URL in the browser's navigation bar
const targetUrlPath: string = $location.path();
const targetUrlPath: string = $location.path(true);
const targetRoute: StarkStateConfigWithParams | undefined = getTargetStateByUrl(targetUrlPath);
let finalTargetRoute$: Observable<StarkStateConfigWithParams | undefined> = of(targetRoute);

Expand Down

0 comments on commit ba6985a

Please sign in to comment.