Commit 6f32352a authored by Laurent's avatar Laurent

SIP Loan refactoring

parent 0219e2a3
......@@ -2,8 +2,9 @@
import 'nativescript-i18n';
import * as app from 'tns-core-modules/application';
import {Database,Manager} from './models';
Database.open('prod');
import {Database, Manager, NSHTTPClient} from './models';
import * as config from "./config/config";
import { AccountsController } from './controllers/accounts';
import * as Frames from 'ui/frame';
function myReloadPage() {
......@@ -27,10 +28,9 @@ function myReloadPage() {
}
global.__onLiveSyncCore = myReloadPage;
import * as config from "./config/config";
Manager.url(config.MANAGER_URL);
import { AccountsController } from './controllers/accounts';
Database.open('prod');
let manager = new Manager(config.MANAGER_URL, new NSHTTPClient());
app.start({ moduleName: 'views/accounts/list',
bindingContext: new AccountsController()});
bindingContext: new AccountsController( manager ) });
......@@ -11,9 +11,11 @@ export class AccountsController extends Controller {
protected _portal: Portal = new Portal();
protected _portals: Array<MBPortal>;
protected _accounts: ObservableArray<Account>;
protected _manager: Manager;
public constructor() {
public constructor(manager: Manager) {
super();
this._manager = manager;
this._accounts = new ObservableArray<Account>();
this._refreshAccounts();
this.set('accounts', this._accounts);
......@@ -48,7 +50,7 @@ export class AccountsController extends Controller {
this.set('currently_edited', account);
this._saveAccount();
let library_controller = new LibraryController(account);
let library_controller = new LibraryController(account, this._manager);
library_controller.doRefreshLoansAndHolds();
this.navigate({
......@@ -109,7 +111,7 @@ export class AccountsController extends Controller {
this._portals = new Array<MBPortal>();
let portals_cache = this._portals;
new Manager().portals().then( (portals) => {
this._manager.portals().then( (portals) => {
portals.map( (element) => { portals_cache.push(element); });
});
......@@ -133,7 +135,7 @@ export class AccountsController extends Controller {
public openAccountAction(args) {
let account = this._accounts.getItem(args.index);
this.navigate({moduleName: "views/library/items",
bindingContext: new LibraryController(account)});
bindingContext: new LibraryController(account, this._manager)});
}
......
......@@ -29,6 +29,7 @@ export class LibraryController extends Controller {
protected _holds: ObservableArray<Hold> = new ObservableArray<Hold>();
protected _on_loans_page_loaded = null;
protected _on_successful_login = null;
protected _manager: Manager;
protected _view;
public setPortal(portal: Portal): LibraryController {
......@@ -37,10 +38,11 @@ export class LibraryController extends Controller {
}
public constructor(account: Account) {
public constructor(account: Account, manager: Manager) {
super();
this._account = account;
this._portal = new Portal();
this._manager = manager;
this._loadLoansAndHolds();
this.set('loans', this._loans);
......@@ -54,7 +56,7 @@ export class LibraryController extends Controller {
this.set('sip_loan', false);
(new Manager())
this._manager
.connect(Database.current(), this._account)
.then( (portal) => {
this.set('sip_loan', portal.sip_loan_feature);
......@@ -74,7 +76,7 @@ export class LibraryController extends Controller {
public goHomeAction() {
this.navigate({ moduleName: "views/accounts/list",
bindingContext: new AccountsController(),
bindingContext: new AccountsController(this._manager),
clearHistory: true });
}
......@@ -114,7 +116,7 @@ export class LibraryController extends Controller {
public editAccountAction() {
(new AccountsController()).editAccount(this._account);
new AccountsController(this._manager).editAccount(this._account);
}
......@@ -220,9 +222,6 @@ export class LibraryController extends Controller {
this
._loadLoansAndHolds()
._turnBusyIndicator('off');
this._view.getViewById("loans").notifyPullToRefreshFinished();
this._view.getViewById("holds").notifyPullToRefreshFinished();
})
.then( () => {
......@@ -270,12 +269,12 @@ export class LibraryController extends Controller {
this.navigate(
{ moduleName: "views/accounts/list",
bindingContext: (new AccountsController()).showAddAccountHint(),
bindingContext: (new AccountsController(this._manager)).showAddAccountHint(),
clearHistory: true });
}
protected _authenticationDone(account): LibraryController {
protected _authenticationDone(account:Account): LibraryController {
Database.current().save(account);
this.set('busy', true);
......@@ -366,7 +365,7 @@ export class LibraryController extends Controller {
}
protected _askLoginPassword(): LibraryController {
protected _askLoginPassword(): this {
this.set('credentials', this._account.credentials);
this.navigate({ moduleName: "views/library/login-password",
bindingContext: this,
......@@ -382,7 +381,7 @@ export class LibraryController extends Controller {
}
protected _closeDrawer(): LibraryController {
protected _closeDrawer(): this {
let sideDrawer: RadSideDrawer = <RadSideDrawer>(this._view.getViewById('sideDrawer'));
sideDrawer.closeDrawer();
return this;
......@@ -412,7 +411,10 @@ export class LibraryController extends Controller {
if (!this._view)
return this;
this._view.getViewById("loans").notifyPullToRefreshFinished();
this._view.getViewById("holds").notifyPullToRefreshFinished();
let indicator = this._view.getViewById("activity-indicator");
if (!indicator || (undefined == indicator.animate))
return this;
......
import { HTTPClient,
NSHTTPClient,
Account,
DataSource } from '../models';
import * as UrlParse from 'url-parse';
......@@ -7,18 +6,14 @@ import {MBPortal} from './manager/mbportal';
export {MBPortal};
export class Manager {
protected static _url: string;
protected _url: string;
protected _http: any;
protected _configuration: any;
public static url (url: string) {
Manager._url = url;
}
public constructor() {
this._http = new NSHTTPClient();
public constructor(url: string, http_client: HTTPClient) {
this._http = http_client;
this._url = url;
this._configuration = {};
}
......@@ -35,7 +30,7 @@ export class Manager {
portal = new MBPortal().setURL(account.getUrl());
return this._http
.request({ url: Manager._url + '/api/portal/' + portal.getKey(),
.request({ url: this._url + '/api/portal/' + portal.getKey(),
headers: { "Content-Type": "application/json" } })
.then( (response) => {
let datas = response.content.toJSON();
......@@ -56,7 +51,7 @@ export class Manager {
public portals(): Promise<any> {
return this._http
.request({ url: Manager._url + '/api/portals' })
.request({ url: this._url + '/api/portals' })
.then( (response) => {
let datas = response.content.toJSON();
let portals = datas.map( (portal) => {
......
declare var describe, expect, it, before, chai: any;
import {AccountsController} from '../controllers/accounts';
import {LibraryController} from '../controllers/library';
import {Account, Database} from '../models/';
import {Account, Database, Manager, NullHTTPClient} from '../models/';
import {StubFrames} from './stub-frames';
describe('AccountsController with one account in database', () => {
......@@ -10,14 +10,16 @@ describe('AccountsController with one account in database', () => {
let navigate_spy;
let goback_spy;
let bibliofil: Account;
let manager: Manager;
before( () => {
bibliofil = (new Account()).setLabel('Bibliofil');
bibliofil = new Account().setLabel('Bibliofil');
manager = new Manager('http://nowhere.com', new NullHTTPClient());
db = Database.open('testdb').clear();
db.save(bibliofil);
accounts_controller = new AccountsController();
accounts_controller = new AccountsController(manager);
accounts_controller.frames = new StubFrames();
navigate_spy = chai.spy.on(accounts_controller.frames, 'navigate');
......@@ -107,6 +109,6 @@ describe('AccountsController with one account in database', () => {
accounts_controller.openAccountAction({index:0});
let first_account = accounts_controller.get('accounts').getItem(0);
expect(navigate_spy).to.have.been.called.with({moduleName: "views/library/items",
bindingContext: new LibraryController(first_account)});
bindingContext: new LibraryController(first_account, manager)});
});
});
declare var describe, expect, it, before, chai: any;
import {AccountsController} from '../controllers/accounts';
import {Account, Database} from '../models/';
import {Account, Database, Manager, NullHTTPClient} from '../models/';
import {StubFrames} from './stub-frames';
import {StubDialogs} from './stub-dialogs';
import {Observable} from 'data/observable';
......@@ -11,6 +11,7 @@ describe('AccountsController with one account in database', () => {
let navigate_spy;
let bibliofil: Account;
let account_form: Observable;
let manager: Manager;
before( () => {
bibliofil = (new Account())
......@@ -23,7 +24,8 @@ describe('AccountsController with one account in database', () => {
db = Database.open('testdb').clear();
db.save(bibliofil);
accounts_controller = new AccountsController();
manager = new Manager('http://nowhere.com', new NullHTTPClient());
accounts_controller = new AccountsController(manager);
accounts_controller.frames = new StubFrames();
navigate_spy = chai.spy.on(accounts_controller.frames, 'navigate');
......
......@@ -72,8 +72,6 @@ describe('Manager', () => {
beforeEach(() => {
db = Database.open('testdb').clear();
Manager.url('http://mbam.org');
manager = new Manager();
});
......@@ -82,7 +80,7 @@ describe('Manager', () => {
beforeEach( () => {
annecy = (new Account()).setUrl('http://library.annecy.fr');
manager.setHTTP(new ManagerConnectAnnecy('http://mbam.org'));
manager = new Manager('http://mbam.org', new ManagerConnectAnnecy('http://mbam.org'));
});
......@@ -106,8 +104,7 @@ describe('Manager', () => {
it('with no existing description it should stub it', () => {
return manager
.setHTTP(new ManagerConnectAnnecyNotFound('http://mbam.org'))
return new Manager('http://mbam.org', new ManagerConnectAnnecyNotFound('http://mbam.org'))
.connect(db, annecy).then( (portal) => {
expect(portal.getLabel()).to.equals('');
expect(portal.getURL()).to.equals('http://library.annecy.fr');
......@@ -120,8 +117,7 @@ describe('Manager', () => {
describe('on server with two portals', () => {
beforeEach( () => {
let http = new ManagerWithTwoPortals('http://mbam.org');
manager.setHTTP(http);
manager = new Manager('http://mbam.org', new ManagerWithTwoPortals('http://mbam.org'))
});
......
declare var describe, expect, it, beforeEach, chai: any;
import {LibraryController} from '../controllers/library';
import {AccountsController} from '../controllers/accounts';
import {Loan, Account, Database, Portal, NullHTTPClient} from '../models/';
import {Loan, Account, Database, Portal, Manager, NullHTTPClient} from '../models/';
import {StubFrames} from './stub-frames';
import {OrpheeMediaWithCasLoansSuccessful} from './portal/orpheemedia-fixtures';
......@@ -10,8 +10,11 @@ describe('Loans for account bibliofil', () => {
let db: Database;
let bibliofil: Account;
let library_controller: LibraryController;
let manager: Manager;
beforeEach( () => {
manager = new Manager('http://nowhere.com', new NullHTTPClient());
db = Database.open('testdb').clear();
bibliofil = (new Account())
......@@ -44,7 +47,7 @@ describe('Loans for account bibliofil', () => {
.setTitle('I have no account')
.setDateDue('2017-05-12'));
library_controller = new LibraryController(bibliofil);
library_controller = new LibraryController(bibliofil, manager);
library_controller.frames = new StubFrames();
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment