Skip to content
This repository has been archived by the owner on Aug 18, 2019. It is now read-only.

Commit

Permalink
refactoring & debuging
Browse files Browse the repository at this point in the history
  • Loading branch information
aguacongas committed Oct 30, 2016
1 parent 6da6593 commit ec2296b
Show file tree
Hide file tree
Showing 14 changed files with 28,680 additions and 53 deletions.
11 changes: 1 addition & 10 deletions src/components/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,7 @@ export class Contact {
}

if (!this.user.conversation) {
let conversation = new Conversation();
let attendees = new Array<Attendee>();
let attendee = new Attendee();

attendee.userId = this.user.id;
attendees.push(attendee);
conversation.attendees = attendees;
conversation.messages = new Array<Message>();

this.user.conversation = conversation;
this.user.conversation = new Conversation(this.user);
}

this.service.showConversation(this.user.conversation, this.router);
Expand Down
1 change: 1 addition & 0 deletions src/components/conversation-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class ConversationPreview {

attached() {
this.lastMessage = this.conversation.messages[0].text;
this.isSelected = this.conversation && this.conversation.isInitiatedByUser;

this.conversationSelectedSubscription = this.ea.subscribe(ConversationSelected, e => {
if (e.conversation.id === this.conversation.id) {
Expand Down
6 changes: 3 additions & 3 deletions src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
debug: false,
testing: false,
apiBaseUrl: 'https://chatle-server.herokuapp.com'
debug: true,
testing: true,
apiBaseUrl: 'http://localhost:5000'
};
16 changes: 15 additions & 1 deletion src/model/conversation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { Attendee } from './attendee';
import { Message } from './message';
import { User } from './user';

export class Conversation {
id: string;
title: string;
attendees: Attendee[];
messages: Message[]
messages: Message[];
isInitiatedByUser: boolean;

constructor(user?: User) {
if (!user) {
return;
}

let attendees = new Array<Attendee>();
attendees.push(new Attendee(user.id));
this.attendees = attendees;
this.messages = new Array<Message>();
this.isInitiatedByUser = true;
}
}
4 changes: 4 additions & 0 deletions src/model/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class Provider {
displayName: string;
authenticationScheme: string;
}
5 changes: 2 additions & 3 deletions src/pages/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export class Home {
private connectionStateSubscription: Subscription;
private showConversationSubscription: Subscription;

constructor(public chatService: ChatService,
private loginService: LoginService,
constructor(private chatService: ChatService,
private ea: EventAggregator) { }

configureRouter(config: RouterConfiguration, router: Router) {
Expand Down Expand Up @@ -44,7 +43,7 @@ export class Home {

private setIsDisconnected(state: ConnectionState) {
if (state === ConnectionState.Error) {
this.loginService.logoff();
this.router.navigateToRoute('login');
} if (state === ConnectionState.Disconnected) {
this.isDisconnected = true;
} else {
Expand Down
6 changes: 2 additions & 4 deletions src/pages/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ <h4>Guess access.</h4>
<section>
<h4>Use another service to log in.</h4>
<hr>
<form class="form-horizontal" role="form" method="post" action.bind="externalLogin" disabled.bind="token">
<form class="form-horizontal" role="form" method="post" action.bind="externalLogin">
<div>
<p>
<button type="submit" class="btn btn-default" name="provider" value="Google" title="Log in using your Google account">Google</button>
<button type="submit" class="btn btn-default" name="provider" value="Twitter" title="Log in using your Twitter account">Twitter</button>
<button type="submit" class="btn btn-default" name="provider" value="Facebook" title="Log in using your Facebook account">Facebook</button>
<button type="submit" class="btn btn-default" name="provider" repeat.for="provider of providers" value.bind="provider.authenticationScheme" title="Log in using your ${provider.displayName} account">${provider.displayName}</button>
</p>
</div>
<input name="__RequestVerificationToken" type="hidden" value.bind="token"></form>
Expand Down
10 changes: 8 additions & 2 deletions src/pages/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { Router } from 'aurelia-router';
import { LoginService } from '../services/login.service';
import { Settings } from '../config/settings';
import { State } from '../services/state';
import { Provider } from '../model/provider';

@autoinject
export class Login {
error: Error;
externalLogin: string;
token: string;
providers: Array<Provider>;

constructor(private service: LoginService, private router: Router, private state: State, settings: Settings) {
let location = window.location;
Expand All @@ -32,8 +34,12 @@ export class Login {
activate() {
this.service.logoff();
this.service.getXhrf(true)
.then(t =>
this.token = t)
.then(t => {
this.token = t;
this.service.getExternalLoginProviders()
.then(providers => this.providers = providers)
.catch(e => this.error = e);
})
.catch(e =>
this.error = e);
}
Expand Down
15 changes: 15 additions & 0 deletions src/services/login.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Settings } from '../config/settings';
import { ChatService } from './chat.service';
import { Helpers } from './helpers';
import { State } from './state';
import { Provider } from '../model/provider'

@autoinject
export class LoginService {
Expand Down Expand Up @@ -95,6 +96,20 @@ export class LoginService {
});
}

getExternalLoginProviders(): Promise<Array<Provider>> {
return new Promise<Array<Provider>>((resolve, reject) => {
this.getXhrf()
.then(r => {
this.http.get(this.settings.accountdAPI + "/getExternalProviders")
.then(response => {
resolve(response.content as Array<Provider>);
})
.catch(error => this.manageError(error, reject, this.helpers.getError(error)));
})
.catch(error => reject(new Error('the service is down')));
});
}

private setXhrf(resolve: Function, reject: Function) {
this.http.get('xhrf')
.then(r => {
Expand Down
27 changes: 16 additions & 11 deletions test/unit/pages/home.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,12 @@ describe('home page specs', () => {
}
} as Promise<string>;

loginService = {
logoff: () => { }
} as LoginService;

chatService = {
start: () => { }
} as ChatService;

ea = new EventAggregator();
page = new Home(chatService, loginService, ea);
page = new Home(chatService, ea);
});

it('configureRouter should add conversation route in router configuration', () => {
Expand Down Expand Up @@ -64,7 +60,7 @@ describe('home page specs', () => {
map: m => { }
} as RouterConfiguration;
let router = {
baseUrl: 'test'
baseUrl: 'test',
} as Router;

// act
Expand All @@ -80,6 +76,7 @@ describe('home page specs', () => {
let subcription = {
dispose: () => { }
};
let router;

beforeEach(() => {
ea.subscribe = (e, c) => {
Expand All @@ -88,6 +85,11 @@ describe('home page specs', () => {

return subcription;
};

router = {
navigateToRoute: name => {},
} as Router;
page.router = router;
});

it('attached should set isDisconnected to true when connaction state is Disconnected', () => {
Expand Down Expand Up @@ -115,13 +117,15 @@ describe('home page specs', () => {
it('attached should logoff when connection state is Error', () => {
// prepare
chatService.currentState = ConnectionState.Error;
spyOn(loginService, 'logoff');

spyOn(router, 'navigateToRoute');

// act
page.attached();

// verify
expect(page.isDisconnected).toBe(false);
expect(loginService.logoff).toHaveBeenCalledTimes(1);
expect(router.navigateToRoute).toHaveBeenCalledWith('login');
});

it('attached should start chat when connection state is not Connected', () => {
Expand All @@ -136,7 +140,7 @@ describe('home page specs', () => {
expect(chatService.start).toHaveBeenCalledTimes(1);
});

it('ConnectionStateChanged subcription callback should set isDisconnected to true when connaction state is Disconnected', () => {
it('ConnectionStateChanged subcription callback should set isDisconnected to true when connection state is Disconnected', () => {
// prepare
chatService.currentState = ConnectionState.Connected;

Expand All @@ -163,14 +167,15 @@ describe('home page specs', () => {
it('ConnectionStateChanged subcription callback should logoff when connection state is Error', () => {
// prepare
chatService.currentState = ConnectionState.Connected;
spyOn(loginService, 'logoff');

spyOn(router, 'navigateToRoute');
// act
page.attached();
callback(new ConnectionStateChanged(ConnectionState.Error));

// verify
expect(page.isDisconnected).toBe(false);
expect(loginService.logoff).toHaveBeenCalledTimes(1);
expect(router.navigateToRoute).toHaveBeenCalledWith('login');
});

it('detached should dispose subcription', () => {
Expand Down
5 changes: 3 additions & 2 deletions test/unit/pages/login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ describe('Login page specs', () => {
},
login: (userName: string) => {
return promise;
},
logoff: () => {}
},
logoff: () => {},
getExternalLoginProviders: () => { return promise ;}
} as LoginService;

settings = {
Expand Down
2,806 changes: 2,803 additions & 3 deletions wwwroot/scripts/app-bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion wwwroot/scripts/app-bundle.js.map

Large diffs are not rendered by default.

25,819 changes: 25,806 additions & 13 deletions wwwroot/scripts/vendor-bundle.js

Large diffs are not rendered by default.

0 comments on commit ec2296b

Please sign in to comment.