Skip to content

Commit 7bafd62

Browse files
Add top level app states
1 parent 741117e commit 7bafd62

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

src/app/app.module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { FormsModule } from '@angular/forms';
44
import { HttpModule } from '@angular/http';
55

66
import { AppComponent } from './app.component';
7-
import { WelcomeComponent } from './welcome/welcome.component';
87
import { WelcomeComponent } from './welcome.component';
98
import { LoginComponent } from './login.component';
109
import { HomeComponent } from './home.component';
10+
import { UIRouterModule, UIView } from 'ui-router-ng2';
11+
import { APP_STATES } from './app.states';
1112

1213
@NgModule({
1314
declarations: [
@@ -17,11 +18,15 @@ import { HomeComponent } from './home.component';
1718
HomeComponent
1819
],
1920
imports: [
21+
UIRouterModule.forRoot({
22+
states: APP_STATES,
23+
otherwise: { state: 'home' }
24+
}),
2025
BrowserModule,
2126
FormsModule,
2227
HttpModule
2328
],
2429
providers: [],
25-
bootstrap: [AppComponent]
30+
bootstrap: [UIView]
2631
})
2732
export class AppModule { }

src/app/app.states.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { AppComponent } from './app.component';
2+
import { WelcomeComponent } from './welcome.component';
3+
import { HomeComponent } from './home.component';
4+
import { LoginComponent } from './login.component';
5+
import { Transition } from 'ui-router-core';
6+
7+
/**
8+
* This is the parent state for the entire application.
9+
*
10+
* This state's primary purposes are:
11+
* 1) Shows the outermost chrome (including the navigation and logout for authenticated users)
12+
* 2) Provide a viewport (ui-view) for a substate to plug into
13+
*/
14+
export const appState = {
15+
name: 'app',
16+
redirectTo: 'welcome',
17+
component: AppComponent,
18+
};
19+
20+
/**
21+
* This is the 'welcome' state. It is the default state (as defined by app.js) if no other state
22+
* can be matched to the URL.
23+
*/
24+
export const welcomeState = {
25+
parent: 'app',
26+
name: 'welcome',
27+
url: '/welcome',
28+
component: WelcomeComponent,
29+
};
30+
31+
/**
32+
* This is a home screen for authenticated users.
33+
*
34+
* It shows giant buttons which activate their respective submodules: Messages, Contacts, Preferences
35+
*/
36+
export const homeState = {
37+
parent: 'app',
38+
name: 'home',
39+
url: '/home',
40+
component: HomeComponent,
41+
};
42+
43+
44+
/**
45+
* This is the login state. It is activated when the user navigates to /login, or if a unauthenticated
46+
* user attempts to access a protected state (or substate) which requires authentication. (see routerhooks/requiresAuth.js)
47+
*
48+
* It shows a fake login dialog and prompts the user to authenticate. Once the user authenticates, it then
49+
* reactivates the state that the user originally came from.
50+
*/
51+
export const loginState = {
52+
parent: 'app',
53+
name: 'login',
54+
url: '/login',
55+
component: LoginComponent,
56+
resolve: { returnTo: returnTo }
57+
};
58+
59+
/**
60+
* A resolve function for 'login' state which figures out what state to return to, after a successful login.
61+
*
62+
* If the user was initially redirected to login state (due to the requiresAuth redirect), then return the toState/params
63+
* they were redirected from. Otherwise, if they transitioned directly, return the fromState/params. Otherwise
64+
* return the main "home" state.
65+
*/
66+
export function returnTo ($transition$: Transition): any {
67+
if ($transition$.redirectedFrom() != null) {
68+
// The user was redirected to the login state (e.g., via the requiresAuth hook when trying to activate contacts)
69+
// Return to the original attempted target state (e.g., contacts)
70+
return $transition$.redirectedFrom().targetState();
71+
}
72+
73+
const $state = $transition$.router.stateService;
74+
75+
// The user was not redirected to the login state; they directly activated the login state somehow.
76+
// Return them to the state they came from.
77+
if ($transition$.from().name !== '') {
78+
return $state.target($transition$.from(), $transition$.params("from"));
79+
}
80+
81+
// If the fromState's name is empty, then this was the initial transition. Just return them to the home state
82+
return $state.target('home');
83+
}
84+
85+
export const APP_STATES = [ appState, welcomeState, homeState, loginState ];

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<link rel="icon" type="image/x-icon" href="favicon.ico">
1010
</head>
1111
<body>
12-
<app-root>Loading...</app-root>
12+
<ui-view>Loading...</ui-view>
1313
</body>
1414
</html>

0 commit comments

Comments
 (0)